項目中有時候需要保存一些永久變量刊懈,使用系統(tǒng)數(shù)據(jù)庫是一個好的選擇咸这,但是如果該變量需要在第三方APP中訪問,就還需要解決要解決權(quán)限問題蚓峦。
下面以變量notification_panel_enable
徽级,bool類型為例
一气破、添加變量
首先要進行變量初始化,一般位于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)會進行調(diào)用進程的包名判斷
if(!"notification_panel_enable".equals(name)){ //bianjb 自定義數(shù)據(jù)不進行權(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)