如何做一个服务返回的结果为一个活动结果、如何做一个

2023-09-12 04:59:33 作者:梦想不去做永远只是梦

我似乎有一个经典的任务,但我无法找到如何做到这一点的任何例子。

i seem to have a classic task, yet i can't find any examples on how to do it.

我要下载的东西。以及我调用Web服务,并得到回应...但它pretty的大致相同。

i want to download something. well i call a web service and get a response... but its pretty much the same.

为了做到这一点,我有启动的产生一个线程来完成这一工作服务的活动。

in order to do this i have an activity that starts a service that spawns a thread that does the job.

现在我想用我在开始的服务活动得到了数据。

now i would like to use the data i got in the activity that started the service.

(我假设开始另一项活动,以处理显示的结果将是简单的工作)

(i assume that starting another activity to handle the job of displaying the result would be simple)

我的问题是如何服务的通知活动的东西(启动它或者另外一个的)。

my problem is how does the service notify an activity (the one that started it or another one) of something.

任何指针的例子是很多AP preciated。

any pointers to examples are much appreciated.

关于

推荐答案

发送广播意图通过sendBroadcast()的数据,该活动挑选了一个BroadcastReceiver。

Send a broadcast Intent with the data via sendBroadcast(), that the activity picks up with a BroadcastReceiver.

下面是一个例子: https://github.com/ commonsguy / CW-的Andr​​oid /树/主/服务/ WeatherAPI 文档: http://developer.android.com/reference/android/content/BroadcastReceiver.html

Here's an example of that: https://github.com/commonsguy/cw-android/tree/master/Service/WeatherAPI Doc: http://developer.android.com/reference/android/content/BroadcastReceiver.html