Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

My android alarms are being cancelled, and not by me

My app uses an alarm timer to check for updates on a server every 2 minutes or so. I create a recurring timer using this:

    alarmIntent = new Intent(context, OnWakeUpReceiver.class);
    PendingIntent pIntent = PendingIntent.getBroadcast(context, 0, alarmIntent, 0);

    mAlarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 120000, pIntent);

For the vast majority of people, this works just fine. But a small number of people report that the app suddenly stops updating itself. I put in a simple check that sees if the timer exists:

    PendingIntent pIntent = PendingIntent.getBroadcast(context, 0, alarmIntent, PendingIntent.FLAG_NO_CREATE);   

and sure enough, when the app is no longer updating, this return null, indicating no timer still exists.

My app had been controlling the alarm timer (turning it off when there was no network connection, then turning it back on when there was, and other scenarios). I thought perhaps I was somehow turning it off and not turning it back on. So I created a version where I removed every call to cancel the alarm timer. So my app no longer has any means to cancel that timer. But after just a few days time, once again the alarm timer is no longer present, and the app is not updating.

I haven't been able to get this to happen on my own systems, or have someone find a foolproof way to repeat it on theirs. I wondered if maybe the Android system was cancelling it (though that would stop my app from ever working again), but on one of the more problem systems, he said he hardly has anything running on the phone.

I didn't know if task killers might kill alarm timers as well, but I understood that after I think SDK 8, task killers couldn't do that anymore anyway, and I've been having problems with post version 8. And also on systems that do not run task killers, and have not been rooted.

I even created a "watchdog" alarm timer that ran a receiver just to check and see if the main app timer had stopped updating. What I found is that THAT timer was being cancelled as well (it gave no further "last ran" updates, and never noticed the main app had stopped).

This problem is an app killer for me. Can anyone suggest any way even to try and debug when and what is happening? Is there any log entry made ever by the system when a timer gets cancelled, whether by the system or something else? I hate that it just evaporates away without a trace.

like image 897
Rob Avatar asked Nov 13 '22 08:11

Rob


1 Answers

I can't say this is the complete answer, but these 2 changes together seem to have stopped MUCH of the cancelled alarms:

1) I was taking the pending intent and cancelling that, as opposed to calling cancel() on the alarm manager when I had to cancel the alarm for whatever reason. Over time, I don't know if that managed to confuse the alarm manager or something, but it probably wasn't good. Now I cancel the alarm manager as well as the pending intent.

2) One of my users mentioned how when they hit Clear RAM, it disables my service. Apparently some phones come with a task manager that gives them a nice shiny button that lets them clear out memory/RAM. The button even says clearly that pressing it will cause some apps to stop working...and still the users mash it. I don't know how exactly, but this appears to kill my alarms MOST of the time. So I put out a big notice for people to stop doing that, and since then things have really quieted down. The sucky side is that I won't ever really know the next time I get a disabled alarm if the user hit the magic button of death, or if something really went wrong.

But either way, things are better now.

like image 197
Rob Avatar answered Nov 16 '22 03:11

Rob