java - 如何将 InputMethodService 与服务连接?

标签 java android service keyboard

我想将扩展 InputMethodService 的自定义软键盘与另一个服务连接起来。我需要从通用服务调用我的软键盘中的方法。

问题是我无法扩展 Binder 类,因为 onBind() 方法在 AbstractInputMethodService 中定义为 final...

现在,如何从其他服务调用我的 SoftKeyboard 类中的方法?

最佳答案

我已经通过使用广播接收器类解决了这个问题。

    class MyReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals("GET_KEY_KEY")) {
            String msg = intent.getStringExtra("msg");
                InputConnection ic = getCurrentInputConnection();
                if (ic != null) {
                    ic.sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DPAD_RIGHT));
                }
        }

    }
}

然后我向我的服务发送了一个广播。

                Intent intent = new Intent();
            intent.setAction("GET_KEY_KEY");
            sendBroadcast(intent);

关于java - 如何将 InputMethodService 与服务连接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56299512/

相关文章:

java - 在 Java 中声明全局 arrayList

java - 是否从标准 Java API 中删除了任何内容?

eclipse : Exception raised during rendering: java. util.Locale.toLanguageTag()Ljava/lang/String 中的 Android 渲染 xml 问题

android - 玻璃 : Null pointer exception when trying to add image to Static Card

android - 应该怎么做? (安卓)

c# - Windows 服务可以在未登录的情况下启动应用程序吗?

java - 为什么 "010"等于 8?

java - 迭代时修改集合 (Java)

android - 如何在没有 Gradle 支持的情况下创建新项目?

java - 如果 get volley 请求为 true,则需要从 cardview 中的可绘制对象更新图像(200 ok)