Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Developer - Alarm manager vs service

I am making an app that needs to execute a function each hour even the app is closed.

First of all, I thought to create a service, but during my tests, I realise that android sometimes kills my service. So I was looking for another solution and I found AlarmManager. I have implemented it and it seems to work but I have the doubt if it will happen the same the service or it will run forever? (Until reboot of the mobile...)

Another question, it is necessary to create a new thread to execute the process in alarm manager or it runs directly in other thread?

like image 864
Jorge181 Avatar asked Apr 22 '13 22:04

Jorge181


People also ask

What is Android alarm Manager?

android.app.AlarmManager. This class provides access to the system alarm services. These allow you to schedule your application to be run at some point in the future.

Is AlarmManager deprecated?

Interface AlarmManager. Deprecated. Interfaces for alarms and alarm management are replaced by ManagedScheduledExecutorService and related interfaces.

How is AlarmManager different from WorkManager?

Alarms only. Unlike WorkManager, AlarmManager wakes a device from Doze mode. It is therefore not efficient in terms of power and resource management.

How do I use alarm Manager?

This example demonstrates how do I use AlarmManager in android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.


2 Answers

I have implemented it and it seems to work but I have the doubt if it will happen the same the service or it will run forever? (Until reboot of the mobile...)

It will run until:

  • the device is rebooted, as you noted, or
  • the user uninstalls your app, or
  • you cancel the events yourself, or
  • the user goes into Settings, finds your app in the list of installed apps, taps on that entry, and clicks the Force Stop button

It's possible that alarms will need to be scheduled again after your app is upgraded (I forget...).

it is necessary to create a new thread to execute the process in alarm manager or it runs directly in other thread??

Unless the work you are going to do will take only a couple of milliseconds, you will want a background thread for it. That leads to two possible patterns:

  1. If you are not using a _WAKEUP-style alarm, use a getService() PendingIntent to send control to an IntentService every hour

  2. If you are using a _WAKEUP-style alarm, you will need to use a getBroadcast() PendingIntent, and have it either invoke your subclass of my WakefulIntentService, or you will need to manage a WakeLock yourself to keep the device awake while you do your bit of work

like image 80
CommonsWare Avatar answered Oct 12 '22 20:10

CommonsWare


No, Android won't kill scheduled alarms and they got executed as planned unless app is replaced or device is rebooted. Use broadcast receivers for these events to reschedule Alarms. There's no way to prevent Force Stop as it kills all of your app components and threads totally.

That depends on what Alarm Manager do. If it sends a broadcast, the receiver limit is 10 second.

If it starts an Activity, Service or Intent Service, there is no limit. For Activity and Services you must finish or stop it and for Intent Services until the process is finished. Be aware that you can't have another thread inside Intent Service and you'r limited to code inside the OnHandleIntent.

Also you must consider device state. If it's sleep and you are using Wake Up flag receivers won't need a wake lock, but others do. It won't take long for device to go back to sleep.

Don't waste system resources with a service because Alarm Manager do what you want.

like image 32
Ali Avatar answered Oct 12 '22 21:10

Ali