Android 數(shù)據(jù)存儲到系統(tǒng)庫及修改系統(tǒng)配置

前言

生成的軟件識別唯一碼想保存到本地,卸載重新安裝后根據(jù)唯一碼進行設備識別浇借。經(jīng)過查詢發(fā)現(xiàn)可以使用Settings系統(tǒng)庫簡單實現(xiàn)捉撮。

使用方法

使用方法類似SharePreference,以鍵值對key-value的形式進行數(shù)據(jù)存儲妇垢,將數(shù)據(jù)內容保存到系統(tǒng)配置文件中巾遭,程序卸載安裝不影響系統(tǒng)文件肉康,仍然能獲取到之前保存的數(shù)據(jù)內容。

系統(tǒng)配置文件的路徑為:/data/data/com.android.providers.settings/databases/

1. 數(shù)據(jù)保存:

//設置系統(tǒng)配置文件中的配置數(shù)據(jù)灼舍,第一個參數(shù)固定,通過上下文獲取ContentResolver迎罗,第二個參數(shù)是要保存數(shù)據(jù)的Key值,第三個參數(shù)是要保存的數(shù)據(jù)即value值片仿。
Settings.System.putString(getContentResolver(), "key值", "保存數(shù)據(jù)value值");

2. 數(shù)據(jù)獲取:

//獲取系統(tǒng)配置文件中的數(shù)據(jù)尤辱,第一個參數(shù)固定,通過上下文獲取ContentResolver砂豌,第二個參數(shù)是已經(jīng)保存的數(shù)據(jù)的Key值,通過key獲取對應value值光督。
String value = Settings.System.getString(getContentResolver(), "key值");

3. 配置清單文件權限及權限獲妊艟唷:

<uses-permission android:name="android.permission.WRITE_SETTINGS"/>
//申請權限
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
    if (!Settings.System.canWrite(context)) {
        Intent intent = new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS);
        intent.setData(Uri.parse("package:" + context.getPackageName()));
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
    }
}

系統(tǒng)配置

通過Settings我們還可以修改系統(tǒng)中的默認值,例如修改亮度:

private static final int MIN_SCREEN_TIMEOUT_VALUE = 15000;
//修改系統(tǒng)滅屏時間為15秒
Settings.System.putInt(getApplicationContext().getContentResolver(),"screen_off_timeout", MIN_SCREEN_TIMEOUT_VALUE);

深入了解

<?xml version="1.0" encoding="utf-8"?>
<!--
/**
 * Copyright (c) 2009, The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
-->
<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>

    <bool name="def_bluetooth_on">true</bool>
    <bool name="def_wifi_display_on">false</bool>
    <bool name="def_install_non_market_apps">false</bool>
    <bool name="def_package_verifier_enable">true</bool>
    <!-- Comma-separated list of location providers.
         Network location is off by default because it requires
         user opt-in via Setup Wizard or Settings.
    -->
    <string name="def_location_providers_allowed" translatable="false">gps</string>
    <bool name="assisted_gps_enabled">true</bool>
    <bool name="def_netstats_enabled">true</bool>
    <bool name="def_usb_mass_storage_enabled">true</bool>
    <bool name="def_wifi_on">false</bool>
    <!-- 0 == never, 1 == only when plugged in, 2 == always -->
    <integer name="def_wifi_sleep_policy">2</integer>
    <bool name="def_wifi_wakeup_enabled">true</bool>
    <bool name="def_networks_available_notification_on">true</bool>

    <bool name="def_backup_enabled">false</bool>
    <string name="def_backup_transport" translatable="false">android/com.android.internal.backup.LocalTransport</string>

    <!-- Default value for whether or not to pulse the notification LED when there is a
         pending notification -->
    <bool name="def_notification_pulse">true</bool>

    <bool name="def_mount_play_notification_snd">true</bool>
    <bool name="def_mount_ums_autostart">false</bool>
    <bool name="def_mount_ums_prompt">true</bool>
    <bool name="def_mount_ums_notify_enabled">true</bool>

    <!-- user interface sound effects -->
    <integer name="def_power_sounds_enabled">1</integer>
    <string name="def_low_battery_sound" translatable="false">/system/media/audio/ui/LowBattery.ogg</string>
    <integer name="def_dock_sounds_enabled">0</integer>
    <integer name="def_dock_sounds_enabled_when_accessibility">0</integer>
    <string name="def_desk_dock_sound" translatable="false">/system/media/audio/ui/Dock.ogg</string>
    <string name="def_desk_undock_sound" translatable="false">/system/media/audio/ui/Undock.ogg</string>
    <string name="def_car_dock_sound" translatable="false">/system/media/audio/ui/Dock.ogg</string>
    <string name="def_car_undock_sound" translatable="false">/system/media/audio/ui/Undock.ogg</string>
    <integer name="def_lockscreen_sounds_enabled">1</integer>
    <string name="def_lock_sound" translatable="false">/system/media/audio/ui/Lock.ogg</string>
    <string name="def_unlock_sound" translatable="false">/system/media/audio/ui/Unlock.ogg</string>
    <string name="def_trusted_sound" translatable="false">/system/media/audio/ui/Trusted.ogg</string>
    <string name="def_wireless_charging_started_sound" translatable="false">/system/media/audio/ui/WirelessChargingStarted.ogg</string>
    <string name="def_charging_started_sound" translatable="false">/system/media/audio/ui/ChargingStarted.ogg</string>

    <!-- sound trigger detection service default values -->
    <integer name="def_max_sound_trigger_detection_service_ops_per_day" translatable="false">1000</integer>
    <integer name="def_sound_trigger_detection_service_op_timeout" translatable="false">15000</integer>

    <bool name="def_lockscreen_disabled">false</bool>
    <bool name="def_device_provisioned">false</bool>
    <integer name="def_dock_audio_media_enabled">1</integer>

    <!-- Notifications use ringer volume -->
    <bool name="def_notifications_use_ring_volume">true</bool>

    <!-- Default for Settings.System.VIBRATE_IN_SILENT -->
    <bool name="def_vibrate_in_silent">true</bool>

    <!-- Default for Settings.Secure.SYNC_PARENT_SOUNDS -->
    <bool name="def_sync_parent_sounds">true</bool>

    <!-- Default for Settings.Secure.ACCESSIBILITY_SPEAK_PASSWORD -->
    <bool name="def_accessibility_speak_password">true</bool>

    <!-- Default for Settings.Secure.TOUCH_EXPLORATION_ENABLED -->
    <bool name="def_touch_exploration_enabled">false</bool>

    <!-- Default value for Settings.Secure.ACCESSIBILITY_DISPLAY_MAGNIFICATION_SCALE -->
    <fraction name="def_accessibility_display_magnification_scale">200%</fraction>

    <!-- Default value for Settings.Secure.ACCESSIBILITY_DISPLAY_MAGNIFICATION_ENABLED -->
    <bool name="def_accessibility_display_magnification_enabled">false</bool>

    <!-- Default value for Settings.Secure.ACCESSIBILITY_DISPLAY_MAGNIFICATION_AUTO_UPDATE -->
    <bool name="def_accessibility_display_magnification_auto_update">true</bool>

    <!-- Default for Settings.System.USER_ROTATION -->
    <integer name="def_user_rotation">0</integer>

    <!-- Default for Settings.Secure.DOWNLOAD_MAX_BYTES_OVER_MOBILE. <=0 if no limit -->
    <integer name="def_download_manager_max_bytes_over_mobile">-1</integer>
    <!-- Default for Settings.Secure.DOWNLOAD_RECOMMENDED_MAX_BYTES_OVER_MOBILE. <=0 if no limit -->
    <integer name="def_download_manager_recommended_max_bytes_over_mobile">-1</integer>

    <!-- Default for Settings.Secure.LONG_PRESS_TIMEOUT_MILLIS -->
    <integer name="def_long_press_timeout_millis">400</integer>

    <!-- Default for Settings.Secure.MULTI_PRESS_TIMEOUT -->
    <integer name="def_multi_press_timeout_millis">300</integer>

    <!-- Default for Settings.Secure.SHOW_IME_WITH_HARD_KEYBOARD -->
    <bool name="def_show_ime_with_hard_keyboard">false</bool>

    <!-- Default for Settings.System.POINTER_SPEED -->
    <integer name="def_pointer_speed">0</integer>

    <!-- Default for DTMF tones enabled -->
    <bool name="def_dtmf_tones_enabled">true</bool>
    <!-- Default for UI touch sounds enabled -->
    <bool name="def_sound_effects_enabled">true</bool>

    <!-- Development settings -->
    <bool name="def_stay_on_while_plugged_in">false</bool>

    <!-- Number of retries for connecting to DHCP.
         Value here is the same as WifiStateMachine.DEFAULT_MAX_DHCP_RETRIES -->
    <integer name="def_max_dhcp_retries">9</integer>

    <!-- Default for Settings.Secure.USER_SETUP_COMPLETE -->
    <bool name="def_user_setup_complete">false</bool>

    <!-- Default for Settings.Global.LOW_BATTERY_SOUND_TIMEOUT.
         0 means no timeout; battery sounds will always play
         >0 is milliseconds of screen-off time after which battery sounds will not play -->
    <integer name="def_low_battery_sound_timeout">0</integer>

    <!-- Initial value for the Settings.Secure.IMMERSIVE_MODE_CONFIRMATIONS setting,
         which is a comma separated list of packages that no longer need confirmation
         for immersive mode.
         Override to disable immersive mode confirmation for certain packages. -->
    <string name="def_immersive_mode_confirmations" translatable="false"></string>

    <!-- Default for Settings.Global.WIFI_SCAN_ALWAYS_AVAILABLE -->
    <integer name="def_wifi_scan_always_available">0</integer>

    <!-- Default for Settings.Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS, 1==on -->
    <integer name="def_lock_screen_show_notifications">1</integer>

    <!-- Default for Settings.Secure.LOCK_SCREEN_ALLOW_PRIVATE_NOTIFICATIONS -->
    <bool name="def_lock_screen_allow_private_notifications">true</bool>

    <!-- Default for Settings.Global.HEADS_UP_NOTIFICATIONS_ENABLED, 1==on -->
    <integer name="def_heads_up_enabled">1</integer>

    <!-- Default for Settings.Global.DEVICE_NAME $1=MANUFACTURER $2=MODEL-->
    <string name="def_device_name">%1$s %2$s</string>

    <!-- Default for Settings.Global.DEVICE_NAME $1=MODEL-->
    <string name="def_device_name_simple">%1$s</string>

    <!-- Default for Settings.Secure.WAKE_GESTURE_ENABLED -->
    <bool name="def_wake_gesture_enabled">true</bool>

    <!-- Default state of tap to wake -->
    <bool name="def_double_tap_to_wake">true</bool>

    <!-- Default for Settings.Secure.NFC_PAYMENT_COMPONENT -->
    <string name="def_nfc_payment_component"></string>

    <!-- Default setting for ability to add users from the lock screen -->
    <bool name="def_add_users_from_lockscreen">false</bool>

    <!--  default setting for Settings.System.END_BUTTON_BEHAVIOR : END_BUTTON_BEHAVIOR_SLEEP -->
    <integer name="def_end_button_behavior">0x2</integer>

    <!-- default setting for Settings.Global.DEFAULT_RESTRICT_BACKGROUND_DATA -->
    <bool name="def_restrict_background_data">false</bool>

    <!-- Default for Settings.Secure.BACKUP_MANAGER_CONSTANTS -->
    <string name="def_backup_manager_constants"></string>

    <!-- Default setting for Settings.Global.MOBILE_DATA_ALWAYS_ON -->
    <bool name="def_mobile_data_always_on">true</bool>

    <!-- Default for Settings.Secure.BACKUP_LOCAL_TRANSPORT_PARAMETERS -->
    <string name="def_backup_local_transport_parameters"></string>

    <!-- Default for Settings.Global.ZEN_DURATION
        If 0, turning on dnd manually will last indefinitely.
        Else if non-negative, turning on dnd manually will last for this many minutes.
        Else (if negative), turning on dnd manually will surface a dialog that prompts
            user to specify a duration.-->
    <integer name="def_zen_duration">0</integer>

    <!-- Default for Settings.Global.BACKUP_AGENT_TIMEOUT_PARAMETERS -->
    <string name="def_backup_agent_timeout_parameters"></string>

    <!-- Default for Settings.System.VIBRATE_WHEN_RINGING -->
    <bool name="def_vibrate_when_ringing">false</bool>
</resources>

這些key對應的值橄仆,我們可以在/data/data/com.android.providers.settings/databases/settings.db里三個表global, system, secure中進行查看桐筏。

自從android 6.0以后這幾個表都變?yōu)榱藊ml文件冯丙,不同用戶放不同的路徑下,如果沒有創(chuàng)建新用戶咖熟,則在/data/system/users/0settings_global.xml, settings_system.xml, settings_secure.xml中。

通過修改文件中配置項對應內容就可以修改系統(tǒng)配置柳畔。

更多內容可以訪問:

GitHub地址:https://github.com/MickJson

歡迎點擊查閱及Star馍管,我也會繼續(xù)補充其它有用的知識及例子在項目上。

歡迎點贊/評論薪韩,你們的贊同和鼓勵是我寫作的最大動力确沸!

?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市俘陷,隨后出現(xiàn)的幾起案子罗捎,更是在濱河造成了極大的恐慌,老刑警劉巖拉盾,帶你破解...
    沈念sama閱讀 219,427評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件桨菜,死亡現(xiàn)場離奇詭異,居然都是意外死亡盾剩,警方通過查閱死者的電腦和手機雷激,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,551評論 3 395
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來告私,“玉大人屎暇,你說我怎么就攤上這事∽に冢” “怎么了根悼?”我有些...
    開封第一講書人閱讀 165,747評論 0 356
  • 文/不壞的土叔 我叫張陵凶异,是天一觀的道長。 經(jīng)常有香客問我挤巡,道長剩彬,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,939評論 1 295
  • 正文 為了忘掉前任矿卑,我火速辦了婚禮喉恋,結果婚禮上,老公的妹妹穿的比我還像新娘母廷。我一直安慰自己轻黑,他們只是感情好,可當我...
    茶點故事閱讀 67,955評論 6 392
  • 文/花漫 我一把揭開白布琴昆。 她就那樣靜靜地躺著氓鄙,像睡著了一般。 火紅的嫁衣襯著肌膚如雪业舍。 梳的紋絲不亂的頭發(fā)上抖拦,一...
    開封第一講書人閱讀 51,737評論 1 305
  • 那天,我揣著相機與錄音舷暮,去河邊找鬼态罪。 笑死,一個胖子當著我的面吹牛下面,可吹牛的內容都是我干的向臀。 我是一名探鬼主播,決...
    沈念sama閱讀 40,448評論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼诸狭,長吁一口氣:“原來是場噩夢啊……” “哼券膀!你這毒婦竟也來了?” 一聲冷哼從身側響起驯遇,我...
    開封第一講書人閱讀 39,352評論 0 276
  • 序言:老撾萬榮一對情侶失蹤芹彬,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后叉庐,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體舒帮,經(jīng)...
    沈念sama閱讀 45,834評論 1 317
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 37,992評論 3 338
  • 正文 我和宋清朗相戀三年陡叠,在試婚紗的時候發(fā)現(xiàn)自己被綠了玩郊。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,133評論 1 351
  • 序言:一個原本活蹦亂跳的男人離奇死亡枉阵,死狀恐怖译红,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情兴溜,我是刑警寧澤侦厚,帶...
    沈念sama閱讀 35,815評論 5 346
  • 正文 年R本政府宣布耻陕,位于F島的核電站,受9級特大地震影響刨沦,放射性物質發(fā)生泄漏诗宣。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,477評論 3 331
  • 文/蒙蒙 一想诅、第九天 我趴在偏房一處隱蔽的房頂上張望召庞。 院中可真熱鬧,春花似錦来破、人聲如沸裁眯。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,022評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至存皂,卻和暖如春晌坤,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背旦袋。 一陣腳步聲響...
    開封第一講書人閱讀 33,147評論 1 272
  • 我被黑心中介騙來泰國打工骤菠, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人疤孕。 一個月前我還...
    沈念sama閱讀 48,398評論 3 373
  • 正文 我出身青樓商乎,卻偏偏與公主長得像,于是被迫代替她去往敵國和親祭阀。 傳聞我的和親對象是個殘疾皇子鹉戚,可洞房花燭夜當晚...
    茶點故事閱讀 45,077評論 2 355