我想要使用Google帐户API构建应用。我完成了谷歌教程的登录,它运作良好,但我尝试从另一个活动注销帐户,它也有效,但方法" onStart"在我注释" onStart"时,在类LoginActivity.java登录帐户中它还可以,但是当我再次尝试登录时,登录已登录的帐户。对于将googleApiClient对象传递给我使用单例的另一个活动。你能救我吗?
代码:
public void onStart() {
super.onStart();
OptionalPendingResult<GoogleSignInResult> opr = Auth.GoogleSignInApi.silentSignIn(mGoogleApiClient);
if (opr.isDone()) {
Log.d(TAG, "Got cached sign-in");
GoogleSignInResult result = opr.get();
handleSignInResult(result);
} else {
showProgressDialog();
opr.setResultCallback(new ResultCallback<GoogleSignInResult>() {
@Override
public void onResult(GoogleSignInResult googleSignInResult) {
hideProgressDialog();
handleSignInResult(googleSignInResult);
}
});
}
}
HomeActivity.java:
public class HomeActivity extends AppCompatActivity implements View.OnClickListener {
private GoogleApiClient mGoogleApiClient;// = state.getmGoogleApiClient();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
GlobalState state = ((GlobalState) getApplicationContext());
mGoogleApiClient = state.getmGoogleApiClient();
findViewById(R.id.sign_out_button).setOnClickListener(this);
}
private void signOut(){
Auth.GoogleSignInApi.signOut(mGoogleApiClient).setResultCallback(
new ResultCallback<Status>() {
@Override
public void onResult(Status status) {
GlobalState state = ((GlobalState) getApplicationContext());
state.setmGoogleApiClient(mGoogleApiClient);
Intent login = new Intent(getApplicationContext(), LoginActivity.class);
login.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(login);
finish();
}
});
}
@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.sign_out_button:
signOut();
break;
}
}
答案 0 :(得分:1)
使用
更改onStart() @Override
protected void onStart() {
super.onStart();
mGoogleApiClient.connect();
}
答案 1 :(得分:0)
我通过在HomeActivity中添加onStart来解决它:
@Override
protected void onStart() {
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestEmail()
.build();
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(Auth.GOOGLE_SIGN_IN_API, gso)
.build();
mGoogleApiClient.connect();
super.onStart();
}