Android10添加系統(tǒng)數(shù)據(jù)庫Settings.Global字段

項目中有時候需要保存一些永久變量,使用系統(tǒng)數(shù)據(jù)庫是一個好的選擇,但是如果該變量需要在第三方APP中訪問,就還需要解決要解決權(quán)限問題。

下面以變量notification_panel_enable,bool類型為例

一、添加變量

首先要進(jìn)行變量初始化,一般位于SettingsProvider

定義變量,方便引用

diff --git a/core/java/android/provider/Settings.java b/core/java/android/provider/Settings.java
public final class Settings {
          */
         public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/global");
 
+               //bianjb 狀態(tài)欄下拉開關(guān)
+        public static final String NOTIFICATION_PANEL_ENABLE = "notification_panel_enable"; 
+

初始化變量

diff --git a/packages/SettingsProvider/res/values/defaults.xml 
 <resources>
+       <!--bianjb 添加狀態(tài)欄下拉開關(guān) -->
+    <bool name="notification_panel_enable">true</bool>

diff --git a/packages/SettingsProvider/src/com/android/providers/settings/DatabaseHelper.java 
class DatabaseHelper extends SQLiteOpenHelper {
         try {
             stmt = db.compileStatement("INSERT OR IGNORE INTO global(name,value)"
                     + " VALUES(?,?);");
+                       //bianjb 添加狀態(tài)欄下拉開關(guān)
+            loadBooleanSetting(stmt, Settings.Global.NOTIFICATION_PANEL_ENABLE,
+                    R.bool.notification_panel_enable);

二、解決第三方APP讀寫的權(quán)限問題

diff --git a/packages/SettingsProvider/src/com/android/providers/settings/SettingsProvider.java 
@@ -1249,18 +1250,23 @@ public class SettingsProvider extends ContentProvider {
     private boolean mutateGlobalSetting(String name, String value, String tag,
             boolean makeDefault, int requestingUserId, int operation, boolean forceNotify,
             int mode) {
-        // Make sure the caller can change the settings - treated as secure.
 -       enforceWritePermission(Manifest.permission.WRITE_SECURE_SETTINGS);
         
       
         String pkg = "com.android.settings"; //數(shù)據(jù)寫入時使用系統(tǒng)settings應(yīng)用的包名,系統(tǒng)會進(jìn)行調(diào)用進(jìn)程的包名判斷
         if(!"notification_panel_enable".equals(name)){  //bianjb 自定義數(shù)據(jù)不進(jìn)行權(quán)限驗證
              pkg = "";
              // Make sure the caller can change the settings - treated as secure.
              enforceWritePermission(Manifest.permission.WRITE_SECURE_SETTINGS);
            }

//下面修改在插入操作時使用上面預(yù)設(shè)的包名,繞過包名驗證
@@ -1268,7 +1274,7 @@ public class SettingsProvider extends ContentProvider {
                 case MUTATION_OPERATION_INSERT: {
                     return mSettingsRegistry.insertSettingLocked(SETTINGS_TYPE_GLOBAL,
                             UserHandle.USER_SYSTEM, name, value, tag, makeDefault,
-                            getCallingPackage(), forceNotify, CRITICAL_GLOBAL_SETTINGS);
+                            "".equals(pkg)?getCallingPackage():pkg, forceNotify,  CRITICAL_GLOBAL_SETTINGS);
                 }

三、變量讀寫

//寫
Settings.Global.putInt(mContext.getContentResolver(),Settings.Global.NOTIFICATION_PANEL_ENABLE,1);
 //讀
Settings.Global.getInt(mContext.getContentResolver(),Settings.Global.NOTIFICATION_PANEL_ENABLE, 1)
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容