Java 在创建InputMethodService时销毁它

Java 在创建InputMethodService时销毁它,java,android,exception,android-input-method,android-securityexception,Java,Android,Exception,Android Input Method,Android Securityexception,我有一个子类InputMethodService,只有当用户在我的应用程序中时才应创建该子类,因此如果用户通过Android语言设置启用该子类,则应立即停用键盘并将其更改回默认键盘。我的子类是这样的: public class CustomIME extends InputMethodService { @Override public void onCreate() { super.onCreate(); // do onCreate() s

我有一个子类
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:使用空令牌需要权限android.permission.WRITE\u SECURE\u设置