Android library module extensible

I need to develop an android library that has a service component. Service requirements:

  • runs as a running service (always starts)
  • started and restarted by the alert service as needed (another service in the library)
  • started with system boot (BOOT_COMPLETED)
  • provide a way to subscribe to specific service events (which must run in the context of the service, I mean in the background too).

The purpose of the service is to provide any client (Android app) that will use it to subscribe to certain events that need to run in the background. Dure to the limitation that the extended client needs to be notified even in the background (even when the client doesn't start), I can see it as an extension for the service (the derived service is reconfigured in the Manifest in the client application).

What is the most correct approach to achieve my goal? How do I associate a basic service with a client extension?

+3


source to share


1 answer


Thus, the first client will have to add this service to theirs AndroidManifest.xml

. The client will then call some methods from the library that will start the service or alarm service, depending on your needs.

The communication depends on the data that you want to send from the service to the client application. If it's just a notification, the best way would be to use BroadcastReceiver

. If you register the receiver with AndroidManifest.xml

, it will receive intents even when the application is not running.

If you want to send many objects or call some method on the service, you can use ServiceConnection

binder as well.



For example, you need your client to receive List<Model>

from your service, even if the client is not running. You declare BroadcastReceiver

in AndroidManifest.xml

, which will receive a specific intent depending on the intent filter. Then you bind to your service via ServiceConnection

and pass the callback object. After that, you start a background thread inside your service to load data. After that, you call the callback method and your client application will receive the data.

Not sure if I answered your question because it is a bit abstract.

+1


source







All Articles