尝试使用Android SharedPreferences维护登录状态 [英] Trying to maintain Login state with Android SharedPreferences

查看:258
本文介绍了尝试使用Android SharedPreferences维护登录状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a class that extends application:

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

}

我觉得我只是简单地使用一个true/false变量来指示用户是否需要在每次启动该应用程序时登录.当然,我还将用户名也存储在SharedPreference中,但是出于这个问题的缘故,我仅讨论一种以使其简单. (请记住,用户无需登录即可启动应用程序,但功能有限-他们可能会通过溢出菜单登录.)

I feel I am halfway there to simply having a true/false variable to indication whether user needs to log in each time the start the app. Of course, I will store username as well in SharedPreference, but for the sake of this question, I am only discussing one to keep it simple. (Keep in mind, user can start the app w/o login, but function is limited -- they may login through overflow menu).

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

When the user first logins, I want to get the preference to true. How do I call this class extending application from within activity?

当前,我有此登录成功的尝试:

Currently I have this for a successful Login attempt:

myApp.userLogin();
myApp.setUser(UsernameLogin);
// would like to set SharedPreference var to true here?

推荐答案

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

To Maintain User Login State first Make one Class Called PreferenceData. When User Logged in Successfully then set one variable true and logged out then false in shared preference.

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();
    }   
}

现在,您可以像下面这样调用其方法.

Now in activity you can call its method like below.

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

这篇关于尝试使用Android SharedPreferences维护登录状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆