Android Alarmmanager Not Working on Some Devices When the App Is Closed

Android - AlarmManager is not working after app is closed

Use instead the androidx WorkManager library, is the replacement for all scheduling services.

The WorkManager API is a suitable and recommended replacement for all previous Android background scheduling APIs

https://developer.android.com/topic/libraries/architecture/workmanager

What the WorkManager does is to wrap all the existing scheduling services, and use the most appropriate one according to what is available, API level, etc., even taking care of compatibility issues and system bugs.

Some tutorials:

https://medium.com/androiddevelopers/introducing-workmanager-2083bcfc4712

https://www.programmersought.com/article/82731596284/

https://medium.com/swlh/periodic-tasks-with-android-workmanager-c901dd9ba7bc

AlarmManager does not work when the app is closed. Why is that?

Based on your comments it looks like the alarm is triggering as it should, but your BroadcastReceiver is failing when trying to start your Service. The most likely reason is that your device has additional restrictions on background processing. Some devices, mostly low-end and Chinese manufacture, have restrictions on what apps are allowed to run "in the background". If your app isn't on a "white list" of allowed apps, Android won't allow components of the app to be launched in the background.

See https://developer.android.com/about/versions/oreo/background to read about background Service limitations and see https://stackoverflow.com/a/45482394/769265 as an example of an answer where I discuss the problem of certain manufacturers limiting background processing.


Note:
Since you cannot start a background Service on Android 8 and above (due to the limitations), you can start your Service as a foreground Service, like this:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
context.startForegroundService(intent)
} else {
context.startService(intent)
}


Related Topics



Leave a reply



Submit