首页 > 解决方案 > 如何将 InputMethodService 与服务连接?

问题描述

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

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

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

标签: javaandroidservicekeyboard

解决方案


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

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

推荐阅读