简体   繁体   中英

Google sign-in in android app

I have a very general question about the google login in android applications.

Lets say I am using xxx@gmail.com as an account on my android device. I logged in through this account in an android application (com.test.app).

Now, I removed the account from the device.

Question: Should I get logged out from com.test.app or not ? (what is advisable)

Self Digging In almost all the apps that I have seen, it doesnt get logged out.

Use SessionManager & SharedPreferences

Create A Button in your XML & Add OnClick Event On it.

Then

 session = new CustomSessionManager(getApplicationContext());
            session.logoutUser();

Here is my CustomSessionManager Class.

 public class CustomSessionManager {
    // Shared Preferences
    SharedPreferences pref;

    // Editor for Shared preferences
    SharedPreferences.Editor editor;

    // Context
    Context _context;

    // Shared pref mode
    int PRIVATE_MODE = 0;

    // Sharedpref file name
    private static final String PREF_NAME = "Amiyo";

    // All Shared Preferences Keys
    private static final String IS_LOGIN = "IsLoggedIn";

    // User name (make variable public to access from outside)
   // public static final String KEY_NAME = "name";

    // Email address (make variable public to access from outside)
    public static final String KEY_EMAIL = "UserEmail";

    // Constructor
    public CustomSessionManager(Context context){
        this._context = context;
        pref = _context.getSharedPreferences(PREF_NAME, PRIVATE_MODE);
        editor = pref.edit();
    }

    /**
     * Create login session
     * */
    public void createLoginSession(String UserEmail){
        // Storing login value as TRUE
        editor.putBoolean(IS_LOGIN, true);

        // Storing name in pref
      //  editor.putString(KEY_NAME, name);

        // Storing email in pref
        editor.putString(KEY_EMAIL, UserEmail);

        // commit changes
        editor.commit();
    }

/**
     * Clear session details
     * */
    public void logoutUser(){
        // Clearing all data from Shared Preferences
        editor.clear();
        editor.commit();

        // After logout redirect user to Loing Activity
        Intent i = new Intent(_context, MainActivity.class);
        i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        i.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
        // Closing all the Activities
        i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
        // Add new Flag to start new Activity
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        // Staring Login Activity
        _context.startActivity(i);


    }



    /**
     * Quick check for login
     * **/
    // Get Login State
    public boolean isLoggedIn(){
        return pref.getBoolean(IS_LOGIN, false);
    }
}

For more details you can visit here http://www.androidhive.info/2012/08/android-session-management-using-shared-preferences/

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