Android Developer - Alarm Manager vs. Service

I am making an application that should execute a function every hour, even if the application is closed.

First of all, I thought about creating a service, but during my tests, I understand that android sometimes kills my ministry. So I was looking for another solution, and I found AlarmManager. I implemented it and it seems to work, but I doubt it will happen with the same service, or will it work forever? (Before rebooting the mobile phone ...)

Another question is, do you need to create a new thread to execute the process in the crash manager, or does it start directly in another thread?

+7
source share
2 answers

I implemented it and it seems to work, but I doubt that this will happen with the same service or it will work forever? (Before rebooting the mobile phone ...)

It will work until:

  • the device restarts, as you noted, or
  • user uninstalls your application or
  • you cancel events yourself, or
  • the user goes to "Settings", finds your application in the list of installed applications, clicks on this entry and clicks the "Power Stop" button

It is possible that after your application is updated, the alarms will need to be scheduled again (I forgot ...).

you must create a new thread to execute the process in the crash manager or it starts directly in another thread

If the work you are about to do takes just a couple of milliseconds, you will need a background thread for it. This leads to two possible patterns:

  • If you are not using the _WAKEUP , use getService() PendingIntent to send an IntentService control every hour

  • If you are using the _WAKEUP alarm, you will need to use getBroadcast() PendingIntent and ask it to call your subclass my WakefulIntentService , or you will need to control WakeLock yourself so that the device does not sleep while you do your work.

+6
source

No, Android will not kill the planned alarms, and they were executed as planned if the application was not replaced or the device rebooted. Use the broadcast receivers for these events to carry the alarms. There is no way to prevent Force Stop, as it completely destroys all components and threads of the application.

It depends on what Alarm Manager does. If it sends a broadcast, the receiver limit is 10 seconds.

If it starts an Activity, Service, or Intent Service, there are no restrictions. For Activity and Services, you must terminate or stop it for Intent Services until the process ends. Keep in mind that you cannot have another thread inside the Intent Service, and you are limited by the code inside OnHandleIntent.

You should also consider the status of the device. If he is sleeping and you are using Wake Up receivers, you will not need tracking lock, but others. The device does not take long for the device to fall asleep again.

Do not waste system resources in the service, because Alarm Manager does what you want.

+3
source

All Articles