1

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

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

}

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

当用户第一次登录时,我想让偏好设置为 true。 如何从活动中调用此类扩展应用程序?

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

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

2 回答 2

6

维护用户登录状态首先创建一个名为 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();
    }   
}

现在在活动中,您可以调用它的方法,如下所示。

PreferenceData.setUserLoggedInStatus(this,true);   // For set user loggedin status
PreferenceData.getUserLoggedInStatus(this);        // Get User Logged In status . true = login  
于 2012-08-10T02:24:57.240 回答
2

要使用 sharedpreference 维护用户登录状态,您必须为用户登录会话创建一个名为 SessionManager 的类。当用户成功登录时,将布尔变量设置为 true,当用户注销时,在共享首选项中设置布尔变量 false。类将如下所示:

import java.util.HashMap;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

public class SessionManager {
    // Shared Preferences
    SharedPreferences pref;

    // Editor for Shared preferences
    Editor editor;

    // Context
    Context _context;

    // Shared pref mode
    int PRIVATE_MODE = 0;

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

    // 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 = "IsLogin";

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

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

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

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

        // Storing email in pref
       // editor.putString(KEY_SITEID, siteid);

        // commit changes
        editor.commit();
    }   

    /**
     * Check login method wil check user login status
     * If false it will redirect user to login page
     * Else won't do anything
     * */
    public void checkLogin(){
        // Check login status
        if(!this.isLoggedIn()){
            // user is not logged in redirect him to Login Activity
            Intent i = new Intent(_context, LoginForm.class);
            // Closing all the Activities
            i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

            // Add new Flag to start new Activity
            i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

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

    }

    /**
     * Get stored session data
     * */
    public HashMap<String, String> getUserDetails(){
        HashMap<String, String> user = new HashMap<String, String>();
        // user name
        user.put(KEY_NAME, pref.getString(KEY_NAME, null));

        // user email id
       // user.put(KEY_EMAIL, pref.getString(KEY_EMAIL, null));

        // return user
        return user;
    }

    /**
     * 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, LoginForm.class);
        // Closing all the Activities
        i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

        // 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);
    }
}
于 2012-11-21T12:53:39.933 回答