Android的 - 服务和活动互动 [英] Android - Service and Activity interaction

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

问题描述

我想创建一个应用程序,包含一个服务S和一个活动答:服务S是负责preprocessing,如preparing上的活性的UI显示的数据,在活动之前,一个被调用。

我希望能够从包的外部调用服务S,从另外一个Android应用程序的活动类别B说,做了preprocessing,然后当数据准备好,调用活动A.

我的问题是:

  1. 什么是共享服务S和活动?A
  2. 之间数据的最佳方式
  3. 如何与服务S中的外部b活动通信,以确定它是否已经与所有的preprocessing完成的,活性的是随时可以调用?

谢谢 克里斯

解决方案
  

什么是共享数据的最佳方式   服务S和活动?A

之间

使用的本地绑定模式,并有活动绑定到服务S,然后调用该服务的公开的API来获取什么是必要的。

  

如何才能使外部b活动   与服务S键沟通   确定其是否已与全部竣工   它的preprocessing和活性的   是随时可以调用?

使用远程绑定模式和AIDL。 b活动将注册一个AIDL定义的回调与服务S,该服务将调用适当的时候。请参见 href="http://github.com/commonsguy/cw-advandroid/tree/master/AdvServices/RemoteClientEx/">和的这里一个例子。

I want to create an app that contains a Service S and an Activity A. The Service S is responsible for preprocessing, such as preparing the data shown on the UI of the Activity A, before the Activity A gets invoked.

I want to be able to invoke the Service S from outside the package, say from another Android app's Activity class B, do the preprocessing, and then when the data is ready, invoke Activity A.

My questions are:

  1. What is the best way to share data between the Service S and Activity A?
  2. How can the external activity B communicate with the Service S to determine if it has completed with all its preprocessing, and the Activity A is ready to be invoked?

Thanks Chris

解决方案

What is the best way to share data between the Service S and Activity A?

Use the local binding pattern and have Activity A bind to Service S, then call the service's exposed API to retrieve whatever is needed.

How can the external activity B communicate with the Service S to determine if it has completed with all its preprocessing, and the Activity A is ready to be invoked?

Use the remote binding pattern and AIDL. Activity B would register an AIDL-defined callback with Service S, which the service would invoke when appropriate. See here and here for an example.

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

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