Note: I tried various solutions that are written about here on StackOverflow (example here). Please do not close this without checking if your solution from what you\'ve fou
Intent.FLAG_RECEIVER_FOREGROUND
flag.https://developer.android.com/about/versions/oreo/background#broadcasts
Intent intent = new Intent(context, Receiver.class);
intent.setAction(action);
...
intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
PendingIntent operation = PendingIntent.getBroadcast(context, 0, intent, flags);
setExactAndAllowWhileIdle()
when targeting API 23+.if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, time, operation);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
alarmManager.setExact(AlarmManager.RTC_WAKEUP, time, operation);
} else {
alarmManager.set(AlarmManager.RTC_WAKEUP, time, operation);
}
https://developer.android.com/about/versions/oreo/background#migration
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
context.startForegroundService(intent);
} else {
context.startService(intent);
}