从服务通知活动 [英] Notify activity from service

查看:23
本文介绍了从服务通知活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从我的 Activity 启动一个 Service 以查看网页上的更改,这是一个私人应用程序,所以我不会打扰电池生活...

I'm trying to start a Service from my Activity to look out for changes on a web page, it's a private app so I don't bother the battery life...

但我想将数据从我的 Service 传递到我的 Activity...我似乎找不到调用 Activity 的方法 来自我的 Service.我怎样才能做到这一点?

But I'd like to pass data from my Service to my Activity... I can't seem to find a way to call the Activity from my Service. How can I achieve this?

推荐答案

正如 Alex 所指出的,您可以绑定到服务并将某种侦听器或回调传递给服务以用于事件.

As Alex indicated, you can bind to the service and pass some sort of listener or callback to the service to use on events.

或者,您可以使用广播Intent,也许在Intent 上使用setPackage() 之类的方法来限制广播的范围.

Or, you can use a broadcast Intent, perhaps using methods like setPackage() on the Intent to limit the scope of the broadcast.

或者,您可以使用 createPendingResult() 创建一个 PendingIntent,您可以将它作为 Intent 额外传递给服务 -- 服务然后可以使用该 PendingIntent 在您的活动中触发 onActivityResult().

Or, you can use createPendingResult() to create a PendingIntent that you pass as an Intent extra to the service -- the service can then use that PendingIntent to trigger onActivityResult() in your activity.

或者,您可以使用 ResultReceiver.

或者,您可以使用 Messenger.

(诚然,我还没有尝试过后两种方法,但我认为它们可以在这里工作)

(admittedly, I have not tried those latter two approaches, but I think they will work here)

这篇关于从服务通知活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆