如何检测用户的键盘是否为Swype?

7

有没有可能知道用户正在使用的键盘?我该如何检查用户是否正在使用Swype键盘?

3个回答

13

您可以使用以下代码检索当前默认键盘:

String currentKeyboard =  Settings.Secure.getString(getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
你将会得到一个类似于com.touchtype.swiftkey/com.touchtype.KeyboardService的结果,它是各种键盘的主要包名称和键盘服务的名称。只需解析此字符串以查看是否与Swype的信息匹配(我现在只能提供SwiftKey的详细信息,因为我没有安装Swype)。

谢谢! :) 我现在明白了!如果没有变量的解释和赋值,我会认为这段代码是用于选择要使用的键盘。 - Arci
请查看此答案:https://dev59.com/al3Ua4cB1Zd3GeqP_lUT#12557231 - Rafael

2

1
这里是其他所有内容:http://developer.android.com/reference/android/provider/Settings.Secure.html - anthropomo
谢谢!是的,我尝试搜索了一下,但似乎我没有使用正确的关键词。我想将您的答案设置为正确答案,但Raghav也在这里发布了带有解释的答案。 - Arci
完整性赢得了胜利。这很公平。无论如何,感谢您的点赞(我假设是您)。 - anthropomo

2
以下内容可让您确定正在使用的键盘是三星、谷歌还是Swype键盘。
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;
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接