您的位置:首页 > 移动开发 > Android开发

android如何添加全局变量宏开关

2017-12-07 14:44 211 查看
systemprop
在java应用文件使用

在cpp文件中的使用方式

在frameworkbasecoreresres的资源中添加一个变量

SettingProvider

system.prop

可以在system.prop中定义一个宏开关,跨应用的读写此值

在system.prop中添加:

ro.carso.audio_debug = true;


在java应用文件使用

import android.os.SystemProperties;
...
if(SystemProperties.getBoolean("ro.carso.audio_debug", false) == true){
...
}


这个使用方式,需要应用程序有system或root权限。

将android程序的权限提升到system权限,有下面方法:

(1) 在AndroidManifest.xml中,在manifest加入

android:sharedUserId="android.uid.system"


(2)在Android.mk中,將LOCAL_CERTIFICATE := XXX修改成

LOCAL_CERTIFICATE := platform


(3)有时会显示无法import android.os.SystemProperties

修改方法如下:

注释掉相应Android.mk文件中的以下内容:

#LOCAL_SDK_VERSION := current


备注:因为SystemProperties是非标准的SDK接口,如果要使用,不能定义LOCAL_SDK_VERSION变量。该变量表示应用只使用标准的SDK接口。

在cpp文件中的使用方式

#include <cutils/properties.h>
......
char carsonAudioValue[PROPERTY_VALUE_MAX];
bool ro.carso.audio_debug = false;
if(property_get("ro.carso.audio_debug", carsonAudioValue, "false")) {
ro.carso.audio_debug = atoi(carsonAudioValue) || !strncmp("true",
carsonAudioValue, 4);
}
if(ro.carso.audio_debug == true){
......
}
.......


在framework/base/core/res/res的资源中添加一个变量

例如 config_animateScreenLights:

/frameworks/base/core/res/res/values/symbols.xml

<java-symbol type="bool" name="config_animateScreenLights" />


/frameworks/base/core/res/res/values/config.xml

<!-- If this is true, the screen will fade off. -->
<bool name="config_animateScreenLights">false</bool>


在java文件中的读取此变量

// True if we should fade the screen while turning it off, false if
4000
we should play
// a stylish color fade animation instead.
private boolean mColorFadeFadesConfig;
.......
final Resources resources = context.getResources();
......
mColorFadeFadesConfig = resources.getBoolean(
com.android.internal.R.bool.config_animateScreenLights);


不足之处是只能读取此变量,不能改变此变量的值

SettingProvider

SettingProvider本质是使用contentprovider的跨应用的特性来实现此需求

定义默认值:

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

public static final class System extends NameValueTable {
......
/**
* Whether the sounds effects (key clicks, lid open ...) are enabled. The value is
* boolean (1 or 0).
*/
public static final String SOUND_EFFECTS_ENABLED = "sound_effects_enabled";
......
}


将默认值给其赋值

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

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


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

loadBooleanSetting(stmt, Settings.System.SOUND_EFFECTS_ENABLED, R.bool.def_sound_effects_enabled);

import android.provider.Settings;
class DatabaseHelper extends SQLiteOpenHelper {
.......
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int currentVersion) {
......
if (upgradeVersion == 49) {
/*
* New settings for new user interface noises.
*/
db.beginTransaction();
SQLiteStatement stmt = null;
try {
stmt = db.compileStatement("INSERT INTO system(name,value)"
+ " VALUES(?,?);");
loadUISoundEffectsSettings(stmt);
db.setTransactionSuccessful();
} finally {
db.endTransaction();
if (stmt != null) stmt.close();
}

upgradeVersion = 50;
}
......
}
private void loadUISoundEffectsSettings(SQLiteStatement stmt) {
......
loadBooleanSetting(stmt, Settings.System.SOUND_EFFECTS_ENABLED,
R.bool.def_sound_effects_enabled);
......
}
private void loadBooleanSetting(SQLiteStatement stmt, String key, int resid) {
loadSetting(stmt, key,
mContext.getResources().getBoolean(resid) ? "1" : "0");
}
private void loadSetting(SQLiteStatement stmt, String key, Object value) {
stmt.bindString(1, key);
stmt.bindString(2, value.toString());
stmt.execute();
}


读取方式

packages/apps/TvSettings/Settings/src/com/android/tv/settings/device/sound/SoundFragment.java

int soundEffect=Settings.System.getInt(contentResolver,
Settings.System.SOUND_EFFECTS_ENABLED, 1);


原来,我们不需要在framework下来定义这个db字段,可以直接在app层来新增加一个字段,然后直接使用。

比如:

import android.provider.Settings;


//新增加一个test_set_db,给其赋值

Settings.System.putInt(context.getContentResolver(), "test_set_db", 1);


//新增加一个test_set_db,获取其值

Settings.System.getInt(context.getContentResolver(), "test_set_db", 0)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: