在 SoundSettings 里添加 tr69 CheckBoxPreference

1. 在 sound_settings.xml里添加

    <CheckBoxPreference     
            android:key="tr69"
            android:title="@string/tr69"
            android:persistent="false"

            android:defaultValue="true"/>

2. 在string.xml中添加

    <string name="tr69">TR69</string>


3.在SoundSettings.java中添加

    private static final String KEY_TR69 = "tr69";
    private CheckBoxPreference mTr69;

    在onCreate(Bundle  savedInstanceState)中,

    添加

    mTr69 = (CheckBoxPreference) findPreference(KEY_TR69);
    mTr69.setPersistent(false);

    注: public void setPersistent (boolean persistent)

           Sets whether this Preference is persistent. When persistent, it stores its value(s) into the persistent SharedPreferences storage.

           SharedPreference

           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.

    此时, 编译执行./partChange

此时, 打开虚拟机后, 由于defaultValue = "true", 所以是打勾的。

点击tr69, 使其不打勾。 关机, 重新打开虚拟机后, 仍然是打勾的, 此时证明没有被保存。

    (1) 此时, sound_settings.xml中,

            android:persistant = "false";

            修改SoundSettings.java

            mTr69.setPersistant(true);

            重新编译, 使其不打勾。 关机, 重新打开虚拟机, 仍然是打勾的, 证明没有被保存。

          

     (2) 修改sound_settings.xml

            android:persitant= "true"

            修改 SoundSettings.java

            mTr69.setPerstant(false);

           

            重新编译。 关机, 重新打开虚拟机,不管操作打勾还是不打勾,重新打开时都是不打勾的。

      (3)

            修改sound_settings.xml

             android:persistant="true".

             修改SoundSettings.java

             mTr69.setPersitant(true);

            

            重新编译, 使其不打勾。 关机, 重新打开虚拟机,不打勾, 证明已被保存。

      (4) sound_setting.xml中

              android:persistant = "true"

              修改SoundSettings.java

              将mTr69.setPersistant()删去。

              重新编译。  此时, 可以保存该 CheckBoxPreference的值。

     结论, Shared Preference是持久化存储的一种, 但 android:persistant必须要为 true才能保存。

    在SoundSettings中, 各个用来配置系统参数的Preference的 androidt:persistant 属性都为 false。

    所以, 不是用这里的preference来存储系统参数的。

    

原文地址:https://www.cnblogs.com/gavinwu/p/3105957.html