android 中共享变量SharedPreferences的使用

来源:互联网 发布:关闭windows数字签名 编辑:程序博客网 时间:2024/05/30 21:59

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;    }
0 0
原创粉丝点击