From the Android design point of view, it is not desirable that there is code running always in the background, because this prevents the CPU from sleeping, notably increasing the use of the battery (because you have to use a wake lock
for that the code keeps running).
On the other hand, there is no mechanism that guarantees that a service is running permanently (perhaps using the awful sticky ), since the services are tied to the applications, the user can always kill the service from a task manager.
That is why the approach you are currently using is the most suitable:
You do not have code making wake lock
of the CPU permanently.
While the onReceive()
method is running you are guaranteed the wake lock
.
There will be no service to be closed by the user, so you will have more guarantees that your code will be executed.
Keep in mind that you have to re-subscribe the alarms if the device restarts, although this is something that you have to control also with the other implementation.
More information about wake lock
here