Search code examples
javaandroidservicekeyboard

How to connect InputMethodService with a Service?


I want to connect my custom Softkeyboard, which is expanding the InputMethodService, with another Service. I need to call a method in my Softkeyboard from a generic Service.

The problem is that i can not extend the Binder class because the method onBind() is defined final in the AbstractInputMethodService...

now, how can I call a method in my SoftKeyboard class from other service ?


Solution

  • I've solved this by using a broadcast receiver class.

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

    then I sent a broadcast to my service.

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