从Android应用程序的另一个活动注销Google Plus?

时间:2018-01-12 07:22:04

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

我在Android应用程序中集成了 Google Plus 。我已经在登录屏幕中完成了所有登录过程,在成功方案之后,我刚刚完成该活动并转移到下一个活动,即 HomeActivty 。我必须从 HomeActivity 中退出 Google Plus 。问题是我在登录活动中声明了所有 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);

   }

1 个答案:

答案 0 :(得分:1)

您所需要做的就是在不同的课程GooglePlusLogin中编写所有Google集成代码(您可以根据需要命名)并在object中创建该课程的CallingActivity要进行谷歌登录,您可以通过使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();
    }
}