将消息从活动发送到服务 - Android [英] Send message from Activity to Service - Android
问题描述
我正在为 Android 编写一个键盘替换应用程序,我需要足够定制的键盘,以便我需要在 Activity 中运行它,而不是将它保留在 InputMethodService
类中.下面是我从 InputMethodService
类调用键盘的方法:
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);
}
我现在遇到了一个问题,我无法更新应该从键盘输入的文本字段.我尝试在我的服务类中创建一个静态 InputConnection,然后从 Activity 更新它,但没有任何反应.
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?
推荐答案
为此使用 Binder.
Use a Binder for that.
这个关于音乐播放器服务的教程是一个很好的例子http://www.helloandroid.com/tutorials/musicdroid-audio-player-part-ii
This tutorial about music player service is a good sample http://www.helloandroid.com/tutorials/musicdroid-audio-player-part-ii
尤其是这一行
mpInterface.addSongPlaylist(file.getName());
这篇关于将消息从活动发送到服务 - Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!