androidime

How to write text to my android IME


I want to send scanned text/barcode to arbitrary application using android input method service.

With

activity.dispatchKeyEvent(sendKeyEvent);

I can simulate KeyEvent direct to my activity. But I'm wondering how can I send the events to MyIME (already activated), to serve arbitrary connected activities. I can't find any answer in internet. Can you give some hints?


Solution

  • Answer to my question is pretty simple: use mechanism of broadcast. I was new in android development and thought that I can directly write input to my own IME.

    In this case, I can use LocalBroacastManager like

    LocalBroadcastManger lbm = LocalBroadcastManager.getInstance(this)

    In my Activity reading scanner input:

    Intent intent = new Intent("...");
    //put scanned input data to intent
    lbm.sendBroadcast(intent);
    

    In my IME:

    public void onCreate() {
        lbm.registerReceiver(broadcastReceiver, new IntentFilter("..."))
    }
    
    private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            //get input from intent and send this to inputConnection
        }
    }