简体   繁体   English

具有可扩展服务的Android库模块

[英]Android library module with extendable service

I need to develop an android library which has a service component. 我需要开发一个具有服务组件的android库。 The service requirements are: 服务要求是:

  • works as a started service (always started) 作为启动服务(始终启动)
  • started and restarted by alarm service when required (another service within the library) 必要时由警报服务启动并重新启动(库中的其他服务)
  • started with the system boot (BOOT_COMPLETED) 从系统启动开始(BOOT_COMPLETED)
  • provide a way to subscribe to certain service events (which should be executed in context of the service, I mean in background as well) 提供订阅某些服务事件的方式(应该在服务的上下文中执行,我的意思也在后台)

The purpose of the service is to provide for any client (android application) which will use it an ability to subscribe for certain events which should be executed in background. 该服务的目的是为任何客户端(Android应用程序)提供订阅某些应该在后台执行的事件的能力。 Dure to the limitation that extended client should receive notification even in background (even when the client is not started) I can see it as an extension for the service (derived service reconfigured at Manifest at the client app). 相当于扩展客户端即使在后台也应该收到通知的限制(即使客户端未启动)我可以将其视为服务的扩展(在客户端应用程序的Manifest上重新配置的派生服务)。

What is the most proper approach to achieve my goal? 实现目标的最恰当方法是什么? How should base service communicate with the client extension? 基本服务应该如何与客户端扩展进行通信?

So first client will have to add this service to their AndroidManifest.xml . 因此,第一个客户端必须将此服务添加到他们的AndroidManifest.xml Then client will call some methods from the library that will start your service or alarm service, depending on your needs. 然后,客户端将根据您的需要从库中调用一些将启动服务或警报服务的方法。

Communication depends on the data you want to send from service to client's app. 通信取决于您要从服务发送到客户端应用程序的数据。 If it's just a notification then the best way would be to use a BroadcastReceiver . 如果它只是一个通知,那么最好的方法是使用BroadcastReceiver If you register a receiver in AndroidManifest.xml it will receive intents even when the app is not started. 如果您在AndroidManifest.xml注册接收器,即使应用程序未启动,它也会收到意图。

If you want to send a lot of objects or call some method on a service you can use ServiceConnection and binder. 如果要发送大量对象或在服务上调用某些方法,可以使用ServiceConnection和binder。

For instance, you need your client to receive a List<Model> from your service even if the client is not started. 例如,即使客户端未启动,您也需要客户端从您的服务接收List<Model> You declare a BroadcastReceiver in AndroidManifest.xml which will receive some intent depending on its intent filter. 您在AndroidManifest.xml声明了一个BroadcastReceiver ,它将根据其intent过滤器接收一些intent。 Then you bind to your service via ServiceConnection and pass a callback object. 然后通过ServiceConnection绑定到您的服务并传递回调对象。 After that you start a background thread inside your service to load data. 之后,在服务中启动后台线程以加载数据。 Once it's done, you call a callback method and your client app will receive the data. 完成后,您将调用回调方法,您的客户端应用程序将接收数据。

Not sure if I answered your question because it's a bit abstract. 不确定我是否回答了你的问题,因为它有点抽象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM