利用Setting Provider添加保存数据项

Framework需要定制的时候,可能会增加新的数据项,setting的数据实际是从SettingProvider里面读出来的,里面有一个settings.db的文件,在第一次启动android手机的时候会在默认的文件中读取设定的值,下面就以添加一项来说明这个过程:

1)第一次启动默认值在frameworks/base/packages/SettingsProvider/res/values/defaults.xml 中,需要添加相应的项

+    <string name="def_dongle_name" translatable="false">00:00:00:00:00</string>

2)frameworks/base/core/java/android/provider/Settings.java中,添加第一次启动加载的流程

a) 申明

+    public static final String DONGLE_NAME = "dongle_name";

 b)加载序列控制: 

+++ b/frameworks/base/core/java/android/provider/Settings.java
@@ -1724,13 +1724,6 @@ public final class Settings {
          */
         public static final int SCREEN_BRIGHTNESS_MODE_AUTOMATIC = 1;
 
-        /// frank: add for default dongle name @{
-        public static final String DONGLE_NAME = "dongle_name";
-        /// frank: add for default dongle name end @}
-
         /**
          * Control whether the process CPU usage meter should be shown.
          *
@@ -2372,9 +2365,6 @@ public final class Settings {
             SCREEN_BRIGHTNESS,
             SCREEN_BRIGHTNESS_MODE,
             SCREEN_AUTO_BRIGHTNESS_ADJ,
-            /// frank: add for default dongle name @{
-            DONGLE_NAME,
-            /// frank: add for default dongle name end @}
             VIBRATE_INPUT_DEVICES,
             MODE_RINGER,                // moved to global
             MODE_RINGER_STREAMS_AFFECTED,

c) 读取默认值


+++ b/frameworks/base/packages/SettingsProvider/src/com/android/providers/settings/DatabaseHelper.java
@@ -1991,11 +1991,6 @@ public class DatabaseHelper extends SQLiteOpenHelper {
             loadBooleanSetting(stmt, Settings.System.SCREEN_BRIGHTNESS_MODE,
                     R.bool.def_screen_brightness_automatic_mode);
 
+            /// frank: add for default dongle name @{
+            loadStringSetting(stmt, Settings.System.DONGLE_NAME,
+                              R.string.def_dongle_name);
+            /// frank: add for default dongle name end @}
+
             loadDefaultAnimationSettings(stmt);
 
             loadBooleanSetting(stmt, Settings.System.ACCELEROMETER_ROTATION,

以上的步骤在OOBE中被调用,以后的开关机流程就不会进去了


3 )

找一个入口更新值:

packages/apps/Settings/src/com/android/settings/wfd/WifiDisplaySettings.java
+        /// frank: add for dongle pair @{
+        DialogInterface.OnClickListener match = new DialogInterface.OnClickListener() {
+            @Override
+            public void onClick(DialogInterface dialog, int which) {
+                Xlog.d(TAG, "click match button, save WifiDisplay and auto connect enabled " 
+                      + " deviceAddress: " + display.getDeviceAddress());
+                Settings.System.putString(getActivity().getContentResolver(),
+                                          Settings.System.DONGLE_NAME, display.getDeviceAddress());
                
            }
        };
        AlertDialog dialog = new AlertDialog.Builder(getActivity())
                .setCancelable(true)
                .setTitle(R.string.wifi_display_options_title)
                .setView(view)
+               .setNeutralButton("match", match)
                .setPositiveButton(R.string.wifi_display_options_done, done)
                .setNegativeButton(R.string.wifi_display_options_forget, forget)
                .create();
        /// frank: add for dongle pair end @}



4) 引用,当数据发生变化的时候更新,可以在不同的线程中:

+    void init() {
+        mMatchedDeviceMac = Settings.System.getString(mContext.getContentResolver(),
+                            Settings.System.DONGLE_NAME);
+        Log.d(TAG, " DONGLE_NAME onInit mMatchedDeviceMac = " + mMatchedDeviceMac);
+        //resolver.unregisterContentObserver(mBrightnessObserver);
+    }
 
+    private void onMatchedDongleChanged() {
+        Log.d(TAG, "DONGLE_NAME onMatchedDongleChanged " +  "");
+        mMatchedDeviceMacOld = mMatchedDeviceMac;
+        mMatchedDeviceMac = Settings.System.getString(mContext.getContentResolver(),
+                             Settings.System.DONGLE_NAME);
+        Log.d(TAG, " DONGLE_NAME onMatchedDongleChanged dongle_name = " + mMatchedDeviceMac);
+    }
+    private ContentObserver mMatchedDongleObserver = new ContentObserver(new Handler()) {
+        @Override
+        public void onChange(boolean selfChange) {
+            onMatchedDongleChanged();
+        }
+    };


你可能感兴趣的:(利用Setting Provider添加保存数据项)