Android 配置默认输入法
1.背景
最近有个国内的项目,预制了输入法apk,但是无法调出软键盘。原因是没有配置默认输入法,本文主要记录下如何配置默认输入法。
2.代码设置
设置默认输入法需要配置Settings.Secure.ENABLED_INPUT_METHODS和Settings.Secure.DEFAULT_INPUT_METHOD的值。
(1)添加默认属性
frameworks/base/packages/SettingsProvider/res/values/defaults.xml
<string name="enabled_input_methods" translatable="false">com.android.inputmethod.latin/.LatinIME:com.xxx.xxx/.LatinIME:com.xxx.xxx/com.xxx.xxx.LatinIME</string>
<string name="def_input_method" translatable="false">com.xxx.xxx/com.xxx.xxx.LatinIME</string>
(2)加载默认属性
frameworks/base/packages/SettingsProvider/src/com/android/providers/settings/DatabaseHelper.java
private void loadSecureSettings(SQLiteDatabase db) {
SQLiteStatement stmt = null;
try {
stmt = db.compileStatement("INSERT OR IGNORE INTO secure(name,value)"
+ " VALUES(?,?);");
......
loadIntegerSetting(stmt, Settings.Secure.SLEEP_TIMEOUT,
R.integer.def_sleep_timeout);
//add for default method
loadStringSetting(stmt, Settings.Secure.ENABLED_INPUT_METHODS,
R.string.enabled_input_methods);
loadStringSetting(stmt, Settings.Secure.DEFAULT_INPUT_METHOD,
R.string.def_input_method);
//add for default method
/*
* IMPORTANT: Do not add any more upgrade steps here as the global,
* secure, and system settings are no longer stored in a database
* but are kept in memory and persisted to XML.
*
* See: SettingsProvider.UpgradeController#onUpgradeLocked
*/
} finally {
if (stmt != null) stmt.close();
}
}
PS:如果无法确认enabled_input_methods 和def_input_method的值的话,可以在设置里手动将内置的输入法apk设置成默认输入法,然后通过adb shell 命令查看当前默认输入法的对应值。
adb shell settings get secure default_input_method
adb shell settings get secure enabled_input_methods
adb shell ime list -a可以查看已安装的输入法apk的属性