我想存储一个时间值,需要检索和编辑它。我如何使用SharedPreferences来做到这一点?


当前回答

在共享首选项中存储值:

SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = preferences.edit();
editor.putString("Name","Harneet");
editor.apply();

从共享首选项中检索值:

SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
String name = preferences.getString("Name", "");
if(!name.equalsIgnoreCase(""))
{
    name = name + "  Sethi";  /* Edit the value here*/
}

其他回答

使用这个简单的库,下面是如何调用SharedPreferences..

TinyDB tinydb = new TinyDB(context);

tinydb.putInt("clickCount", 2);

tinydb.putString("userName", "john");
tinydb.putBoolean("isUserMale", true); 

tinydb.putList("MyUsers", mUsersArray);
tinydb.putImagePNG("DropBox/WorkImages", "MeAtlunch.png", lunchBitmap);

//These plus the corresponding get methods are all Included

SharedPreferences的基本思想是将内容存储在XML文件中。

Declare your xml file path.(if you don't have this file, Android will create it. If you have this file, Android will access it.) SharedPreferences prefs = this.getSharedPreferences("com.example.app", Context.MODE_PRIVATE); Write value to Shared Preferences prefs.edit().putLong("preference_file_key", 1010101).apply(); the preference_file_key is the name of shared preference files. And the 1010101 is the value you need to store. apply() at last is to save the changes. If you get error from apply(), change it to commit(). So this alternative sentence is prefs.edit().putLong("preference_file_key", 1010101).commit(); Read from Shared Preferences SharedPreferences sp = this.getSharedPreferences("com.example.app", Context.MODE_PRIVATE); long lsp = sp.getLong("preference_file_key", -1); lsp will be -1 if preference_file_key has no value. If 'preference_file_key' has a value, it will return the value of this.

整个代码是

    SharedPreferences prefs = this.getSharedPreferences("com.example.app", Context.MODE_PRIVATE);    // Declare xml file
    prefs.edit().putLong("preference_file_key", 1010101).apply();    // Write the value to key.

阅读的代码是

    SharedPreferences sf = this.getSharedPreferences("com.example.app", Context.MODE_PRIVATE);    // Declare xml file
    long lsp = sp.getLong("preference_file_key", -1);    // Read the key and store in lsp

简单和无麻烦::"Android-SharedPreferences-Helper"库

迟到总比不到好:我创建了“Android-SharedPreferences-Helper”库来帮助降低使用SharedPreferences的复杂性和工作量。它还提供了一些扩展功能。它提供的一些东西如下:

一行初始化和设置 轻松选择是使用默认首选项还是自定义首选项文件 每种数据类型的预定义(默认数据类型)和自定义(您可以选择的)默认值 能力设置不同的默认值为单一使用,只是一个额外的参数 你可以注册和取消注册OnSharedPreferenceChangeListener,因为你做的默认类

dependencies {
    ...
    ...
    compile(group: 'com.viralypatel.sharedpreferenceshelper', name: 'library', version: '1.1.0', ext: 'aar')
}

SharedPreferencesHelper对象的声明:(建议在类中 级)

SharedPreferencesHelper sph; 

SharedPreferencesHelper对象的实例化:(建议在 onCreate()方法)

// use one of the following ways to instantiate
sph = new SharedPreferencesHelper(this); //this will use default shared preferences
sph = new SharedPreferencesHelper(this, "myappprefs"); // this will create a named shared preference file
sph = new SharedPreferencesHelper(this, "myappprefs", 0); // this will allow you to specify a mode

将价值观融入到共同的偏好中

非常简单!不像默认的方式(当使用SharedPreferences类时),你不需要调用.edit()和.commit()。

sph.putBoolean("boolKey", true);
sph.putInt("intKey", 123);
sph.putString("stringKey", "string value");
sph.putLong("longKey", 456876451);
sph.putFloat("floatKey", 1.51f);

// putStringSet is supported only for android versions above HONEYCOMB
Set name = new HashSet();
name.add("Viral");
name.add("Patel");
sph.putStringSet("name", name);

就是这样!您的值存储在共享首选项中。

从共享首选项中获取值

同样,这只是一个使用键名的简单方法调用。

sph.getBoolean("boolKey");
sph.getInt("intKey");
sph.getString("stringKey");
sph.getLong("longKey");
sph.getFloat("floatKey");

// getStringSet is supported only for android versions above HONEYCOMB
sph.getStringSet("name");

它还有很多其他扩展功能

在GitHub知识库页面上查看扩展功能的详细信息,使用和安装说明等。

SharedPreferences.Editor editor = getSharedPreferences("identifier", 
MODE_PRIVATE).edit();
//identifier is the unique to fetch data from your SharedPreference.


editor.putInt("keyword", 0); 
// saved value place with 0.
//use this "keyword" to fetch saved value again.
editor.commit();//important line without this line your value is not stored in preference   

// fetch the stored data using ....

SharedPreferences prefs = getSharedPreferences("identifier", MODE_PRIVATE); 
// here both identifier will same

int fetchvalue = prefs.getInt("keyword", 0);
// here keyword will same as used above.
// 0 is default value when you nothing save in preference that time fetch value is 0.

你需要在AdapterClass或任何其他类中使用SharedPreferences。 那一次只是使用这个声明和使用相同的屁股上面。

SharedPreferences.Editor editor = context.getSharedPreferences("idetifier", 
Context.MODE_PRIVATE).edit();
SharedPreferences prefs = context.getSharedPreferences("identifier", Context.MODE_PRIVATE);

//here context is your application context

用于字符串或布尔值

editor.putString("stringkeyword", "your string"); 
editor.putBoolean("booleankeyword","your boolean value");
editor.commit();

取数据与上面相同

String fetchvalue = prefs.getString("keyword", "");
Boolean fetchvalue = prefs.getBoolean("keyword", "");

您可以使用以下方法保存值:

public void savePreferencesForReasonCode(Context context,
    String key, String value) {
    SharedPreferences sharedPreferences = PreferenceManager
    .getDefaultSharedPreferences(context);
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putString(key, value);
    editor.commit();
    }

使用这个方法,你可以从SharedPreferences中获取值:

public String getPreferences(Context context, String prefKey) {
  SharedPreferences sharedPreferences = PreferenceManager
 .getDefaultSharedPreferences(context);
 return sharedPreferences.getString(prefKey, "");
}

这里prefKey是用来保存特定值的键。谢谢。