12

Since the introduction Doze Mode and App StandBy managing alarms have changed. The problem I'm facing is my alarm manager fires correctly on KitKat, Lolipop and Marshmellow devices but above API 23 it does not fire unless the app is in foreground or background. But if the app is killed, the alarms are stopped.

Checked out Google Keep Application on my Android 7, turns out it does the same.

But Google Calendar fires regardless of whether the app is killed or not.

Done some reading and found out setExactAndAllowWhileIdle method on the alarm manager ensures to break the doze mode and trigger your alarm.

But it does not work, is there anything I'm missing here?

Here's my code:

Intent alertIntent = new Intent(this, NotificationPublisher.class);

    alertIntent.putExtra("Hello", "Meal Time");

    AlarmManager alarmManager = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);

    int id = (int) System.currentTimeMillis();

    if (Build.VERSION.SDK_INT < 23) {
        if (Build.VERSION.SDK_INT >= 19) {
            alarmManager.setExact(AlarmManager.RTC_WAKEUP, delay, PendingIntent.getBroadcast(this, id, alertIntent, PendingIntent.FLAG_UPDATE_CURRENT));
        } else {
            alarmManager.set(AlarmManager.RTC_WAKEUP, delay, PendingIntent.getBroadcast(this, id, alertIntent, PendingIntent.FLAG_UPDATE_CURRENT));
        }
    } else {
        alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, delay, PendingIntent.getBroadcast(this, id, alertIntent, PendingIntent.FLAG_UPDATE_CURRENT));
    }

Broadcast Receiver:

public class NotificationPublisher extends WakefulBroadcastReceiver {


@Override
public void onReceive(Context context, Intent intent) {
    String message = intent.getStringExtra("Hello");
    Log.d("Called", "Meal Time");
}

Manifest:

    <uses-permission android:name="android.permission.WAKE_LOCK" />
        <receiver android:name=".NotificationPublisher" />
Veeresh Charantimath
  • 4,641
  • 5
  • 27
  • 36
  • Please explain, in precise technical terms, what "the app is killed" means. For example, if you are "killing" the "app" yourself, how are you doing this? `adb shell`? From the Android Monitor in Android Studio? By swiping a task off of the overview screen? By some device-supplied task manager? Something else? If it is any of the latter three, what is the device model that you are testing on? Alarms get wiped out on a "force stop", but not for normal process termination. – CommonsWare Jun 28 '17 at 11:05
  • By swiping a task off the overview screen, I'm testing it on One plus 3T running Android 7.1.1 – Veeresh Charantimath Jun 28 '17 at 16:26
  • Presumably, OnePlus tied the swipe-off-the-overview-screen to a "force stop" operation. You can try to confirm this by running your app, swiping it off the overview screen, then visiting the app's page in Settings and seeing if "Force Stop" is enabled or not. If this is what OnePlus has done, there is absolutely nothing you can do about it. – CommonsWare Jun 28 '17 at 16:29
  • Force stop was enabled when I swiped the app off the screen and checked settings – Veeresh Charantimath Jun 28 '17 at 16:32
  • OK. Next test: run your app, at least to the point where you have scheduled your alarm. Run `adb shell dumpsys alarm` on the development machine and see if your alarm is scheduled (it should be, if it has been working outside this swipe scenario). Then, swipe the app off of the overview screen, then run `adb shell dumpsys alarm` again. If the alarm is now gone, they must have rigged up their own way to cancel outstanding alarms without a full "force stop", and there may be workarounds. – CommonsWare Jun 28 '17 at 16:35
  • The logs are intact even after swiping the app off the screen - +177ms 1 wakes 1 alarms, last -3m32s489ms: *walarm*:droid.veeresh.jobscheduling/.NotificationPublisher – Veeresh Charantimath Jun 28 '17 at 16:56
  • OK, I'm stumped. I have no idea why your receiver would not be triggered by `AlarmManager`, since it appears that your alarm is still outstanding. – CommonsWare Jun 28 '17 at 17:16
  • 1
    Ran the app on Google Pixel running 7.1.1 same as OnePlus 3T, worked fine with Pixel. Since a large number of our consumers may have OnePlus 3T, is there anything I can do to fix it? – Veeresh Charantimath Jun 29 '17 at 18:19
  • Since I have no idea what the underlying problem is, I have no idea how to fix it. – CommonsWare Jun 29 '17 at 18:33
  • 1
    Were you able to find a solution to this? – CoderP Aug 11 '17 at 07:34
  • No, the problem persists! Also, I recently found that a "started service" does not survive when the app is swiped. – Veeresh Charantimath Aug 11 '17 at 10:06
  • Because of battery optimisation it is not working,i turned off the battery optimisation of particular app , it is working fine in oneplus 3 – Brahmam Yamani May 11 '18 at 10:57
  • You wanna white-list your app from battery optimisation, check this link https://stackoverflow.com/a/42651399/3752079 – Brahmam Yamani May 11 '18 at 11:31
  • @all you need to remove app from battery optimization it works like charm – Ravi Jun 22 '20 at 10:39

2 Answers2

7

Because of battery optimisation it is not working,i turned off the battery optimisation of particular app, it is working fine in oneplus 3.

You wanna white-list your app from battery optimisation programatically, check this link stackoverflow.com/a/42651399/3752079

Brahmam Yamani
  • 4,475
  • 5
  • 21
  • 30
-1

Try with adding below line for your alertIntent myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0,myIntent, 0);

Harshal Shah
  • 427
  • 3
  • 5