我正在编写我的第一个Android应用程序,试图弄清楚服务和活动之间的通信。我有一个服务,将在后台运行,并做一些gps和基于时间的日志记录。我将有一个活动,将用于启动和停止服务。

因此,首先,我需要能够确定当活动启动时服务是否正在运行。这里还有一些其他的问题,所以我想我可以解决(但请随意提供建议)。

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).

似乎我应该能够确定服务是否正在运行,如果是的话,将活动添加为侦听器。然后在Activity暂停或停止时删除Activity作为监听器。这真的可能吗?我能想出的唯一方法是让活动实现Parcelable并构建一个AIDL文件,这样我就可以通过服务的远程接口传递它。这似乎是多余的,但我不知道活动应该如何实现writeToParcel() / readFromParcel()。

有没有更简单或更好的方法?谢谢你的帮助。

编辑:

对于稍后对此感兴趣的人来说,在示例目录:/apis/app/RemoteService.java中有来自谷歌的通过AIDL处理此问题的示例代码


当前回答

To follow up on @MrSnowflake answer with a code example. This is the XABBER now open source Application class. The Application class is centralising and coordinating Listeners and ManagerInterfaces and more. Managers of all sorts are dynamically loaded. Activity´s started in the Xabber will report in what type of Listener they are. And when a Service start it report in to the Application class as started. Now to send a message to an Activity all you have to do is make your Activity become a listener of what type you need. In the OnStart() OnPause() register/unreg. The Service can ask the Application class for just that listener it need to speak to and if it's there then the Activity is ready to receive.

通过应用程序类,你会看到比这个更多的战利品。

其他回答

To follow up on @MrSnowflake answer with a code example. This is the XABBER now open source Application class. The Application class is centralising and coordinating Listeners and ManagerInterfaces and more. Managers of all sorts are dynamically loaded. Activity´s started in the Xabber will report in what type of Listener they are. And when a Service start it report in to the Application class as started. Now to send a message to an Activity all you have to do is make your Activity become a listener of what type you need. In the OnStart() OnPause() register/unreg. The Service can ask the Application class for just that listener it need to speak to and if it's there then the Activity is ready to receive.

通过应用程序类,你会看到比这个更多的战利品。

使用LocalBroadcastManager注册一个接收器来监听本地服务在你的应用内发送的广播,参考如下:

http://developer.android.com/reference/android/support/v4/content/LocalBroadcastManager.html

正如Madhur提到的,您可以使用总线进行通信。

在使用总线的情况下,您有一些选择:

Otto事件总线库(已弃用,支持RxJava)

http://square.github.io/otto/

绿色机器人的EventBus

http://greenrobot.org/eventbus/

NYBus (RxBus,使用RxJava实现。非常类似于EventBus)

https://github.com/MindorksOpenSource/NYBus

使用信使是在服务和活动之间进行通信的另一种简单方法。

在Activity中,创建带有相应Messenger的Handler。这将处理来自服务的消息。

class ResponseHandler extends Handler {
    @Override public void handleMessage(Message message) {
            Toast.makeText(this, "message from service",
                    Toast.LENGTH_SHORT).show();
    }
}
Messenger messenger = new Messenger(new ResponseHandler());

Messenger可以通过将其附加到Message来传递给服务:

Message message = Message.obtain(null, MyService.ADD_RESPONSE_HANDLER);
message.replyTo = messenger;
try {
    myService.send(message);
catch (RemoteException e) {
    e.printStackTrace();
}

在API演示中可以找到完整的示例:MessengerService和MessengerServiceActivity。关于MyService如何工作,请参阅完整的示例。

另一种方法是通过活动和服务本身使用带有假模型类的观察者,实现MVC模式变化。我不知道这是否是最好的方法,但这种方法对我来说很有效。如果你需要一些例子,我会张贴一些东西。