Android 主要概念总结归纳
2010-02-23 15:54:00 来源:WEB开发网下面一一介绍Preferences的使用
Preferences:一个轻量级机制,存储和获得原始数据类型的关键词。这是典型的用来存储数据的首选项。
你能享受存储应用程序优先级,就像一个默认的招呼或者是无论何时开始运行应用程序时的文本字体。调用 Context.getSharePreferences()读和写数据值。为你所设置的首先项分配一个名称,如果你想将它们分享给同一个包中的其他组件的话,或者是使用Activity.getPreferences()去使它们作为私有使用,这时此函数不需要任何参数。
下面是一个例子:
public class Calc extends Activity {
public static final String PREFS_NAME = "MyPrefsFile";
...
@Override
protected void onCreate(Bundle state){
super.onCreate(state);
...
// Restore preferences
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
/*
public abstract SharedPreferences getSharedPreferences(String name, int mode)
Retrieve and hold the contents of the preferences file 'name', returning a SharedPreferences through which you can retrieve and modify its values. Only one instance of the SharedPreferences object is returned to any callers for the same name, meaning they will see each other's edits as soon as they are made.
Parameters
name Desired preferences file. If a preferences file by this name does not exist, it will be created when you retrieve an editor (SharedPreferences.edit()) and then commit changes (Editor.commit()).
mode Operating mode. Use 0 or MODE_PRIVATE for the default operation, MODE_WORLD_READABLE and MODE_WORLD_WRITEABLE to control permissions.
Returns
* Returns the single SharedPreferences instance that can be used to retrieve and modify the preference values.
*/
boolean silent = settings.getBoolean("silentMode", false);
setSilent(silent);
}
@Override
protected void onStop(){
更多精彩
赞助商链接