簡體   English   中英

如何從Android中的谷歌帳戶注銷?

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

我正在開發使用google Auth2.0與Android客戶經理登錄的應用程序。 我已成功登錄並使用不同的apis從我的谷歌獲取數據但我不知道如何從我的應用程序注銷以及注銷時想要再次顯示登錄屏幕。

我不認為你可以注銷,你將不得不再次顯示AccountChooser

嘗試

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

通常我在SharedPreferences上保存了帳戶名,在注銷時只需從SharedPreferences中刪除帳戶名。 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
                }
            }

並注銷(我的注銷發生在不同的活動上):

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
    }
}

登錄時您已保存數據,即共享優先級中的訪問令牌。 因此,當您想要LogOut清除共享首選項時。 這是退出的唯一方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM