Android API demos 阅读笔记 4

PersistentState (简单的数据存储:SharedPreferences)

 

SharedPreferences 只能支持 String,int, float.... 等基本类型。

关键代码如下:

存:

        SharedPreferences.Editor editor = getPreferences(0).edit();
        editor.putString("text", mSaved.getText().toString());
        editor.putInt("selection-start", mSaved.getSelectionStart());
        editor.putInt("selection-end", mSaved.getSelectionEnd());
        editor.commit();

 

注: getPreferences(0) 中的 参数是工作模式,0 代表MODE_PRIVATE,the default mode, where the created file can only be accessed by the calling application (or all applications sharing the same user ID).

 

 对应的还有:

MODE_WORLD_READABLE
MODE_WORLD_WRITEABLE。

 

这些选项在写文件的时候也有涉及过。

 

读:

SharedPreferences prefs = getPreferences(0);

 String restoredText = prefs.getString("text", null);

int selectionStart = prefs.getInt("selection-start", -1);
 int selectionEnd = prefs.getInt("selection-end", -1);

 

 

 

 

你可能感兴趣的:(android,api,String,File,null,application)