我有一个子类,InputMethodService
仅当用户在我的应用程序中时才应创建它,因此如果用户通过 Android 语言设置启用它,则应立即停用键盘并将其更改回默认键盘。我的子类是这样的:
public class CustomIME extends InputMethodService {
@Override
public void onCreate() {
super.onCreate();
// do onCreate() stuff and find current activity
if(currentActivity != myActivity){
switchBackToDefaultKeyboard(defaultKeyboard);
}
}
public void switchBackToDefaultKeyboard(IME ime) {
try {
switchInputMethod(ime.getId());
} catch (Exception e) {
DebugLog.e("Switching failed"); // this always fails when called from onCreate()
}
}
}
就像我在评论中写的那样,InputMethodService.switchInputMethod()
如果我试图从内部切换键盘,该方法总是会抛出异常onCreate()
。
异常消息是:
java.lang.SecurityException: Using null token requires permission android.permission.WRITE_SECURE_SETTINGS