恢复Shared Preference

mac2022-06-30  62

访问保存的Shared Preference同样还是用getSharedPreferences方法。传入你要访问的Shared Preference的名字,然后使用类型安全的get<type>方法来提取保存的值。

 

每一个get<type>方法要带一个键值和默认值(当键值没有可获得的值时使用),如下面的框架代码所示:

 

public void loadPreferences()

{

// Get the stored preferences

int mode = Activity.MODE_PRIVATE;

SharedPreferences mySharedPreferences = getSharedPreferences(MYPREFS, mode);

 

// Retrieve the saved values.

boolean isTrue = mySharedPreferences.getBoolean(“isTrue”, false);

float lastFloat = mySharedPreferences.getFloat(“lastFloat”, 0f);

int wholeNumber = mySharedPreferences.getInt(“wholeNumber”, 1);

long aNumber = mySharedPreferences.getLong(“aNumber”, 0);

String stringPreference;

stringPreference = mySharedPreferences.getString(“textEntryValue”,“”);

}

转载于:https://www.cnblogs.com/xirihanlin/archive/2009/08/05/1539330.html

相关资源:JAVA上百实例源码以及开源项目
最新回复(0)