如何从不同的活动中注销G + plus sigin?

时间:2018-03-01 04:33:25

标签: android google-api google-oauth

我已经在同一个活动上成功实现了他的代码,但是如何从另一个Activity中注销。

引用完整代码:https://www.androidhive.info/2014/02/android-login-with-google-plus-account-1/

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

2 个答案:

答案 0 :(得分:0)

我在我的一个项目中做了以下......

public class MainActivity extends AppCompatActivity {


    private GoogleSignInClient mGoogleSignInClient;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);  


        // Configure Google Sign In
        GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                .requestIdToken(getString(R.string.default_web_client_id))
                .requestEmail()
                .build();   

        mGoogleSignInClient = GoogleSignIn.getClient(this, gso);   

    }  

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.signout) {
            //DialogUtils.getInstance().
            signOut();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    private void signOut() {        

        // Google sign out
        mGoogleSignInClient.signOut().addOnCompleteListener(this,
                new OnCompleteListener<Void>() {
                    @Override
                    public void onComplete(@NonNull Task<Void> task) {
                       startActivity(new Intent(MainActivity.this,LoginActivity.class));
                        finish();
                    }
                });
    }
}

答案 1 :(得分:0)

在第二个活动创建

public GoogleActivity google_activity;

在Google Activity初始化

SecondActivity.google_activity=this;

然后你可以打电话

google_activity.signout();