【问题标题】:Logout Google Plus from another activity of the application android?从应用程序 android 的另一个活动中注销 Google Plus?
【发布时间】:2018-01-12 07:22:04
【问题描述】:

我正在将 Google Plus 集成到 android 应用程序中。我已经在 Login Screen 中完成了所有登录过程,在成功场景之后,我刚刚完成了该活动并移至下一个活动,即 HomeActivty。我必须从此 HomeActivity 中退出 Google Plus。问题是我在 Login Activity 中声明了所有 GoogleApiClient。在搜索需要 GoogleApiClient 对象的注销时,我得到了一个代码。错误是没有活动存在,因为我在登录后已经完成了该活动。我的问题是关于你能否建议我知道如果用户没有注销我必须检查并让他从另一个活动中注销?

我的登录活动代码

    @Override
        public void onClick(View view) {
            switch (view.getId()) {


                case R.id.btn_gplus:
                    GlobalPreferManager.setString(Keys.SIGN_IN_METHOD, "google_plus");
                    if (!new ConnectionInfo(getActivity()).isConnectingToInternet()) {
                        CommonUtils.showToast(getActivity(), "No Internet Connection");
                    } else {
                        signIn();
                    }

                    break;
            }
        }

 private void signIn() {
        Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
        startActivityForResult(signInIntent, RC_SIGN_IN);
    }

  @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == RC_SIGN_IN) {
            Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
            handleSignInResult(task);
        }
    }

 private void handleSignInResult(Task<GoogleSignInAccount> completedTask) {
        try {
            GoogleSignInAccount account = completedTask.getResult(ApiException.class);
            GlobalPreferManager.setBoolean(Keys.IS_LOGGED_IN, true);
            GlobalPreferManager.setString(Keys.SIGN_IN_METHOD, "google_plus");
            startActivity(new Intent(getActivity(), LocationActivity.class));
            getActivity().finish();

        } catch (ApiException e) {
            CommonUtils.showToast(getActivity(), "Failed to connect!");
        }
    }

我的家庭活动代码

 @Override
 public void onClick(View view) {

 Auth.GoogleSignInApi.signOut(LoginFragment.mGoogleApiClient);

   }

【问题讨论】:

    标签: android google-plus google-plus-signin


    【解决方案1】:

    您所要做的就是在不同的类 GooglePlusLogin 中编写所有 google 集成代码(您可以随意命名),并在您的 CallingActivity 中创建该类的 object 以进行 google 登录,并且您可以通过使 signOut() 可访问来调用 signOut() 方法。

    public class GooglePlusLogin implements GoogleApiClient.OnConnectionFailedListener, GoogleApiClient.ConnectionCallbacks {
    
        private GoogleApiClient mGoogleApiClient;
        FragmentActivity context;
        public static final int RESULT_OK = -1;
        public static final int RC_SIGN_IN = 9001;
        private OnClientConnectedListener listener;
        private int type;
    
        public GooglePlusLogin(FragmentActivity context, OnClientConnectedListener listener) {
            this.context = context;
            this.listener = listener;
            GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                    .requestEmail()
                    .build();
            mGoogleApiClient = new GoogleApiClient.Builder(context)
                    .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
                    .addScope(new Scope(Scopes.PROFILE))
                    .addConnectionCallbacks(this)
                    .addOnConnectionFailedListener(this)
                    .build();
        }
    
        public void onActivityResult(int requestCode, int resultCode, Intent data) {
            if (requestCode == RC_SIGN_IN && mGoogleApiClient.isConnected()) {
                GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
                handleSignInResult(result);
            } else {
                signIn(type);
            }
        }
    
        public void signIn(int type) {
            this.type = type;
            revokeAccess();
        }
    
        private void revokeAccess() {
            Auth.GoogleSignInApi.revokeAccess(mGoogleApiClient).setResultCallback(
                    new ResultCallback<Status>() {
                        @Override
                        public void onResult(Status status) {
                            Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
                            ((Activity) context).startActivityForResult(signInIntent, RC_SIGN_IN);
                        }
                    });
        }
    
    
        private void handleSignInResult(GoogleSignInResult result) {
            Log.d(TAG, "handleSignInResult:" + result.isSuccess());
            if (result.isSuccess()) {
                GoogleSignInAccount acct = result.getSignInAccount();
                String imageUrl = "";
                String email = "";
                String fullname = "";
    
                if (acct.getPhotoUrl() != null) {
                    imageUrl = acct.getPhotoUrl().toString();
                }
                if (acct.getEmail() != null) {
                    email = acct.getEmail();
                }
                if (acct.getDisplayName() != null) {
                    fullname = acct.getDisplayName();
                }
                acct.getIdToken();
                listener.onGoogleProfileFetchComplete(fullname, acct.getId(), email, imageUrl, type);
                signOut();
            } else {
                listener.onClientFailed(type);
                signOut();
            }
    
        public void onStart() {
            if (mGoogleApiClient != null) {
                mGoogleApiClient.connect();
            }
        }
    
        public void onStop() {
            if (mGoogleApiClient != null && mGoogleApiClient.isConnected()) {
                mGoogleApiClient.disconnect();
            }
        }
    
        @Override
        public void onConnectionFailed(ConnectionResult connectionResult) {
            Log.d(TAG, "onConnectionFailed:" + connectionResult);
            ((OnClientConnectedListener) context).onClientFailed(type);
        }
    
        @Override
        public void onConnected(@Nullable Bundle bundle) {
    
        }
    
        @Override
        public void onConnectionSuspended(int i) {
    
        }
    
    // implement this interface in your `CallingActivity`
    
        public interface OnClientConnectedListener {
            void onGoogleProfileFetchComplete(String fullname, String acctId, String email, String imageUrl, int forType);
    
            void onClientFailed(int forType);
        }
    }
    

    // LoginActivity中的实现

    public class LoginActivity extends Activity implements GooglePlusLogin.OnClientConnectedListener {
    
        public GooglePlusLogin plusLogin;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_login);
            initViews();
            plusLogin = new GooglePlusLogin(this, this);
        }
    
        // this is becuase I haven't used enableAutoManage() while connecting to googleApiClient in GooglePLusLogin
    
        @Override
        protected void onStart() {
            super.onStart();
            if (plusLogin != null) {
                plusLogin.onStart();
            }
        }
    
        @Override
        protected void onStop() {
            super.onStop();
            if (plusLogin != null) {
                plusLogin.onStop();
            }
        }
    
        @Override
        public void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
            if (requestCode == GooglePlusLogin.RC_SIGN_IN) {
                plusLogin.onActivityResult(requestCode, resultCode, data);
            }
        }
    
        @Override
        public void onGoogleProfileFetchComplete(String fullname, String acctId, String email, String imageUrl, int forType) {
    
            // here you will get all the details of user   
            plusLogin.signOut();
    
        }
    
        @Override
        public void onClientFailed(int forType) {
            plusLogin.signOut();
        }
    }
    

    【讨论】:

    • 是的,它的工作原理。每次进入活动时,我们都会启动和停止 googleapiclient 对象。
    • 如果您在应用程序中的很多地方都使用了 google API 客户端对象,那么是的,调用 onStart() 和 onStop() 会很有成效,如果没有,则 enableAutoManage() 会自动处理这个问题。跨度>
    猜你喜欢
    • 1970-01-01
    • 2015-05-31
    • 1970-01-01
    • 2014-08-22
    • 1970-01-01
    • 1970-01-01
    • 2013-07-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多