gpt4 book ai didi

android - mGoogleApiClient 无法在另一个 Activity 中实现

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:49:49 24 4
gpt4 key购买 nike

我在我的项目中遇到了一个问题。我想使用“Google API”登录我的 LoginActivity。并从另一个 Activity (名为 WelcomeActivity)注销

登录 Activity :(代码为 here )

public class LoginActivity extends AppCompatActivity implements
GoogleApiClient.OnConnectionFailedListener,
View.OnClickListener {

// Configuration of Google API - Step 1/3
private static final String TAG = "LoginActivity";
private static final int RC_SIGN_IN = 9001;
public static GoogleApiClient mGoogleApiClient;
private ProgressDialog mProgressDialog;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
GoogleAPI();

}
public void GoogleAPI(){

// Button listeners
findViewById(R.id.sign_in_button).setOnClickListener(this);

GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestEmail()
.build();

mGoogleApiClient = new GoogleApiClient.Builder(this)
.enableAutoManage(this /* FragmentActivity */, this /* OnConnectionFailedListener */)
.addApi(Auth.GOOGLE_SIGN_IN_API, gso)
.build();
}

@Override
public void onStart() {
super.onStart();
....
}

// [START onActivityResult]
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...);
if (requestCode == RC_SIGN_IN) {
GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
handleSignInResult(result);
}
}
// [END onActivityResult]

// [START handleSignInResult]
private void handleSignInResult(GoogleSignInResult result) {

if (result.isSuccess()) {
// Signed in successfully, show authenticated UI.
CustomApplication app = (CustomApplication)getApplication();
GoogleSignInAccount acct = result.getSignInAccount();
Intent i = new Intent(LoginActivity.this, WelcomePage.class);
i.putExtra("Username", acct.getDisplayName());
startActivity(i);
}
}
// [END handleSignInResult]

// [START signIn]
private void signIn() {
Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
startActivityForResult(signInIntent, RC_SIGN_IN);
}
// [END signIn]

@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
...
}

private void showProgressDialog() {
...
}

private void hideProgressDialog() {
...
}

@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.sign_in_button:
signIn();
break;
….
}
}

}

我想在欢迎 Activity 中使用 Sign_out 方法,

private void signOut() {
// Auth.GoogleSignInApi.signOut(mGoogleApiClient).setResultCallback(
// new ResultCallback<Status>() {
// @Override
// public void onResult(Status status) {
// // [START_EXCLUDE]
//// updateUI(false);
// // [END_EXCLUDE]
// }
// });
// }

为了解决这个问题,我尝试了两种方法:

  1. 将 mGoogleApiClient 作为全局变量(扩展应用程序或单例),我尝试了,但失败了,在欢迎页面中,mGoogleApiClient 不为空,但错误是:mGoogleApiClient 尚未连接.

  2. 我调用了 LoginActivity.getMGoogleApiClient(静态变量),但也失败了,同样的错误:mGoogleApiClient 尚未连接

我已经搜索这个问题好几天了,但没有解决它的有用方法,请帮助我..

最佳答案

当您启用 AutoManage 时,googleApiClient 会在开始时连接并在停止时断开连接,并由库自动处理。你可以做的是,在其他 Activity 中注销用户,无论你的代码是从你的后端注销用户,除了不要从谷歌注销。

伪代码:

private void logOut(){
//your logout code in the log out activity

setCurrentUser(null);
}

在 googleApiClient 的 Activity 中,您可以检查用户是否已登录到 googleApiClient 的 onConnected 回调中。如果当前用户未登录,请将用户从 Google 中注销。

@Override
public void onConnected(Bundle connectionHint) {

if (getCurrentUser() == null){
signOut();
}

}

private void signOut() {
Auth.GoogleSignInApi.signOut(mGoogleApiClientPlus).setResultCallback(
new ResultCallback<Status>() {
@Override
public void onResult(Status status) {
//signed out.
}
});
}

如果您不使用 enableAutoManage 并且不在 onStop 中断开客户端(我不知道是否推荐这样做),您可以使用您的 2 种方法,但我不建议对 googleApiClient 对象使用静态字段。

关于android - mGoogleApiClient 无法在另一个 Activity 中实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33745845/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com