gpt4 book ai didi

android - 确定启用的输入法

转载 作者:行者123 更新时间:2023-11-29 22:21:03 24 4
gpt4 key购买 nike

在 Android 上,我想以编程方式检查我的应用程序提供的 InputMethod 是否是当前激活的输入法

目前,我使用了一个技巧:检测我的 InputMethodService 是否启动。

public boolean isInputMethodEnabled() {
ActivityManager activityManager = (ActivityManager).getSystemService(ACTIVITY_SERVICE);
List<RunningServiceInfo> servicesInfo = activityManager.getRunningServices(Integer.MAX_VALUE);

for (RunningServiceInfo serviceInfo : servicesInfo) {
if (MyInputMethodService.class.getName().equals(serviceInfo.service.getClassName())) {
return true;
}
}
return false;
}

这在一般情况下运行良好,但在更新应用程序时失败:我的输入法仍处于选中状态,但服务尚未启动。该服务将在第一次需要键盘时启动,直到那时 isInputMethodEnabled() 将返回 false。

所以我的问题是:你知道另一种检查我的输入法是否被选中的方法吗,或者你知道在更新后重新启动输入法服务的方法,而不必显示 TextView ?

最佳答案

我在另一个 Stack Overflow 问题上找到了答案:

Android: Determine active input method from code

以下代码(已更新)起到了作用:

public boolean isInputMethodEnabled() {
String id = Settings.Secure.getString(mContext.getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);

ComponentName defaultInputMethod = ComponentName.unflattenFromString(id);

ComponentName myInputMethod = new ComponentName(mContext, MyInputMethodService.class);

return myInputMethod.equals(defaultInputMethod);
}

关于android - 确定启用的输入法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7171193/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com