如何从服务发送消息到活动 [英] how to send message from service to activity
本文介绍了如何从服务发送消息到活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我构建创建服务的AsyncTask的,现在我想从服务发送到AsyncTask的消息。
在AsyncTask的我的code是:
I build an AsyncTask that create a service and now I want to sent from service to AsyncTask message. my code on the AsyncTask is:
class ResponseHandler extends Handler {
public void handleMessage(Message message) {
// Toast.makeText(this, "message from service",Toast.LENGTH_SHORT).show();
}
希望它能够处理来自服务的消息纠正我,如果我错了。
hope that it will handle the message from service correct my if I wrong.
和从服务中试图做到这一点。
and from service tried to do this
Message message = Message.obtain(null, MyService.ADD_RESPONSE_HANDLER);
message.replyTo = messenger;
try {
myService.send(message);
catch (RemoteException e) {
e.printStackTrace();
}
但我的错误是无法找到行代码:
but my errors are cannot find symbol in lines:
MyService.ADD_RESPONSE_HANDLER
message.replyTo = messenger;
try {
myService.send(message);
我需要什么补充?请给我一个code,将做的工作。非常感谢。
What do I need to add? Please give me a code that will do the work. thanks a lot.
推荐答案
一种方法是使用 ResultReceiver
。这是我最近张贴着为例我的完整的博客文章。
One way is using ResultReceiver
. Here is my complete blog post which I had recently posted with an Example.
这篇关于如何从服务发送消息到活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文