current position:Home>Add custom Key value in Settings in Android system

Add custom Key value in Settings in Android system

2022-09-23 10:02:31speak up

Settings ProviderSystem preferences are saved in the system settings data sheet,应用可通过Settings的APIAccess the corresponding preferences,主要Settings.Global、 Settings.Secure、 Settings.SystemThree types of settings.
Settings.Global:全局系统设置,Contains preferences that always apply the same way to all defined users. Applications can read these,但不允许写入;与“安全”设置一样,These settings apply if the user has to go through the system UI or private for these values API Explicitly modified preferences.Common setting items include Bluetooth switch status、adb开关状态等.
Settings.Secure:Security system settings,Contains system preferences that applications can read but not write to. These preferences apply to what the user has to go through the system UI or private for these values API Explicitly modified preferences,rather than being modified directly by the application.Common settings items are accessible、VPN状态等.
Settings.System:系统设置,Contains mixed system preferences. This table contains simple names/值对. There are some convenient methods for accessing individual settings entries.Commonly used setting items include ringtones、亮度、time format, etc.
Custom one is described belowSettings.Global的key的流程,自定义Settings.Secure、 Settings.System的方式类似.

Settings中的修改

源码路径 :

frameworks/base/core/java/android/provider/Settings.java

Settings的内部类GlobalAdd customizationKey的常量值,可以是String、int、boolean等常用类型.

 public static final class Global extends NameValueTable {
    
        /** * @hide */
        public static final String CUSTOM_KEY = "your_custom_key";
 }

MOVED_TO_GLOBALAdd the corresponding customization to the listKey:

        @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
        private static final HashSet<String> MOVED_TO_GLOBAL;
        @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
        private static final HashSet<String> MOVED_TO_SECURE_THEN_GLOBAL;
        static {
    
            MOVED_TO_GLOBAL = new HashSet<>();
            MOVED_TO_SECURE_THEN_GLOBAL = new HashSet<>();

            // these were originally in system but migrated to secure in the past,
            // so are duplicated in the Secure.* namespace
            MOVED_TO_SECURE_THEN_GLOBAL.add(Global.ADB_ENABLED);
            MOVED_TO_SECURE_THEN_GLOBAL.add(Global.BLUETOOTH_ON);
            ...省略
            MOVED_TO_GLOBAL.add(Settings.Global.RADIO_NFC);
            MOVED_TO_GLOBAL.add(Settings.Global.RADIO_CELL);
            MOVED_TO_GLOBAL.add(Settings.Global.RADIO_WIFI);
            MOVED_TO_GLOBAL.add(Settings.Global.RADIO_BLUETOOTH);
            MOVED_TO_GLOBAL.add(Settings.Global.RADIO_WIMAX);
            MOVED_TO_GLOBAL.add(Settings.Global.SHOW_PROCESSES);
            
            /** 添加你的自定义Keys */
            MOVED_TO_GLOBAL.add(Settings.Global.CUSTOM_KEY);

        }

再将自定义Key添加到PUBLIC_SETTINGSpublic systemkey列表中:


        /** * @hide */
        public static final String CUSTOM_KEY = Global.CUSTOM_KEY;

        /** * These are all public system settings * * @hide */
        @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
        public static final Set<String> PUBLIC_SETTINGS = new ArraySet<>();
        static {
    
            PUBLIC_SETTINGS.add(END_BUTTON_BEHAVIOR);
            PUBLIC_SETTINGS.add(WIFI_USE_STATIC_IP);
            PUBLIC_SETTINGS.add(WIFI_STATIC_IP);
            PUBLIC_SETTINGS.add(WIFI_STATIC_GATEWAY);
            PUBLIC_SETTINGS.add(WIFI_STATIC_NETMASK);
            ...省略
            PUBLIC_SETTINGS.add(HAPTIC_FEEDBACK_ENABLED);
            PUBLIC_SETTINGS.add(SHOW_WEB_SUGGESTIONS);
            PUBLIC_SETTINGS.add(VIBRATE_WHEN_RINGING);
            PUBLIC_SETTINGS.add(APPLY_RAMPING_RINGER);
            
            /** 添加你的自定义Keys*/
            PUBLIC_SETTINGS.add(CUSTOM_KEY);
        }

SettingsProvider中的修改

1.在SettingsProvideradded in the system application module默认值.
源码路径:

frameworks/base/packages/SettingsProvider/res/values/defaults.xml

代码如下:

<resources>
    <bool name="def_dim_screen">true</bool>
    <integer name="def_screen_off_timeout">60000</integer>
    <integer name="def_sleep_timeout">-1</integer>
    <bool name="def_airplane_mode_on">false</bool>
    <bool name="def_theater_mode_on">false</bool>
    <!-- Comma-separated list of bluetooth, wifi, and cell. -->
    <string name="def_airplane_mode_radios" translatable="false">cell,bluetooth,wifi,nfc,wimax</string>
    <string name="airplane_mode_toggleable_radios" translatable="false">bluetooth,wifi,nfc</string>
    <string name="def_bluetooth_disabled_profiles" translatable="false">0</string>
    <bool name="def_auto_time">true</bool>
    <bool name="def_auto_time_zone">true</bool>
    <bool name="def_accelerometer_rotation">false</bool>
    <!-- Default screen brightness, from 0 to 255. 102 is 40%. -->
    <integer name="def_screen_brightness">102</integer>
    <bool name="def_screen_brightness_automatic_mode">false</bool>
    <fraction name="def_window_animation_scale">100%</fraction>
    <fraction name="def_window_transition_scale">100%</fraction>
    <bool name="def_haptic_feedback">true</bool>
    ...省略

    <!-- 添加你的自定义Keys -->
    <string name="def_custom_key">key1</bool>

2.通过loadGlobalSettings加载自定义的Keys到SettingsProvidersystem application module数据库中.
源码路径:

frameworks/base/packages/SettingsProvider/src/com/android/providers/settings/DatabaseHelper.java

代码如下:

    private void loadGlobalSettings(SQLiteDatabase db) {
    
        SQLiteStatement stmt = null;
        final Resources res = mContext.getResources();
        try {
    
            stmt = db.compileStatement("INSERT OR IGNORE INTO global(name,value)"
                    + " VALUES(?,?);");

            // --- Previously in 'system'
            loadBooleanSetting(stmt, Settings.Global.AIRPLANE_MODE_ON,
                    R.bool.def_airplane_mode_on);
   
            ...省略
            loadSetting(stmt, Settings.Global.LID_BEHAVIOR, defaultLidBehavior);
            
            //添加你的自定义Keys
            loadStringSetting(stmt, Settings.Global.CUSTOM_KEY, R.string.def_custom_key);
        } finally {
    
            if (stmt != null) stmt.close();
        }
    }

    private void loadStringSetting(SQLiteStatement stmt, String key, int resid) {
    
        loadSetting(stmt, key, mContext.getResources().getString(resid));
    }

结语

完成以上修改后,You can use the customized one in any system applicationKey:

     String value = Settings.Global.getString(getContentResolver(), Settings.Global.CUSTOM_KEY);

     Settings.Global.putString(getContentResolver(), Settings.Global.CUSTOM_KEY, " ");

For more, please refer to another blog forSettingsProvider的相关介绍:浅谈androidData sharing in system applications

copyright notice
author[speak up],Please bring the original link to reprint, thank you.
https://en.cdmana.com/2022/266/202209230955486737.html

Random recommended