How to detect if user's keyboard is Swype?

后端 未结 3 762
攒了一身酷
攒了一身酷 2020-12-19 05:34

Is it possible to know the keyboard being used by the user? How do I check if the user is using a Swype keyboard?

3条回答
  •  猫巷女王i
    2020-12-19 06:23

    The following lets you determine if Samsung, Google, or Swype keyboard is used.

    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;
        }
    

提交回复
热议问题