为Android系统添加config.xml 新配置的设置
作者:xcy2011sky 时间:2022-02-19 23:31:48
在日常系统开发中,经常需要在adroid的framework修改或添加自己的配置。例如在config.xml 添加一个新的变量。我这边测试发现如果只是简单的添加配置项,在代码里面怎么也访问不到。为了解决这个问题仔细看了一下代码,最终发现需要在public.xml 定义才可以。
下面用一个例子来说明一下。
1.在framework/base/core/res/res/valus/config.xml 添加默认输入配置:
<!--
set default inputmethod.
-->
<string translatable="false" name="config_def_input_method">com.taypo.android.trskb/.TRSoftKeyboard</string>
这是默认输入法为土耳其语。
修改后,需要在framework/base/core/res/res z执行mm 编译一下修改。
完成后,croot到根目录,执行make update-api 更新一下api。
2.如果使用这个配置项
我这边在framework/base/service/java/com/android/interanl/InputMethodManangerService.java 中的resetDefaultIMeLocked函数使用这个变量
private void resetDefaultImeLocked(Context context) {
// Do not reset the default (current) IME when it is a 3rd-party IME
if (mCurMethodId != null
&& !InputMethodUtils.isSystemIme(mMethodMap.get(mCurMethodId))) {
return;
}
InputMethodInfo defIm = null;
String id=context.getResources().getString(com.android.internal.R.string.config_def_input_method);
Slog.i(TAG, "internal.id: " + id);
for (InputMethodInfo imi : mMethodList) {
if(imi.getId().equals(id)) defIm=imi;
}
/* if (defIm == null) {
if (InputMethodUtils.isValidSystemDefaultIme(
mSystemReady, imi, context)) {
defIm = imi;
Slog.i(TAG, "Selected default: " + imi.getId());
}
}
}
if (defIm == null && mMethodList.size() > 0) {
defIm = InputMethodUtils.getMostApplicableDefaultIME(
mSettings.getEnabledInputMethodListLocked());
Slog.i(TAG, "No default found, using " + defIm.getId());
}
*/
if (defIm != null) {
setSelectedInputMethodAndSubtypeLocked(defIm, NOT_A_SUBTYPE_ID, false);
}
}
这样使用很简单吧,一开始我以为就是这样,查了很多资料大家都是这样使用的。最后编译吧报了如下错误
frameworks/base/services/java/com/android/server/InputMethodManagerService.java:726: 找不到符号
符号: 变量
config_def_input_method
位置: 类
com.android.internal.R.string
String id=context.getResources().getString(com.android.internal.R.string.config_def_input_method);
但是我明明定义了,为什么还是找不到呢。
解决方案:
1.在framework/base/core/res/res/values/public.xml文件里增加对这些string的声明。
2.framework/base/core/res/res/ 下mm编译
3.到根目录下执行make update-api 更新api。
<public type="string" name="config_def_input_method" id="0x01040018" />
注意在 里面的id时一个递增的值,在系统中是唯一的,千万不要重复。
到此,在变异inputmethodmanagerService.java 就可以pass啦。
补充知识:向config.xml中添加一个配置项
1. 在config.xml中添加一项(路径:frameworks/base/core/res/res/values/)
如:
<bool name="config_myValue">true</bool>
2. 在 frameworks/base/core/res/res/values/symbols.xml中,添加:
<java-symbol type="bool" name="config_myValue"/>
3. 在frameworks/base/core/res/res/values/android.xml中,添加
一带有id的项,但此id怎么生成呢?如下步骤:
首先:在frameowrks/base/tools/aapt/ResourceTable.cpp中
在addSymbols()函数中,把如下的注释去掉:
//printf("<android type=\"%\" name=\"%\" id=...>,
然后,在代码根目录下,执行:
make framework-res > res.txt
就可以把android原始资源输出到res.txt文件中。
然后,把其中的
<android type="bool" name="config_myValue" id="0x0111005b"/>
的代码拷贝出来放到android.xml文件中即可。
来源:https://blog.csdn.net/xcy2011sky/article/details/41009869
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Android画画板的制作方法
WPF利用WindowChrome实现自定义窗口
![](https://img.aspxhome.com/file/2023/8/122708_0s.png)
Mybatis-Plus 条件构造器示例详解
![](https://img.aspxhome.com/file/2023/9/118809_0s.png)
详细解读Hibernate的缓存机制
SpringBoot集成Redis—使用RedisRepositories详解
![](https://img.aspxhome.com/file/2023/7/67017_0s.png)
C#中Task.Yield的用途深入讲解
C# ComboBox的联动操作(三层架构)
![](https://img.aspxhome.com/file/2023/5/113785_0s.png)
Spring AOP在web应用中的使用方法实例
![](https://img.aspxhome.com/file/2023/3/81353_0s.png)
java的IO流详细解读
Android入门之实现自定义Adapter
![](https://img.aspxhome.com/file/2023/3/88673_0s.jpg)
Lucene 索引删除策略源码解析
![](https://img.aspxhome.com/file/2023/5/65215_0s.jpg)
Android中EditText显示明文与密码的两种方式
![](https://img.aspxhome.com/file/2023/1/137681_0s.gif)
C#在图片增加文字的实现代码
浅谈Android Studio 3.0 的一些小变化
![](https://img.aspxhome.com/file/2023/1/126691_0s.png)
Linux下g++编译与使用静态库和动态库的方法
![](https://img.aspxhome.com/file/2023/2/68232_0s.jpg)