如何拥有Android的服务与活动通信 [英] How to have Android Service communicate with Activity

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

问题描述

我在写我的第一个Android应用程序,并试图让我的头,围绕服务和活动之​​间的通信。我会在后台运行,并做一些GPS和基于时间的日志记录服务。我有一个将被用于启动和停止服务的活动。

I'm writing my first Android application and trying to get my head around communication between services and activities. I have a Service that will run in the background and do some gps and time based logging. I will have an Activity that will be used to start and stop the Service.

因此​​,首先,我需要能够计算出,如果在活动启动的服务正在运行。还有一些其他的问题在这里关于这一点,所以我想我可以明白这一点(但随时提供意见)。

So first, I need to be able to figure out if the Service is running when the Activity is started. There are some other questions here about that, so I think I can figure that out (but feel free to offer advice).

我的真正的问题:如果活动正在运行,服务已启动,我需要一种方法为服务将消息发送到了活动。简单的字符串,并在该点整数 - 主要是状态信息。该消息不会经常发生,所以我不认为查询服务是一个很好的方法去,如果有另一种方式。我只希望这个交流活动时已启动用户 - 我不想开始从服务的活动。换句话说,如果你启动的活动和服务正在运行,你会看到一些状态信息在活动界面时,一些有趣的发生。如果不启动该活动,您将不会看到这些消息(它们是不是很有趣)。

My real problem: if the Activity is running and the Service is started, I need a way for the Service to send messages to the Activity. Simple Strings and integers at this point - status messages mostly. The messages will not happen regularly, so I don't think polling the service is a good way to go if there is another way. I only want this communication when the Activity has been started by the user - I don't want to start the Activity from the Service. In other words, if you start the Activity and the Service is running, you will see some status messages in the Activity UI when something interesting happens. If you don't start the Activity, you will not see these messages (they're not that interesting).

好像我应该能够确定服务正在运行,如果是这样,添加活动作为一个倾听者。然后取出活动作为一个听众,当活动暂停或停止。是实际可能吗?我可以找出这样做的唯一方法是有活动实施Parcelable并建立一个AIDL文件,这样我就可以通过服务的远程接口传递。这似乎有点小题大做,虽然,我不知道该活动应该如何实现writeToParcel()/ readFromParcel()。

It seems like I should be able to determine if the Service is running, and if so, add the Activity as a listener. Then remove the Activity as a listener when the Activity pauses or stops. Is that actually possible? The only way I can figure out to do it is to have the Activity implement Parcelable and build an AIDL file so I can pass it through the Service's remote interface. That seems like overkill though, and I have no idea how the Activity should implement writeToParcel() / readFromParcel().

有没有更简单或更好的办法?感谢您的帮助。

Is there an easier or better way? Thanks for any help.

编辑:

对于任何人谁是有兴趣在此以后,有来自谷歌的样品code通过AIDL在样本目录中处理这样的:/apis/app/RemoteService.java

For anyone who's interested in this later on, there is sample code from Google for handling this via AIDL in the samples directory: /apis/app/RemoteService.java

推荐答案

有三个明显的方式与服务进行通信:

There are three obvious ways to communicate with services:

  1. 使用意图
  2. 使用AIDL
  3. 使用服务对象本身(如单身)

在你的情况,我会使用选项3.去使静态参考服务它自身并填充它的onCreate():

In your case, I'd go with option 3. Make a static reference to the service it self and populate it in onCreate():

void onCreate(Intent i) {
  sInstance = this;
}

请一个静态函数为MyService的getInstance(),它返回静态 sInstance

Make a static function MyService getInstance(), which returns the static sInstance.

然后在 Activity.onCreate()启动该服务,以异步方式等待,直到该服务实际上是启动(你可以有你的服务通知您的应用程序,它已经准备好通过发送意图活动),并得到它的实例。当你有实例,注册服务监听器对象为您服务,您的设置。注:在活动内编辑视图,你应该修改它们在UI线程,该服务可能会运行自己的线程,所以你需要调用 Activity.runOnUiThread()

Then in Activity.onCreate() you start the service, asynchronously wait until the service is actually started (you could have your service notify your app it's ready by sending an intent to the activity.) and get its instance. When you have the instance, register your service listener object to you service and you are set. NOTE: when editing Views inside the Activity you should modify them in the UI thread, the service will probably run its own Thread, so you need to call Activity.runOnUiThread().

您需要做的最后一件事是你在 Activity.onPause()监听器对象以删除引用,否则你的活动范围内的一个实例会泄漏,不不错。

The last thing you need to do is to remove the reference to you listener object in Activity.onPause(), otherwise an instance of your activity context will leak, not good.

请注意:当你的应用程序/活动/任务就是将要访问你的服务的唯一方法这种方法是唯一有用的。如果不是这种情况下,你必须使用选项1或2。

NOTE: This method is only useful when your application/Activity/task is the only process that will access your service. If this is not the case you have to use option 1. or 2.

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

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