您的位置:首页 > 移动开发 > Android开发

android 中共享变量SharedPreferences的使用

2015-07-21 16:37 519 查看
SharedPreferences 的使用具体请参考API:

Interface for accessing and modifying preference data returned by getSharedPreferences(String, int). For any particular set of preferences, there is a single instance of this class that all clients share. Modifications to the preferences must go through an SharedPreferences.Editor object to ensure the preference values remain in a consistent state and control when they are committed to storage. Objects that are returned from the various get methods must be treated as immutable by the application.

Note: currently this class does not support use across multiple processes. This will be added later.

使用如下:

public boolean saveData(String userName,String password)
{
boolean flag = false;

//      1.获取共享参数
SharedPreferences sharedPreferences = context.getSharedPreferences("qjg", context.MODE_WORLD_READABLE+context.MODE_WORLD_WRITEABLE);

//      2.通过共享参数获取编辑器对象
Editor editor = sharedPreferences.edit();

//      3.通过编辑器对象放入相应的值
editor.putString("name",userName);
editor.putString("password",password);
//      4.提交数据 ,必须使用。否则,写不到文件中!
flag = editor.commit();

return flag;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: