I create a sample application where I used shared preferences like this:
SharedPreferences sharedPref = getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString("data", "This is my data...");
editor.apply();
And reading the data like this:
SharedPreferences sharedPref = getPreferences(Context.MODE_PRIVATE);
String base_data = sharedPref.getString("data", "");
This code worked in the sample application but when I copied and pasted the sample application project files to another project in android studio, SharedPreferences does not seem to work. I have tried everything but it does not work. Please save me from going insane...
In the "duplicate" project base_data variable just returns the default value ("").
public static final String PREFS_GAME ="PLAY";
public static final String GAME_SCORE= "GameScore";
//======== Code to save data ===================
SharedPreferences sp = getApplicationContext.getSharedPreferences(PREFS_GAME ,Context.MODE_PRIVATE);
sp.edit().putString(GAME_SCORE,"100").commit();
//========= Code to get saved/ retrieve data ==============
SharedPreferences sp = getApplicationContext.getSharedPreferences(PREFS_GAME ,Context.MODE_PRIVATE);
String sc = sp.getString(GAME_SCORE,"");
You need provide a name to identify the preference file
private static final Object MY_PREFS_NAME = "MY_PREFS";
SharedPreferences.Editor editor = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE).edit();
editor.putString("data","This is my data...");
editor.apply();
Get data from preference:
SharedPreferences prefs = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE);
String data = prefs.getString("data", "No data");
Or try this class
public class PrefManager {
SharedPreferences pref;
SharedPreferences.Editor editor;
Context _context;
// shared pref mode
int PRIVATE_MODE = 0;
// Shared preferences file name
private static final String PREF_NAME = "your_app_name";
private static final String IS_FIRST_TIME_LAUNCH = "IsFirstTimeLaunch";
public PrefManager(Context context) {
this._context = context;
pref = _context.getSharedPreferences(PREF_NAME, PRIVATE_MODE);
editor = pref.edit();
}
public void setBoolean(String PREF_NAME,Boolean val) {
editor.putBoolean(PREF_NAME, val);
editor.commit();
}
public void setString(String PREF_NAME,String VAL) {
editor.putString(PREF_NAME, VAL);
editor.commit();
}
public void setInt(String PREF_NAME,int VAL) {
editor.putInt(PREF_NAME, VAL);
editor.commit();
}
public boolean getBoolean(String PREF_NAME) {
return pref.getBoolean(PREF_NAME,true);
}
public void remove(String PREF_NAME){
if(pref.contains(PREF_NAME)){
editor.remove(PREF_NAME);
editor.commit();
}
}
public String getString(String PREF_NAME) {
if(pref.contains(PREF_NAME)){
return pref.getString(PREF_NAME,null);
}
return "";
}
public int getInt(String key) {
return pref.getInt(key,0);
}
}
then
PrefManager prefManager = new PrefManager(context);
prefManager.setString("data","mydata");
String data = prefManager.getString("data");
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.