Android系统添加config.xml 新配置

来源:互联网 发布:mysql队列 编辑:程序博客网 时间:2024/05/29 02:55

Android系统开发交流群:484966421 OSHome。

微信公众号:oshome2015


在日常系统开发中,经常需要在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啦。

1 0