5

I have a BroadcastReceiver registered in the manifest which receives notifications from the AlarmManager. This all works great while the app is running i.e. the onReceive() method is being called.

However, if I force-stop the app so that I can test if the BroadcastReceiver will still receive notifications from the AlarmManager while not running, the onReceive() method is never called.

Can force-stop not be used to test the AlarmManager & receiver in this way? There seems to be some responses saying it won't work, and some saying it should.

If not, is there a way of testing this?

CopsOnRoad
  • 237,138
  • 77
  • 654
  • 440
jFort
  • 327
  • 6
  • 17
  • Have you defined your `BroadcastReceiver` on the manifest? – K-ballo May 22 '12 at 17:54
  • @jFort : Are you using one of the `WAKEUP` types for the alarm? If not then that might be the cause but I'm not sure. – Squonk May 22 '12 at 18:02
  • Yes, the broadcast receiver is defined in the manifest. And the alarm manager is defined AlarmManager.RTC – jFort May 22 '12 at 18:54
  • @jFort : Only `RTC`? Not `RTC_WAKEUP`? – Squonk May 22 '12 at 19:15
  • I think RTC_WAKEUP will wake the device but they're only simple reminders so that's not what I'm intending to do. – jFort May 22 '12 at 19:28
  • @jFort : But have you tried `RTC_WAKEUP` to see if it fixes the situation? It's worth a try at least for testing purposes. I can tell you that a correctly registered `BroadcastReceier` will, in fact, be called even if the app has been force-closed. My network state monitoring receiver certainly is. Perhaps alarms are just a different case. – Squonk May 22 '12 at 19:39
  • Ckeck it out [link](http://stackoverflow.com/a/35457280/925504) – Nolesh Feb 17 '16 at 12:52

3 Answers3

3

If your receiver is registered in your manifest, it should always work. Even if your app is force closed.

The problem might be the alarm. The alarms might be getting deleted, once your app force closes.

Kumar Bibek
  • 9,016
  • 2
  • 39
  • 68
  • Thanks for that. If I know it **should** work I'll keep on troubleshooting. Is there a way I can find out what's happening to these alarms? – jFort May 22 '12 at 18:53
  • A crash means, something unexpected happens. So, you should probably re-initialize the alarms when you program recovers. I don't exactly know what happens to the alarms. Sorry – Kumar Bibek May 23 '12 at 16:00
  • 1
    For android > version 3.0, your receiver will stop working when your app is force closed! – Hoang Nguyen Huu Apr 21 '14 at 05:19
  • 1
    As Hoang says, this answer has not been correct for many years, and many Android versions. – Richard Le Mesurier Oct 24 '17 at 21:42
  • 1
    i am not using Alarm Manager i am using call / phone state broadcast receiver i am getting issue is when app is not running at that time call broadcast not getting – Mehul Tank Oct 18 '18 at 06:08
  • 1
    Did you fin\d any solution? I face same issue @MehulTank – Brinda Rathod Nov 19 '18 at 06:50
  • 1
    @BrindaRathod You can add android:enabled="true" android:exported="true" intent-filter android:priority="999" – Mehul Tank Nov 21 '18 at 12:44
1

As per comments on this answer, you cannot test this type of thing by killing the app yourself. You must throw an exception so the app dies "naturally".

Community
  • 1
  • 1
Richard Le Mesurier
  • 29,432
  • 22
  • 140
  • 255
0

May this could be your device problem. In Xiomi, Oppo, Vivo, honor, and Letv mobiles user has to give an autostart permission to the application and for this, you should ask the user to enable this manually you cant done it programmatically

Brinda Rathod
  • 2,693
  • 1
  • 20
  • 32