我有一个应用程序,如果用户没有使用默认的 Android 软键盘,我想警告用户。 (即他们正在使用 Swype 或其他东西)。
如何查看他们当前选择了哪种输入法?
您可以获得默认输入法,使用:
Settings.Secure.getString(getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
InputMethodManager
有getEnabledInputMethodList()
。您会从 InputMethodManager
中的 getSystemService()
获得 Activity
。
从 API 34 开始,
InputMethodManager
还有一个方法 getCurrentInputMethodInfo()
,它似乎返回当前选择的 IME。
这是我用来确定是否使用 GoogleKeyboard、Samsung Keyboard 或 Swype Keyboard 的一些代码。 mCurId 反射返回的值表示 IME ID。
使用您正在寻找的不同键盘/输入法进行测试,以找到相关的
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;
}