How to determine the current IME in Android?

前端 未结 3 1718
长发绾君心
长发绾君心 2020-12-15 07:01

I have an application where I would like to warn the user if they are not using the default Android softkeyboard. (i.e. they are using Swype or some thing else).

How

相关标签:
3条回答
  • 2020-12-15 07:30

    You can get a default IME, use:

    Settings.Secure.getString(getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
    
    0 讨论(0)
  • 2020-12-15 07:34

    InputMethodManager has getEnabledInputMethodList(). You get an InputMethodManager from getSystemService() in your Activity.

    0 讨论(0)
  • 2020-12-15 07:39

    Here's a bit of code I used to determine if GoogleKeyboard, Samsung Keyboard, or Swype Keyboard is used. The value returned by reflection for mCurId indicates the IME ID.

    Test with the different keyboards/input methods you are looking for to find the relevant one

    public boolean usingSamsungKeyboard(Context context){
        return usingKeyboard(context, "com.sec.android.inputmethod/.SamsungKeypad");
    }
    
    public boolean usingSwypeKeyboard(Context context){
        return usingKeyboard(context, "com.nuance.swype.input/.IME");
    }
    
    public boolean usingGoogleKeyboard(Context context){
        return usingKeyboard(context, "com.google.android.inputmethod.latin/com.android.inputmethod.latin.LatinIME");
    }   
    
    public boolean usingKeyboard(Context context, String keyboardId)
        {
            final InputMethodManager richImm =
              (InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE);
    
            boolean isKeyboard = false;
    
            final Field field;
            try
            {
                field = richImm.getClass().getDeclaredField("mCurId");
                field.setAccessible(true);
                Object value = field.get(richImm);
                isKeyboard = value.equals(keyboardId);
    
            }
            catch (IllegalAccessException e)
            {
    
            }
            catch (NoSuchFieldException e)
            {
    
            }
            return  isKeyboard;
        }
    
    0 讨论(0)
提交回复
热议问题