Send message from Activity to Service - Android

有些话、适合烂在心里 提交于 2019-11-28 12:01:06

问题


I am writing a keyboard replacement app for Android, and I needed the keyboard customized enough that I need to run it in an Activity, instead of keeping it in the InputMethodService class. Here is how I call the keyboard from my InputMethodService class:

    @Override public void onStartInputView(EditorInfo attribute, boolean restarting) {
    super.onStartInputView(attribute, restarting);

    Intent intent = new Intent(this, Keyboard.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
    context.startActivity(intent);

}

I have now run into the problem that I can't update the text field where the input from the keyboard should go. I tried creating a static InputConnection, in my service class, then updating it from the Activity, but nothing happens.

So I guess here is my question: I was able to find lots of info about how to send data from a Service to an Activity, but nothing about sending data from an Activity to a Service(specifically an input method service). Does anyone know how to do this?


回答1:


Use a Binder for that.

This tutorial about music player service is a good sample http://www.helloandroid.com/tutorials/musicdroid-audio-player-part-ii

Especially this line

mpInterface.addSongPlaylist(file.getName());


来源:https://stackoverflow.com/questions/8169749/send-message-from-activity-to-service-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!