I am trying to run IntentService as such from AlarmManager setRepeating() for every half an hour. I want to send a to broadcast, from broad cast to intent service. In the service, some functionality will be done.

But, initially the AlarmManager in not triggering while the app is in closed state.

When my app is running or in background state the alarm is working fine and when I close the app the alarm is not working in some devices.

What should I do to run the alarm even if the app is closed?

Solution 1

From the documentation of setRepeating():

As of API 19, all repeating alarms are inexact.

Moreover, setRepeating() does not work with Doze.

You should use exact alarms (set by the appropriate AlarmManager method based on the API level of the device):

if (Build.VERSION.SDK_INT >= 23) {
    alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP,
            triggerTime, pendingIntent);
} else if (Build.VERSION.SDK_INT >= 19) {
    alarmManager.setExact(AlarmManager.RTC_WAKEUP, triggerTime, pendingIntent);
} else {
    alarmManager.set(AlarmManager.RTC_WAKEUP, triggerTime, pendingIntent);
}

And reschedule them every time they fire.

For the rescheduling you could add the original trigger time to the Intent:

intent.putExtra(KEY_TRIGGER_TIME, triggerTime);

Then retrieve this extra in onReceive(), add your desired interval to it and reschedule the alarm using the new value:

@Override
public void onReceive(Context context, Intent intent) {
    long triggerTime = intent
            .getLongExtra(KEY_TRIGGER_TIME, System.currentTimeMillis());

    // adding one day to the current trigger time
    triggerTime += TimeUnit.DAYS.toMillis(1);

    // set a new alarm using the new trigger time
    // ...
}

NOTE: As @Opiatefuchs mentioned in the comment above, some manufacturers (such as Xiaomi or Huawei) may implement certain battery saver functions that can prevent alarms from being fired and cannot be bypassed programmatically.

Solution 2

Nowadays devices are coming with more security in context of Battery power consumption. By default devices keep almost all apps in power saving mode. It means in some devices your background work (Location, Alarm manager) won't work as soon as you come out from the app. In other devices background tasks won't work after a battery threshold limit (like 13%). So you need to keep out your app from this battery saving mode to run your app smoothly even in background. The way to achieve that behavior in these two manufacturers is:

Xiaomi

  • Go to the Battery => Power => App battery Saver => select your app and choose No restrictions (for Background settings), then Allow option for Background location.

  • To AutoStart your app after Boot: Go to the Security app => Permissions => Auto start and check your app.

Samsung

Samsung Smart Manager App used to stop all background work after 3 days if you don't come to your app. So the way to disable this feature is:

  • Go to Battery in the Settings => Unmonitored apps => Add your app to the whitelist. Some other Samsung versions may differ the place to disable it, like Battery => Detail => Select the app and "Don't optimize".

For other devices there should be same power options either in settings option directly or some app are given to handle it.

Solution 3

First, there is a bug with android studio. If you start the app from the android studio and then swipe it away from the recents, the alarms will be deleted. So after that, relaunch your app by clicking on the launcher Icon and then if you swipe it away, the alarm will be still there.

Second, on some devicrs with battery optimization stuff, you should start a foreground service and that works totally fine.

I could make it work using these two points and now it works like a charm.

Solution 4

The below behavior changed after a full charge. Previously, after disabling energy saving for this app, it displayed that there was no battery saving active etc, but only after a full charge (from very low battery state) did the device behave as it should. So change those settings for the app, then do a full recharge (maybe only after previously having low battery). This could fix it.


ZTE Blade L110

Even with the battery saver disabled (Settings -> Battery -> Options -> Battery Saver) and the app marked as important for messages (Settings -> Prompt & notification -> App notifications -> appname -> Priority) it seems like neither setExact nor setAlarmClock are triggering on time.

Solution 5

for Lenovo phone:-

you need to go in setting -> application->select app-> uncheck Restrict to launch

now it will be work in the background as killed state