SharedPreferences prefs = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE);
String restoredText = prefs.getString("text", null);
if (restoredText != null) {
String name = prefs.getString("name", "No name defined");//"No name defined" is the default value.
int idName = prefs.getInt("idName", 0); //0 is the default value.
}
editor.remove("key_name3"); // will delete key key_name3
editor.remove("key_name4"); // will delete key key_name4
// Save the changes in SharedPreferences
editor.commit(); // commit changes
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.util.Log;
public class Keystore { //Did you remember to vote up my example?
private static Keystore store;
private SharedPreferences SP;
private static String filename="Keys";
private Keystore(Context context) {
SP = context.getApplicationContext().getSharedPreferences(filename,0);
}
public static Keystore getInstance(Context context) {
if (store == null) {
Log.v("Keystore","NEW STORE");
store = new Keystore(context);
}
return store;
}
public void put(String key, String value) {//Log.v("Keystore","PUT "+key+" "+value);
Editor editor = SP.edit();
editor.putString(key, value);
editor.commit(); // Stop everything and do an immediate save!
// editor.apply();//Keep going and save when you are not busy - Available only in APIs 9 and above. This is the preferred way of saving.
}
public String get(String key) {//Log.v("Keystore","GET from "+key);
return SP.getString(key, null);
}
public int getInt(String key) {//Log.v("Keystore","GET INT from "+key);
return SP.getInt(key, 0);
}
public void putInt(String key, int num) {//Log.v("Keystore","PUT INT "+key+" "+String.valueOf(num));
Editor editor = SP.edit();
editor.putInt(key, num);
editor.commit();
}
public void clear(){ // Delete all shared preferences
Editor editor = SP.edit();
editor.clear();
editor.commit();
}
public void remove(){ // Delete only the shared preference that you want
Editor editor = SP.edit();
editor.remove(filename);
editor.commit();
}
}
现在保存该文件并忘记它 . 你完成了它 . 现在回到你的活动中并像这样使用它:
public class YourClass extends Activity{
private Keystore store;//Holds our key pairs
public YourSub(Context context){
store = Keystore.getInstance(context);//Creates or Gets our key pairs. You MUST have access to current context!
int= store.getInt("key name to get int value");
string = store.get("key name to get string value");
store.putInt("key name to store int value",int_var);
store.put("key name to store string value",string_var);
}
}
SharedPreferences mysettings= getSharedPreferences(PREFERENCES_FILE_NAME, 0);
IITAMIYO = mysettings.getBoolean("IITAMIYO", false);
//returns value for the given key.
//second parameter gives the default value if no user preference found
// (set to false in above case)
VOLUME = mysettings.getFloat("VOLUME", 0.5)
//0.5 being the default value if no volume preferences found
// and similarly there are get methods for other data types
0
public class Preferences {
public static final String PREF_NAME = "your preferences name";
@SuppressWarnings("deprecation")
public static final int MODE = Context.MODE_WORLD_WRITEABLE;
public static final String USER_ID = "USER_ID_NEW";
public static final String USER_NAME = "USER_NAME";
public static final String NAME = "NAME";
public static final String EMAIL = "EMAIL";
public static final String PHONE = "PHONE";
public static final String address = "address";
public static void writeBoolean(Context context, String key, boolean value) {
getEditor(context).putBoolean(key, value).commit();
}
public static boolean readBoolean(Context context, String key,
boolean defValue) {
return getPreferences(context).getBoolean(key, defValue);
}
public static void writeInteger(Context context, String key, int value) {
getEditor(context).putInt(key, value).commit();
}
public static int readInteger(Context context, String key, int defValue) {
return getPreferences(context).getInt(key, defValue);
}
public static void writeString(Context context, String key, String value) {
getEditor(context).putString(key, value).commit();
}
public static String readString(Context context, String key, String defValue) {
return getPreferences(context).getString(key, defValue);
}
public static void writeFloat(Context context, String key, float value) {
getEditor(context).putFloat(key, value).commit();
}
public static float readFloat(Context context, String key, float defValue) {
return getPreferences(context).getFloat(key, defValue);
}
public static void writeLong(Context context, String key, long value) {
getEditor(context).putLong(key, value).commit();
}
public static long readLong(Context context, String key, long defValue) {
return getPreferences(context).getLong(key, defValue);
}
public static SharedPreferences getPreferences(Context context) {
return context.getSharedPreferences(PREF_NAME, MODE);
}
public static Editor getEditor(Context context) {
return getPreferences(context).edit();
}
}
// Create object of SharedPreferences.
SharedPreferences sharedPref = getSharedPreferences("mypref", 0);
//now get Editor
SharedPreferences.Editor editor = sharedPref.edit();
//put your value
editor.putString("name", required_Text);
//commits your edits
editor.commit();
// Its used to retrieve data
SharedPreferences sharedPref = getSharedPreferences("mypref", 0);
String name = sharedPref.getString("name", "");
if (name.equalsIgnoreCase("required_Text")) {
Log.v("Matched","Required Text Matched");
} else {
Log.v("Not Matched","Required Text Not Matched");
}
15 回答
在首选项中设置值:
从首选项中检索数据:
更多信息:
Using Shared Preferences
Shared Preferences
Create SharedPreferences
Storing data as KEY/VALUE pair
Get SharedPreferences data
//如果key的值不存在则返回第二个参数值 - 在这种情况下为null
Deleting Key value from SharedPreferences
Clear all data from SharedPreferences
How to Intialize?
How to Store Data In Shared Preference?
要么
并且不要忘记申请:
How to retrieve Data From Shared Preferences ?
希望这会帮助你:)
您可以创建自定义SharedPreference类
您可以获取YourPrefrence实例,如:
我刚刚发现上面的所有例子都太混乱了,所以我写了自己的 . 如果你知道自己在做什么,那么代码碎片就没问题,但是像我这样的人却没有?
想要一个切割粘贴解决方案吗?好吧,这是!
创建一个新的java文件并将其命名为Keystore . 然后粘贴此代码:
现在保存该文件并忘记它 . 你完成了它 . 现在回到你的活动中并像这样使用它:
Shared Preferences 是用于将私有原始数据存储在键值对中的XML文件 . 数据类型包括布尔值,浮点数,整数,长整数和字符串 .
当我们想要保存一些可在整个应用程序中访问的数据时,一种方法是将其保存在全局变量中 . 但是一旦申请被关闭,它就会消失 . 另一种推荐的方法是保存在
SharedPreference
中 . 保存在SharedPreferences文件中的数据可在整个应用程序中访问,并且即使在应用程序关闭或重新启动后仍会保留 .SharedPreferences 将数据保存在键值对中,并且可以以相同的方式访问 .
您可以使用两种方法创建 SharedPreferences 的Object,
1) . getSharedPreferences() :使用此方法,您可以创建多个SharedPreferences.and它的第一个参数,名称为
SharedPreferences
.2) . getPreferences() :使用此方法可以创建单个
SharedPreferences
.Storing Data
添加变量声明/创建首选项文件
检索文件名的句柄(使用getSharedPreferences)
Open Editor and Add key-value pairs
不要忘记使用
myeditor.apply()
进行应用/保存,如上所示 .Retrieving Data
****使用首选项写入值: - ****
****使用首选项读取值: - ****
创建
SharedPreference
的最佳方法,对于全局使用,您需要创建如下所示的类:Write to Shared Preferences
Read from Shared Preferences
你也可以看一下为此而写的我过去的sample project . 我在本地保存一个名称,并根据用户的请求或应用程序启动时检索它 .
但是,此时,最好使用
commit
(而不是apply
)来保存数据 . 更多信息here .共享首选项很容易学习,所以看看这个关于sharedpreference的简单教程