Andrfoid SharedPreferences 偏好设置 保存list集合对象 commit和apply区别



1.字符的数组和数字的数组
2.设置第一次没有进入时候的值===默认值
3.数组中的直接翻译和…………entry和entryvalues代表的值


1.在xml中碰到转义字符
2.碰到int类型的数据

应用
1.天气预报
2.语音
3.行车记录仪
4.canbus

1.有界面和无界面的偏好设置( 语音 )


2.传递不同的数据类型( 天气预报 )
boolean
int
String

3.怎么提高效率( 天气预报 )
Commit用一次


SharedPreferences 的写数据和读数据第一步都是先获得对象
SharedPreferences mySharedPreferences = getSharedPreferences("test",
                Activity. MODE_PRIVATE );  


在Android系统中,这些信息以XML文件的形式保存在 /data/data/PACKAGE_NAME /shared_prefs 目录下。

不能保存复杂类型的数据,只能存储一些基本类型数据

一个SharedPreferences,顾名思义,这个Preferences是共享的,共享的范围据现在同一个Package中
所以可以New多个Prefences类。

可以写一个工具类,进行赋值,存储值,和Commit=====写在一个方法里面

SharedPreferences userInfo = getSharedPreferences("user_info", 0);  
userInfo.edit().putString("name", user.getText().toString()).commit();  
String pass = userInfo.getString("pass", "");  ============================?????


问题:如何保存Boolean类型的数据


使用SharedPreferences保存key-value对的步骤如下:
  (1)使用Activity类的getSharedPreferences方法获得SharedPreferences对象,其中存储key-value的文件的名称由getSharedPreferences方法的第一个参数指定。
  (2)使用SharedPreferences接口的edit获得SharedPreferences.Editor对象。
  (3)通过SharedPreferences.Editor接口的putXxx方法保存key-value对。其中Xxx表示不同的数据类型。例如:字符串类型的value需要用putString方法。
  (4)通过SharedPreferences.Editor接口的commit方法保存key-value对。commit方法相当于数据库事务中的提交(commit)操作。


具体代码的书写流程为:
 
A、存放数据信息
1、打开Preferences,名称为setting,如果存在则打开它,否则创建新的Preferences
SharedPreferences settings = getSharedPreferences(“setting”, 0);
2、让setting处于编辑状态
SharedPreferences.Editor editor = settings.edit();
3、存放数据
editor.putString(“name”,”ATAAW”);
editor.putString(“URL”,”ATAAW.COM”);
4、完成提交
editor.commit();
B、读取数据信息
1、获取Preferences
SharedPreferences settings = getSharedPreferences(“setting”, 0);
2、取出数据
String name = settings.getString(“name”,”默认值”);
String url = setting.getString(“URL”,”default”);
以上就是Android中SharedPreferences的使用方法,其中创建的Preferences文件存放位置可以在Eclipse中查看:
DDMS->File Explorer /<package name>/shared_prefs/setting.xml




修改SharedPreferences后两种提交方式有什么区别?

commit的区别:
commit这种方式很常用,在比较早的SDK版本中就有了,这种提交修改的方式是同步的,会阻塞调用它的线程,并且这个方法会返回boolean值告知保存是否成功(如果不成功,可以做一些补救措施)。
apply 异步的提交方式 ,目前Android Studio也会提示大家使用这种方式。



SharedPreferences还提供一个 监听接口可以监听 SharedPreferences的键值变化,需要监控键值变化的可以用registerOnSharedPreferenceChangeListener添加监听器。


多进程操作和读取SharedPreferences的问题


在SDK 3.0及以上版本,可以通过Context.MODE_MULTI_PROCESS属性来实现SharedPreferences多进程共享。如下设置:

public static SharedPreferences getSharedPreferences(String name) { if (null != context)
 { if (Build.VERSION.SDK_INT >= 11) { return context.getSharedPreferences(name, Context.MODE_MULTI_PROCESS); } 
else { return context.getSharedPreferences(name, Context.MODE_PRIVATE); } } return null; }




方法的第二个参数指定文件的操作模式,共有四种操作模式,这四种模式前面介绍使用文件方式保存数据时已经讲解过。如果希望SharedPreferences背后使用的xml文件能被其他应用读和写,可以指定Context.MODE_WORLD_READABLE和Context.MODE_WORLD_WRITEABLE权限。




解决办法( 只是Google不建议大家这么干了 ):
与传统的本进程中使用的shareprefence不同的地方在于,我们使用了MODE_WORLD_READABLE(传统本进程使用,一般使用MODE_PRIVATE):

/**
 * 采用SharedPreferences存放配置<br/>
 * 多进程访问
 */
@SuppressLint("InlinedApi")
public final class CXPreferencesHelper {
    /** 配置文件名称 */
    public final static String SPName = "huanji";

    /** 极光推送id */
    public final static String SP_KEY_JPush_ID = "registrationID";

    /** 卡片位积分 */
    public final static String SP_KEY_CARD_ALL_JINGYAN = "jingyanall";

    /** 松鼠整理所有卡片位积分 */
    public final static String SP_KEY_CARD_INTEGRAL = "integral";

    /** 松鼠整理首页统计所有节省的空间总量*/
    public final static String SP_KEY_SAVE_SPACE= "savspace";


    /**用户登录的身份信息。为120位长的字符串,当前的用户会话令牌*/
    public static final String SP_USER_TOKEN = "user_token";

    public static void saveBoolean(Context context, String key, Boolean value) {
        SharedPreferences sp = context.getSharedPreferences(SPName, Context.MODE_MULTI_PROCESS);
        SharedPreferences.Editor editor = sp.edit();
        editor.putBoolean(key, value);
        apply(editor);
    }

    public static Boolean readBoolean(Context context, String key, Boolean defValue) {
        SharedPreferences sp = context.getSharedPreferences(SPName, Context.MODE_MULTI_PROCESS);
        return sp.getBoolean(key, defValue);
    }

    public static void saveInt(Context context, String key, int value) {
        SharedPreferences sp = context.getSharedPreferences(SPName, Context.MODE_MULTI_PROCESS);
        SharedPreferences.Editor editor = sp.edit();
        editor.putInt(key, value);
        apply(editor);
    }

    public static int readInt(Context context, String key, int defValue) {
        SharedPreferences sp = context.getSharedPreferences(SPName, Context.MODE_MULTI_PROCESS);
        return sp.getInt(key, defValue);
    }

    public static void saveLong(Context context, String key, long value) {
        SharedPreferences sp = context.getSharedPreferences(SPName, Context.MODE_MULTI_PROCESS);
        SharedPreferences.Editor editor = sp.edit();
        editor.putLong(key, value);
        apply(editor);
    }

    public static float readFloat(Context context, String key, float defValue) {
        SharedPreferences sp = context.getSharedPreferences(SPName, Context.MODE_MULTI_PROCESS);
        return sp.getFloat(key, defValue);
    }


    public static void saveFloat(Context context, String key, float value) {
        SharedPreferences sp = context.getSharedPreferences(SPName, Context.MODE_MULTI_PROCESS);
        SharedPreferences.Editor editor = sp.edit();
        editor.putFloat(key, value);
        apply(editor);
    }

    public static long readLong(Context context, String key, long defValue) {
        SharedPreferences sp = context.getSharedPreferences(SPName, Context.MODE_MULTI_PROCESS);
        return sp.getLong(key, defValue);
    }

    public static void saveString(Context context, String key, String value) {
        SharedPreferences sp = context.getSharedPreferences(SPName, Context.MODE_MULTI_PROCESS);
        SharedPreferences.Editor editor = sp.edit();
        editor.putString(key, value);
        apply(editor);
    }

    public static String readString(Context context, String key, String defValue) {
        SharedPreferences sp = context.getSharedPreferences(SPName, Context.MODE_MULTI_PROCESS);
        return sp.getString(key, defValue);
    }
    public static void apply(SharedPreferences.Editor editor){
        if (editor == null){
            return;
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
            editor.apply();
        }else {
            editor.commit();
        }
    }



1.字符的数组和数字的数组
2.设置第一次没有进入时候的值===默认值
3.数组中的直接翻译和…………entry和entryvalues代表的值


1.在xml中碰到转义字符
2.碰到int类型的数据

应用
1.天气预报
2.语音
3.行车记录仪
4.canbus

1.有界面和无界面的偏好设置( 语音 )


2.传递不同的数据类型( 天气预报 )
boolean
int
String

3.怎么提高效率( 天气预报 )
Commit用一次


SharedPreferences 的写数据和读数据第一步都是先获得对象
SharedPreferences mySharedPreferences = getSharedPreferences("test",
                Activity. MODE_PRIVATE );  


在Android系统中,这些信息以XML文件的形式保存在 /data/data/PACKAGE_NAME /shared_prefs 目录下。

不能保存复杂类型的数据,只能存储一些基本类型数据

一个SharedPreferences,顾名思义,这个Preferences是共享的,共享的范围据现在同一个Package中
所以可以New多个Prefences类。

可以写一个工具类,进行赋值,存储值,和Commit=====写在一个方法里面

SharedPreferences userInfo = getSharedPreferences("user_info", 0);  
userInfo.edit().putString("name", user.getText().toString()).commit();  
String pass = userInfo.getString("pass", "");  ============================?????


问题:如何保存Boolean类型的数据


使用SharedPreferences保存key-value对的步骤如下:
  (1)使用Activity类的getSharedPreferences方法获得SharedPreferences对象,其中存储key-value的文件的名称由getSharedPreferences方法的第一个参数指定。
  (2)使用SharedPreferences接口的edit获得SharedPreferences.Editor对象。
  (3)通过SharedPreferences.Editor接口的putXxx方法保存key-value对。其中Xxx表示不同的数据类型。例如:字符串类型的value需要用putString方法。
  (4)通过SharedPreferences.Editor接口的commit方法保存key-value对。commit方法相当于数据库事务中的提交(commit)操作。


具体代码的书写流程为:
 
A、存放数据信息
1、打开Preferences,名称为setting,如果存在则打开它,否则创建新的Preferences
SharedPreferences settings = getSharedPreferences(“setting”, 0);
2、让setting处于编辑状态
SharedPreferences.Editor editor = settings.edit();
3、存放数据
editor.putString(“name”,”ATAAW”);
editor.putString(“URL”,”ATAAW.COM”);
4、完成提交
editor.commit();
B、读取数据信息
1、获取Preferences
SharedPreferences settings = getSharedPreferences(“setting”, 0);
2、取出数据
String name = settings.getString(“name”,”默认值”);
String url = setting.getString(“URL”,”default”);
以上就是Android中SharedPreferences的使用方法,其中创建的Preferences文件存放位置可以在Eclipse中查看:
DDMS->File Explorer /<package name>/shared_prefs/setting.xml




修改SharedPreferences后两种提交方式有什么区别?

commit的区别:
commit这种方式很常用,在比较早的SDK版本中就有了,这种提交修改的方式是同步的,会阻塞调用它的线程,并且这个方法会返回boolean值告知保存是否成功(如果不成功,可以做一些补救措施)。
apply 异步的提交方式 ,目前Android Studio也会提示大家使用这种方式。



SharedPreferences还提供一个 监听接口可以监听 SharedPreferences的键值变化,需要监控键值变化的可以用registerOnSharedPreferenceChangeListener添加监听器。


多进程操作和读取SharedPreferences的问题


在SDK 3.0及以上版本,可以通过Context.MODE_MULTI_PROCESS属性来实现SharedPreferences多进程共享。如下设置:

public static SharedPreferences getSharedPreferences(String name) { if (null != context)
 { if (Build.VERSION.SDK_INT >= 11) { return context.getSharedPreferences(name, Context.MODE_MULTI_PROCESS); } 
else { return context.getSharedPreferences(name, Context.MODE_PRIVATE); } } return null; }




方法的第二个参数指定文件的操作模式,共有四种操作模式,这四种模式前面介绍使用文件方式保存数据时已经讲解过。如果希望SharedPreferences背后使用的xml文件能被其他应用读和写,可以指定Context.MODE_WORLD_READABLE和Context.MODE_WORLD_WRITEABLE权限。




解决办法( 只是Google不建议大家这么干了 ):
与传统的本进程中使用的shareprefence不同的地方在于,我们使用了MODE_WORLD_READABLE(传统本进程使用,一般使用MODE_PRIVATE):

/**
 * 采用SharedPreferences存放配置<br/>
 * 多进程访问
 */
@SuppressLint("InlinedApi")
public final class CXPreferencesHelper {
    /** 配置文件名称 */
    public final static String SPName = "huanji";

    /** 极光推送id */
    public final static String SP_KEY_JPush_ID = "registrationID";

    /** 卡片位积分 */
    public final static String SP_KEY_CARD_ALL_JINGYAN = "jingyanall";

    /** 松鼠整理所有卡片位积分 */
    public final static String SP_KEY_CARD_INTEGRAL = "integral";

    /** 松鼠整理首页统计所有节省的空间总量*/
    public final static String SP_KEY_SAVE_SPACE= "savspace";


    /**用户登录的身份信息。为120位长的字符串,当前的用户会话令牌*/
    public static final String SP_USER_TOKEN = "user_token";

    public static void saveBoolean(Context context, String key, Boolean value) {
        SharedPreferences sp = context.getSharedPreferences(SPName, Context.MODE_MULTI_PROCESS);
        SharedPreferences.Editor editor = sp.edit();
        editor.putBoolean(key, value);
        apply(editor);
    }

    public static Boolean readBoolean(Context context, String key, Boolean defValue) {
        SharedPreferences sp = context.getSharedPreferences(SPName, Context.MODE_MULTI_PROCESS);
        return sp.getBoolean(key, defValue);
    }

    public static void saveInt(Context context, String key, int value) {
        SharedPreferences sp = context.getSharedPreferences(SPName, Context.MODE_MULTI_PROCESS);
        SharedPreferences.Editor editor = sp.edit();
        editor.putInt(key, value);
        apply(editor);
    }

    public static int readInt(Context context, String key, int defValue) {
        SharedPreferences sp = context.getSharedPreferences(SPName, Context.MODE_MULTI_PROCESS);
        return sp.getInt(key, defValue);
    }

    public static void saveLong(Context context, String key, long value) {
        SharedPreferences sp = context.getSharedPreferences(SPName, Context.MODE_MULTI_PROCESS);
        SharedPreferences.Editor editor = sp.edit();
        editor.putLong(key, value);
        apply(editor);
    }

    public static float readFloat(Context context, String key, float defValue) {
        SharedPreferences sp = context.getSharedPreferences(SPName, Context.MODE_MULTI_PROCESS);
        return sp.getFloat(key, defValue);
    }


    public static void saveFloat(Context context, String key, float value) {
        SharedPreferences sp = context.getSharedPreferences(SPName, Context.MODE_MULTI_PROCESS);
        SharedPreferences.Editor editor = sp.edit();
        editor.putFloat(key, value);
        apply(editor);
    }

    public static long readLong(Context context, String key, long defValue) {
        SharedPreferences sp = context.getSharedPreferences(SPName, Context.MODE_MULTI_PROCESS);
        return sp.getLong(key, defValue);
    }

    public static void saveString(Context context, String key, String value) {
        SharedPreferences sp = context.getSharedPreferences(SPName, Context.MODE_MULTI_PROCESS);
        SharedPreferences.Editor editor = sp.edit();
        editor.putString(key, value);
        apply(editor);
    }

    public static String readString(Context context, String key, String defValue) {
        SharedPreferences sp = context.getSharedPreferences(SPName, Context.MODE_MULTI_PROCESS);
        return sp.getString(key, defValue);
    }
    public static void apply(SharedPreferences.Editor editor){
        if (editor == null){
            return;
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
            editor.apply();
        }else {
            editor.commit();
        }
    }


猜你喜欢

转载自blog.csdn.net/WHB20081815/article/details/77337725