Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android library module with extendable service

I need to develop an android library which has a service component. 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)
  • 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. 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).

What is the most proper approach to achieve my goal? How should base service communicate with the client extension?

like image 895
Alexey Strakh Avatar asked Feb 21 '26 22:02

Alexey Strakh


1 Answers

So first client will have to add this service to their 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. If you register a receiver in AndroidManifest.xml it will receive intents even when the app is not started.

If you want to send a lot of objects or call some method on a service you can use ServiceConnection and binder.

For instance, you need your client to receive a List<Model> from your service even if the client is not started. You declare a BroadcastReceiver in AndroidManifest.xml which will receive some intent depending on its intent filter. Then you bind to your service via ServiceConnection and pass a callback object. 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.

like image 170
Gennadii Saprykin Avatar answered Feb 23 '26 11:02

Gennadii Saprykin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!