gpt4 book ai didi

Android:更新 SharedPreferences

转载 作者:行者123 更新时间:2023-11-29 16:01:44 33 4
gpt4 key购买 nike

我正在尝试记录我的应用程序的启动时间。为此,我需要将信息存储在设备中,即使应用程序已被终止。

我阅读了这个链接:

http://developer.android.com/guide/topics/data/data-storage.html#pref

我认为实现这一点的最简单方法是使用 SharedPreferences。所以我写了这段代码:

final String PREFS_NAME = "MyPrefsFile";


SharedPreferences settings = getApplicationContext().getSharedPreferences(PREFS_NAME, 0);
int TimesLaunched = settings.getInt("TimesLaunched", 0);

TimesLaunched++;

SharedPreferences.Editor editor = settings.edit();
editor.putInt("TimesLaunched", TimesLaunched);


editor.commit();

我第一次发现它有效,所以它从 0 变到 1,仅此而已。以下几次我启动该应用程序时没有任何反应。

谁知道哪里出了问题?

最佳答案

我在这里发布了一个我写的类,用于在他登录我的应用程序时保存用户凭据,按照您想要的方式修改它

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 = "TGCPrefs";

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

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

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

// Storing name in pref
editor.putString(KEY_USERNAME, email);

// Storing email in pref
editor.putString(KEY_Password, password);

editor.putBoolean("FB", facebookCall);
// 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 boolean checkLogin() {
// Check login status
if (!this.isLoggedIn()) {
/* // user is not logged in redirect him to Login Activity
Intent i = new Intent(_context, LoginActivity.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);*/
return false;
} else if (this.isLoggedIn()) {
return true;
}
return false;
}


public boolean isFacebookLoggedId(){
return pref.getBoolean("FB", false);
}

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

// user email id
user.put(KEY_Password, pref.getString(KEY_Password, 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, LoginActivity.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);
}
}

您需要做的就是修改它并调用 get prefs last counter 并在您的初始屏幕上将其递增 1(如果这是您的工作方式或您想要使用的任何策略)

关于Android:更新 SharedPreferences,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24027429/

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