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

LoginActivity:
(代码为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]
//                    }
//                });
//    }

为了解决这个问题,我尝试了两种方法:
  • 使mGoogleApiClient成为全局变量(扩展Application或Singleton),我尝试了一下,但是失败了,在Welcome页面中,mGoogleApiClient不为null,但错误是: mGoogleApiClient未连接但
  • 我调用LoginActivity.getMGoogleApiClient(静态变量),但是也失败了,同样的错误: mGoogleApiClient尚未连接

  • 我已经搜索了好几天了,但是没有什么有用的解决方法,请帮助我..

    最佳答案

    启用AutoManage时,googleApiClient会在onStart上建立连接,并在onStop上断开连接,并由库自动处理。您可能要做的是,在其他 Activity 中注销用户,无论用什么代码从您的后端注销用户,除非尚未从Google注销。

    伪代码:

    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 - 无法在其他 Activity 中实现mGoogleApiClient,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33745845/

    10-10 00:53