尝试使用Android SharedPreferences维护登录状态

The*_*ter 1 android login sharedpreferences

我有一个扩展应用程序的类:

public class myApp extends Application {

    public static boolean isUserLoggedIn = false;
    public static String username = null;
    public static SharedPreferences logInState;

    public static boolean userLogin() {

        return myApp.isUserLoggedIn = true;
    }

    public static boolean userLogout() {

        return myApp.isUserLoggedIn = false;
    }

    public static void setUser(String s) {

        myApp.username = s;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        String PREFS_NAME = "LoginState";
        logInState = getSharedPreferences(PREFS_NAME,
                MODE_PRIVATE);
    }

}
Run Code Online (Sandbox Code Playgroud)

我觉得我在那里只是有一个真/假变量来指示用户是否需要在每次启动应用程序时登录.当然,我也会在SharedPreference中存储用户名,但是为了这个问题,我只讨论一个以保持简单.(请记住,用户无需登录即可启动应用程序,但功能有限 - 他们可以通过溢出菜单登录).

当用户第一次登录时,我希望将首选项设置为true. 如何在活动中调用此类扩展应用程序?

目前我有一个成功的登录尝试:

myApp.userLogin();
myApp.setUser(UsernameLogin);
// would like to set SharedPreference var to true here?
Run Code Online (Sandbox Code Playgroud)

Chi*_*rag 5

要维护用户登录状态,请首先创建一个名为PreferenceData的类.当用户成功登录然后将一个变量设置为true并在共享首选项中注销然后为false.

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.preference.PreferenceManager;

public class PreferenceData 
{
    static final String PREF_LOGGEDIN_USER_EMAIL = "logged_in_email";
    static final String PREF_USER_LOGGEDIN_STATUS = "logged_in_status";

    public static SharedPreferences getSharedPreferences(Context ctx) 
    {
        return PreferenceManager.getDefaultSharedPreferences(ctx);
    }

    public static void setLoggedInUserEmail(Context ctx, String email) 
    {
        Editor editor = getSharedPreferences(ctx).edit();
        editor.putString(PREF_LOGGEDIN_USER_EMAIL, email);
        editor.commit();
    }

    public static String getLoggedInEmailUser(Context ctx) 
    {
        return getSharedPreferences(ctx).getString(PREF_LOGGEDIN_USER_EMAIL, "");
    }

    public static void setUserLoggedInStatus(Context ctx, boolean status) 
    {
        Editor editor = getSharedPreferences(ctx).edit();
        editor.putBoolean(PREF_USER_LOGGEDIN_STATUS, status);
        editor.commit();
    }

    public static boolean getUserLoggedInStatus(Context ctx) 
    {
        return getSharedPreferences(ctx).getBoolean(PREF_USER_LOGGEDIN_STATUS, false);
    }

    public static void clearLoggedInEmailAddress(Context ctx) 
    {
        Editor editor = getSharedPreferences(ctx).edit();
        editor.remove(PREF_LOGGEDIN_USER_EMAIL);
        editor.remove(PREF_USER_LOGGEDIN_STATUS);
        editor.commit();
    }   
}
Run Code Online (Sandbox Code Playgroud)

现在,您可以在活动中调用其方法,如下所示.

PreferenceData.setUserLoggedInStatus(this,true);   // For set user loggedin status
PreferenceData.getUserLoggedInStatus(this);        // Get User Logged In status . true = login  
Run Code Online (Sandbox Code Playgroud)