简体   繁体   English

如何从Android中的谷歌帐户注销?

[英]How to log out from google account in android ?

I am developing application that uses login using google Auth2.0 with android account manager. 我正在开发使用google Auth2.0与Android客户经理登录的应用程序。 I am login successfully and fetching data from my google using different apis but I don't know about how to log out from my application and when logout want to shows login screen once again. 我已成功登录并使用不同的apis从我的谷歌获取数据但我不知道如何从我的应用程序注销以及注销时想要再次显示登录屏幕。

I don't think you can log-out, you will have to show the AccountChooser again 我不认为你可以注销,你将不得不再次显示AccountChooser

try 尝试

Intent intent = AccountPicker.newChooseAccountIntent(null, null, new String[] {"com.google"}, false, null, null, null, null);
 startActivityForResult(intent, SOME_REQUEST_CODE);

Usually i saved the account name on SharedPreferences and on log-out just remove the account name from the SharedPreferences. 通常我在SharedPreferences上保存了帐户名,在注销时只需从SharedPreferences中删除帐户名。 Saving the account name from onActivityResult onActivityResult保存帐户名称

if (resultCode == Activity.RESULT_OK && data != null && data.getExtras() != null) {
                String accountName = data.getExtras().getString(AccountManager.KEY_ACCOUNT_NAME);
                if (accountName != null) {
                    SharedPreferences settings = getPreferences(Context.MODE_PRIVATE);
                    SharedPreferences.Editor editor = settings.edit();
                    editor.putString(PREF_ACCOUNT_NAME, accountName);
                    editor.commit();
                    //do the rest after saving the account name on SharedPreferences
                }
            }

And log out(my log out occur on a different activity): 并注销(我的注销发生在不同的活动上):

private void logOut(){
    SharedPreferences sharedPreferences = getSharedPreferences("MainActivity",Context.MODE_PRIVATE);
    if (sharedPreferences.getString(PREF_ACCOUNT_NAME,null)!=null){
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.remove(PREF_ACCOUNT_NAME);
        editor.commit();
        //here show the log-in screen again
    }
}

While logging in you have saved data ie your access token in shared preferenes. 登录时您已保存数据,即共享优先级中的访问令牌。 So when you want to LogOut clear shared preferences. 因此,当您想要LogOut清除共享首选项时。 It is the only way to logout. 这是退出的唯一方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM