我创建了一个具有登录功能的应用。当用户第一次登录时,他的凭据将使用SharedPreferences保存。他下次登录时会直接重定向到第二个屏幕。我实现了注销功能。我清除了首选项,但是当我尝试从其他帐户登录时,我仍然可以获得之前帐户的详细信息。
这是我的代码: -
Main.java
private SessionManagement sessionManager;
sessionManager.createLoginSession(username, deviceUrl, deviceId, endPointHost, deviceName, name , encodedAccountNameToken, hostUrlToken);
SessionManagement.java
public class SessionManagement
{
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 = "UserDetails";
// 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_EMAILID = "email";
// Email address (make variable public to access from outside)
public static final String KEY_DEVICEURL = "deviceurl";
public static final String KEY_ENDPOINTHOST = "endpointhost";
public static final String KEY_DEVICENAME = "devicename";
public static final String KEY_USERSNAME = "usersname";
public static final String KEY_ENCODEDACCOUNTNAME = "encodedaccountname";
public static final String KEY_HOSTURL = "hosturl";
public static final String KEY_DEVICEiD = "deviceid";
public static final String KEY_DEVICEREGISTERED = "deviceregistered";
// Constructor
public SessionManagement(Context context)
{
this._context = context;
pref = _context.getSharedPreferences(PREF_NAME, PRIVATE_MODE);
editor = pref.edit();
}
public void createLoginSession(String emailId, String deviceauthurl, String deviceid, String endpointhost, String devicename, String usersname, String encodedaccountname, String hosturl)
{
// Storing login value as TRUE
editor.putBoolean(IS_LOGIN, true);
editor.putBoolean(KEY_DEVICEREGISTERED, true);
editor.putString(KEY_EMAILID, emailId);
editor.putString(KEY_DEVICEURL, deviceurl);
editor.putString(KEY_DEVICEiD, deviceid);
editor.putString(KEY_ENDPOINTHOST, endpointhost);
editor.putString(KEY_DEVICENAME,devicename);
editor.putString(KEY_USERSNAME, usersname);
editor.putString(KEY_ENCODEDACCOUNTNAME, encodedaccountname);
editor.putString(KEY_HOSTURL, hosturl);
// commit changes
editor.commit();
}
/**
* Get stored session data
* */
public HashMap<String, String> getUserDetails()
{
HashMap<String, String> user = new HashMap<String, String>();
user.put(KEY_EMAILID, pref.getString(KEY_EMAILID, null));
user.put(KEY_DEVICEAUTHURL, pref.getString(KEY_DEVICEAUTHURL, null));
user.put(KEY_DEVICEiD, pref.getString(KEY_DEVICEiD, null));
user.put(KEY_ENDPOINTHOST, pref.getString(KEY_ENDPOINTHOST, null));
user.put(KEY_DEVICENAME, pref.getString(KEY_DEVICENAME, null));
user.put(KEY_USERSNAME, pref.getString(KEY_USERSNAME, null));
user.put(KEY_ENCODEDACCOUNTNAME, pref.getString(KEY_ENCODEDACCOUNTNAME, null));
user.put(KEY_HOSTURL, pref.getString(KEY_HOSTURL, null));
// return user
return user;
}
/**
* 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, Login.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);
}
}
// This function clears all session data and redirect the user to LoginActivity
/**
* Clear session details
* */
public void logoutUser()
{
// Clearing all data from Shared Preferences
//editor.clear();
editor.remove(KEY_DEVICEURL);
editor.remove(KEY_DEVICENAME);
editor.remove(KEY_DEVICEREGISTERED);
editor.remove(KEY_DEVICEiD);
editor.remove(KEY_EMAILID);
editor.remove(KEY_ENCODEDACCOUNTNAME);
editor.remove(KEY_ENDPOINTHOST);
editor.remove(KEY_HOSTURL);
editor.remove(KEY_USERSNAME);
editor.remove(IS_LOGIN);
editor.remove(PREF_NAME);
editor.clear();
editor.commit();
// After logout redirect user to Loing Activity
Intent i = new Intent(_context, Login.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);
}
public boolean isLoggedIn()
{
return pref.getBoolean(IS_LOGIN, false);
}
}
可能出现什么问题?
答案 0 :(得分:0)
您的主要活动不包含完整来源。请确保您从MainActivity调用logoutUser方法
在主要活动中的ondestroy方法中调用logout函数。
答案 1 :(得分:0)
在您注销时调用main.class中的session.logoutUser()
Intent z = new Intent(this,main.class);
z.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
Intent.FLAG_ACTIVITY_CLEAR_TASK |
Intent.FLAG_ACTIVITY_NEW_TASK);
SharedPreferences prefs = getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putString("EMP_ID"," ");
editor.commit();
session.logoutUser();
startActivity(z);
this.finish();
答案 2 :(得分:0)
使用Context初始化SessionManagement。
public SessionManagement(Context context)
{
this._context = context;
pref = _context.getSharedPreferences(PREF_NAME, PRIVATE_MODE);
editor = pref.edit();
}
这&#34; context&#34;不可用。