gpt4 book ai didi

java - SharedPreferences 没有被清除 android

转载 作者:行者123 更新时间:2023-12-01 23:00:03 25 4
gpt4 key购买 nike

我有一个应用程序,用户登录后会保存他的详细信息,以便他下次启动该应用程序时不需要再次登录。我为此目的使用了 SharedPreferences。现在,当我实现注销功能时,我清除首选项并得到一个包含 0 个元素的 Map。我还删除了首选项文件。但是,当另一个用户登录时,他仍然可以看到以前用户的详细信息,而不是他自己的详细信息。我该如何解决这个问题?

这是我的代码:-

session 管理.java

public class SessionManagement extends Application
{
static SharedPreferences pref;

// Editor for Shared preferences
SharedPreferences.Editor editor;

// ContextS
Context _context;

// Shared pref mode
int PRIVATE_MODE = 0;

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

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


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 usersname)
{
// Storing login value as TRUE
editor.putBoolean(IS_LOGIN, true);

editor.putBoolean(KEY_DEVICEREGISTERED, true);
editor.putString(KEY_EMAILID, emailId);

editor.putString(KEY_USERSNAME, usersname);

editor.commit();

// commit changes

}


/**
* 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_USERSNAME, pref.getString(KEY_USERSNAME, 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(this, Login.class);
// Closing all the Activities
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

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

}

}


// 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.remove(KEY_EMAILID);
editor.remove(KEY_USERSNAME);
editor.remove(IS_LOGIN);

editor.clear();
editor.commit();

}

public boolean isLoggedIn()
{
return pref.getBoolean(IS_LOGIN, false);
}
}

登录.java

sessionManager = new SessionManagement(getApplicationContext());

if(sessionManager.isLoggedIn())
{
//Go directly to main activity
HashMap<String, String> userDetails = sessionManager.getUserDetails();

startMyActivity();
finish();
}
else
{
sessionManager.createLoginSession(email, username);
}
public void startMyActivity()
{
// TODO Auto-generated method stub
Intent in = new Intent(getApplicationContext(), Details1.class);

in.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

startActivity(in);
finish();
}

注销.java

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

ClearData cl = new ClearData();
cl.clearApplicationData(getApplicationContext());

Intent i = new Intent(Home.this, Login.class);
// Closing all the Activities
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);

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

// Staring Login Activity
startActivity(i);

ClearData.java

public class ClearData
{
public static void clearApplicationData(Context context)
{
File cache = context.getCacheDir();
File appDir = new File(cache.getParent());
if (appDir.exists()) {
String[] children = appDir.list();
for (String s : children) {
File f = new File(appDir, s);
if(deleteDir(f))
Log.i("TAG", String.format("**************** DELETED -> (%s) *******************",
f.getAbsolutePath()));
}
}
}
private static boolean deleteDir(File dir) {
if (dir != null && dir.isDirectory()) {
String[] children = dir.list();
for (int i = 0; i < children.length; i++) {
boolean success = deleteDir(new File(dir, children[i]));
if (!success) {
return false;
}
}
}
return dir.delete();
}
}

最佳答案

您可以只用空值编辑共享首选项的值,而不是从共享首选项中删除值。

       public void logoutUser()
{
editor.putBoolean(IS_LOGIN, false);
editor.putBoolean(KEY_DEVICEREGISTERED, false);
editor.putString(KEY_EMAILID, null);
editor.putString(KEY_USERSNAME, null);
editor.commit();
}

我认为这会起作用..

关于java - SharedPreferences 没有被清除 android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23508550/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com