简体   繁体   中英

How to log out from google account in android ?

I am developing application that uses login using google Auth2.0 with android account manager. 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.

I don't think you can log-out, you will have to show the AccountChooser again

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. Saving the account name from 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. It is the only way to logout.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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