3

I have relatively simple setup that should trigger an alarm at certain time of the day and show a notification to user. here is relative code,

Setting the alarm

 long inTime = /*expirationTime*/ Calendar.getInstance().getTimeInMillis() + 10000;
 Intent startIntent = new Intent("parking.event");
 startIntent.setClass(getBaseContext(), ParkingExpirationWarmingBroadcast.class);
 PendingIntent startPendingIntent = PendingIntent.getBroadcast(this, 99, startIntent, 0);


 alarmMgr.setExact(AlarmManager.RTC_WAKEUP,
                inTime,
                startPendingIntent);

BroadcastReceiver registered

    <receiver
      android:name=".modules.parking.ParkingExpirationWarmingBroadcast"
      android:enabled="true">
        <intent-filter>
            <action android:name="parking.event" />
        </intent-filter>
    </receiver>

Broadcast Receiver

class ParkingExpirationWarmingBroadcast : BroadcastReceiver() {

    @SuppressLint("NewApi")
    override fun onReceive(context: Context, intent: Intent) {
    }
}

The receiver is only getting triggered if app is in background. as soon as i swipe the app from multitasking, the notification is cleared and no new Alarms are triggered. I checked this setup on Android 7.0 and BroadcastReceiver is triggered regardless of app running or not.

I am aware regarding restrictions over implicit broadcasts in Android Oreo but i don't believe the intent that i have mentioned above is considered implicit.

Can anyone point out what i am doing wrong?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Ankit
  • 429
  • 3
  • 16

2 Answers2

0

This is a general behavior of any Android's version. If you force-quit an application, then its Alarms and PendingIntents are deleted as well.

You can find the same answer here and here.

payloc91
  • 3,724
  • 1
  • 17
  • 45
  • What i understand is that killing app from multitasking is different than force stopping it. plus, this work as expected on 7.0. alarm is triggred even if i kill app from multitasking. – Ankit Jan 11 '18 at 11:50
  • 1
    @Ankit on many devices, swiping an app from `recent tasks` is the same as `force quitting` it. You may happen to be testing your app on one of those devices. – payloc91 Jan 11 '18 at 12:00
  • i am testing on OnePlus 3T. The default Alarm app seems to be working fine though, even if i kill it. let me find another device with Oreo. – Ankit Jan 11 '18 at 12:12
0

Force closing an app destroys its components . This is what force stop does. It's not a bug, it is very much a feature. Follow the following thread , it has been discussed by android framework engineers . https://groups.google.com/forum/?fromgroups=#!topic/android-developers/anUoem0qrxU

Abhishek Luthra
  • 2,575
  • 1
  • 18
  • 34
  • Well, if you read the question i did not explicitly force quite the app from settings. i just removed it from recent apps(by pressing multi tasking button and swiping left/right). As @Marko Pacak mentioned in his answer above, some device do consider this as force quite. i did a bit of testing and the issue is only with OnePlus Devices. – Ankit Apr 16 '18 at 06:16
  • None of the other apps behaves as expected. calendar wont trigger events or google keep wont pop Reminders set if they aren't in recent apps. – Ankit Apr 16 '18 at 06:18
  • try replacing setExact() with setExactAndAllowWhileIdle() method . Its specified in documentation that old method setExact() doesnt work while device enters doze mode and i think this is what is happening in your case . Follow this link https://developer.android.com/training/monitoring-device-state/doze-standby.html – Abhishek Luthra Apr 18 '18 at 02:17