標籤:android style blog http java color
Android源碼中預設的有三種IME:英文,中文,日文。對應的工程代碼路徑為:
<android_root>/packages/inputmethods/LatinIME/
<android_root>/packages/inputmethods/OpenWnn/
<android_root>/packages/inputmethods/PinyinIME/
一般情況下,預設都是選擇的LatinIMEIME,但是Android系統預設都是選擇系統語言作為IME,比如我們要用中文IME,
就需要切換系統語言為中文,或不勾選系統語言,主動勾選中文,但是我們怎麼修改預設的語言IME呢?
主要要關注3個模組代碼:
(1) Setting源碼
(2) SettingsProvider源碼
(3) LatinIMEIME源碼
方法一:(修改預設IME語言為英文和泰文,親測ok)
(1) 修改 packages\inputmethods\LatinIME\java\AndroidManifest.xml,增加
<uses-permission android:name="android.permission.WRITE_SETTINGS" /> <uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS" /> <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> <receiver android:name="LatinImeReceiver" android:enabled="true"> <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED" /> </intent-filter> </receiver>
(2) 在packages\inputmethods\LatinIME\java\src\com\android\inputmethod\latin目錄增加LatinImeReceiver.java檔案,源碼如下
package com.android.inputmethod.latin; import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.content.SharedPreferences;import android.provider.Settings;import android.util.Log;import android.view.inputmethod.InputMethodInfo;import android.view.inputmethod.InputMethodManager;import android.view.inputmethod.InputMethodSubtype;import android.text.TextUtils;import android.content.SharedPreferences.Editor; import android.preference.PreferenceManager; public class LatinImeReceiver extends BroadcastReceiver { private static final String TAG = LatinImeReceiver.class.getSimpleName(); private static final String[] DEFAULT_LATIN_IME_LANGUAGES = {"en_US","th"};//預設開啟IME的語言 @Override public void onReceive(Context context, Intent intent) { // Set the default input language at the system boot completed. if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) { Log.i(TAG,"onReceive:ACTION_BOOT_COMPLETED"); SharedPreferences sp = context.getSharedPreferences("default_input_language_config", Context.MODE_PRIVATE); boolean hasSet = sp.getBoolean("has_set", false); if (!hasSet) { setDefaultSubtypes(context); sp.edit().putBoolean("has_set", true).commit(); } } } /** * M: Set the default IME subtype. */ private void setDefaultSubtypes(Context context) { Log.i(TAG,"setDefaultSubtypes"); final String serviceName = "com.android.inputmethod.latin/.LatinIME"; final String currentPackageName = "com.android.inputmethod.latin"; final String enable = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ENABLED_INPUT_METHODS); Log.i(TAG,"enable="+enable);//com.android.inputmethod.latin/.LatinIME final InputMethodManager imm = (InputMethodManager) context.getSystemService( Context.INPUT_METHOD_SERVICE); final StringBuilder builder = new StringBuilder(); // Get sub type hash code for (InputMethodInfo info : imm.getInputMethodList()) { if (currentPackageName.equals(info.getPackageName())) { Log.i(TAG,"info.getSubtypeCount()="+info.getSubtypeCount());//55 for (int i = 0; i < info.getSubtypeCount(); i++) { final InputMethodSubtype subtype = info.getSubtypeAt(i); final String locale = subtype.getLocale().toString(); Log.i(TAG,"locale="+locale); if (isDefaultLocale(locale)) { Log.i(TAG, "default enabled subtype locale = " + locale); builder.append(‘;‘); builder.append(subtype.hashCode()); } } break; } } // Insert the sub type if (builder.length() > 0 && !TextUtils.isEmpty(enable)) { final String subtype = builder.toString(); builder.setLength(0); final int index = enable.indexOf(serviceName) + serviceName.length(); if (enable.length() > index) { builder.append(enable.substring(0, index)); builder.append(subtype); builder.append(enable.substring(index)); } else if (enable.length() == index) { builder.append(enable); builder.append(subtype); } else { return; } } else { Log.w(TAG, "Build Latin IME subtype failed: " + " builder length = " + builder.length() + "; enable isEmpty :" + TextUtils.isEmpty(enable)); return; } /*android/packages/inputmethods/LatinIME/java/res/xml/method.xml -921088104;529847764分別代表en_US和th */ Log.i(TAG,"commoit:"+builder.toString());//com.android.inputmethod.latin/.LatinIME;-921088104;529847764 // Commit the result android.provider.Settings.Secure.putString(context.getContentResolver(), android.provider.Settings.Secure.ENABLED_INPUT_METHODS, builder.toString()); String lastInputMethodId = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD); Log.w(TAG, "DEFAULT_INPUT_METHOD = " + lastInputMethodId); //com.android.inputmethod.latin/.LatinIME if(lastInputMethodId.equals(serviceName)) { Log.w(TAG, "DEFAULT_INPUT_METHOD = com.android.inputmethod.latin/.LatinIME" ); for (InputMethodInfo info : imm.getInputMethodList()) { if (currentPackageName.equals(info.getPackageName())) { for (int i = 0; i < info.getSubtypeCount(); i++) { final InputMethodSubtype subtype = info.getSubtypeAt(i); final String[] locales = DEFAULT_LATIN_IME_LANGUAGES; Log.w(TAG, "i = " + i + ", locales[0] = " + locales[0]); if((subtype.getLocale()).equals(locales[0])) { Log.w(TAG, "putString " + subtype.hashCode()); android.provider.Settings.Secure.putInt(context.getContentResolver(), android.provider.Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE, subtype.hashCode()); } } } } } } /** * M: Check if the current locale is default or not. */ private boolean isDefaultLocale (String locale) { final String[] locales = DEFAULT_LATIN_IME_LANGUAGES; for (String s : locales) { if (s.equals(locale)) { return true; } } return false; } }
方法二:
還有一種修改方案:(在amlogic驗證是OK的)
(1) 首先frameworks\base\packages\SettingsProvider\res\values\defaults.xml 增加下面語句
<string name="def_input_methods">com.android.inputmethod.latin/.LatinIME;529847764;-921088104</string>
意思是增加英文和泰文IME,android/packages/inputmethods/LatinIME/java/res/xml/method.xml中有定義的
-921088104;529847764分別代表en_US和th
(2) 然後在frameworks\base\packages\SettingsProvider\src\com\android\providers\settings\DatabaseHelper.java
增加如下代碼
loadStringSetting(stmt, Settings.Secure.ENABLED_INPUT_METHODS, R.string.def_input_methods);
frameworks/base/packages/SettingsProvider的作用
我們在調用android.provider.Settings修改一些設定時,Settings會調用真正的SettingsProvider去訪問資料庫。
android把SettingsProvider的代碼放在了frameworks/base/packages下面。
Android framework系統預設設定修改
修改Settings源碼可修改系統設定項,Settings資料被存放於com.android.providers.settings/databases/settings.db 中,如果想修改系統啟動後載入的預設值,一種方法是直接修改settings.db的值,另一種就是修改SettingsProvider預設值
Settings應用能夠配置Android系統的各種設定,這些設定的預設值都是由frameworks中的SettingsProvider從資料庫中讀取的,
那麼第一次開機的時候這些資料都是從哪兒來的呢?
frameworks/base/packages/SettingsProvider/res/values/defaults.xml這個檔案就是用來儲存Android系統的預設設定
如果想定義defaults.xml中沒有的,在這裡添加後,需修改frameworks/base/packages/SettingsProvider/src/com/android/providers/settings/DatabaseHelper.java,加入自己的儲存代碼。
例如:
600000設定關屏逾時時間的預設值
102 設定亮度的預設值
false設定是否允許安裝非Market應用程式的預設值
參考:
Android的語言設定(一)
http://blog.csdn.net/seker_xinjian/article/details/6288957
LatinIME怎麼預設勾選幾種語言
http://wenku.baidu.com/link?url=gAscqnKoMNOi_wzR3LEsk9kw-Hsp6k-hkWsW3_Jvyz3SmMXkENODD6XjRtS9BrndAS4iY2IuM8nQaxj05J4NUmBFdFE5-7nl9P9bVIwqfCm
Android framework系統預設設定修改
http://blog.csdn.net/tiantian715/article/details/7739294
在android settings.db資料庫中添加一項新的設定
http://www.2cto.com/kf/201303/198067.html