我花了一天的時間研究,修改第三方的國筆輸入法的手寫默認顯示方式,原先是全屏模式,我要改為非全屏模式。
首先把國筆輸入法生成的sharedPreferences文件拷貝出來,查看顯示全屏與非全屏的關鍵字。
但是2.3系統使用
try {
otherAppsContext=createPackageContext("com.guobi.gbime", CONTEXT_IGNORE_SECURITY );
} catch (NameNotFoundException e) {
// TODO Auto-generated catch block
Log.i("guobi", e.getMessage());
}
SharedPreferences sharedPreferences = otherAppsContext.getSharedPreferences("com.guobi.gbime_preferences", Context.MODE_WORLD_READABLE);
String name2 = sharedPreferences.getString("HandwriteFrameType", "");是可以獲取到數據,而4.0就不行,打印的是空。
最終發現主要是權限問題。
國筆輸入法生成的sharedPreferences文件是rw- rw- ---,,所以我們必須改變它的權限進行操作,其實目錄也要改權限,都改為777。我在launcher的oncreate()方法中加入以下代碼
//修改國筆輸入法的顯示模式,把全屏改為非全屏模式
xmlFile = new File("/data/data/com.guobi.gbime/shared_prefs/com.guobi.gbime_preferences.xml");
if(xmlFile.exists())
{
try {
Runtime.getRuntime().exec(new String[]{"su","-c","chmod 777 /data/data/com.guobi.gbime/shared_prefs/com.guobi.gbime_preferences.xml"});
Log.i("guobi", "chmod 777");
Runtime.getRuntime().exec(new String[]{"su","-c","rm /data/data/com.guobi.gbime/shared_prefs/com.guobi.gbime_preferences.xml"});
Log.i("guobi", " rm");
Runtime.getRuntime().exec(new String[]{"su","-c","touch /data/data/com.guobi.gbime/shared_prefs/com.guobi.gbime_preferences.xml"});
Log.i("guobi", " touch");
Runtime.getRuntime().exec(new String[]{"su","-c","chmod 777 /data/data/com.guobi.gbime/shared_prefs/com.guobi.gbime_preferences.xml"});
Log.i("guobi", " chomd");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
else
{
if(!xmlFile.getParentFile().exists())
{
try {
Runtime.getRuntime().exec(new String[]{"su","-c","mkdir /data/data/com.guobi.gbime/shared_prefs"});
Log.i("guobi", "mkdir");
Runtime.getRuntime().exec(new String[]{"su","-c","chmod 777 /data/data/com.guobi.gbime/shared_prefs"});
Log.i("guobi", " chomd");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
try {
Runtime.getRuntime().exec(new String[]{"su","-c","touch /data/data/com.guobi.gbime/shared_prefs/com.guobi.gbime_preferences.xml"});
Log.i("guobi", "touch");
Log.i("guobi", "chmod 777");
Runtime.getRuntime().exec(new String[]{"su","-c","chmod 777 /data/data/com.guobi.gbime/shared_prefs/com.guobi.gbime_preferences.xml"});
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
try {
otherAppsContext=mContext.createPackageContext("com.guobi.gbime",mContext. CONTEXT_IGNORE_SECURITY );
} catch (NameNotFoundException e) {
// TODO Auto-generated catch block
Log.i("guobi", e.getMessage());
}
SharedPreferences sharedPreferences = otherAppsContext.getSharedPreferences("com.guobi.gbime_preferences", Context.MODE_WORLD_READABLE);
{
SharedPreferences.Editor guobieditor=sharedPreferences.edit();
guobieditor.putString("HandwriteLang", "china");
guobieditor.putString("HandwriteFrameType", "box");
guobieditor.putString("IMType", "pinyin");
guobieditor.putString("KeyboardType", "26");
guobieditor.putString("HandwriteStrokeMask", "normal");
guobieditor.putString("SkinType", "default");
guobieditor.commit();
String name2 = sharedPreferences.getString("HandwriteFrameType", "");
Log.i("guobi", "HandwriteFrameType--"+name2);
}
不要高興太早,這只是成功了一半,你如果加在launcher中,會發現,每次設置為全屏模式後,要重啟兩次才能變成非全屏,後了我發現了,原來這是默認的輸入法,他每次都是在launcher啟動完之前就啟動了,所以我們需要放在Launcher應用啟動之前去把數據寫進去才能成功。這就要考驗我們對Android的啟動流程有所了解。
android4.0/frameworks/base/services/java/com/android/server/am/ActivityManagerService.java中有個方法叫startHomeActivityLocked(),這個方法是條用啟動launcher的方法,我們在最前面加入上面的代碼,launcher中的不需要。OK大功告成。每次啟動都是默認為非全屏模式。
更多Android相關信息見Android 專題頁面 http://www.linuxidc.com/topicnews.aspx?tid=11