I am having a problem with the Alarmmanager functions for the Android.
The problem is alarms that have over an hour or so to wait fail to go off.
My application initially creates an alarm like so:-
PendingIntent sender = PendingIntent.getBroadcast(this, 192837, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, mCal.getTimeInMillis(), sender);
When the alarm goes off it triggers my RecieverHandler class, specifically this function:-
public void onReceive(Context context, Intent intent)
{
try {
Bundle bundle = intent.getExtras();
Intent newIntent = new Intent(context, MessageDispatcher.class);
newIntent.putExtras(bundle);
// newIntent.addFlags(Intent.FLAG);
context.startService(newIntent);
} catch (Exception e) {
Toast.makeText(context, "There was an error somewhere, but we still received an alarm", Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
}
This then launches a service by the name of MessageDispatcher and this function is called:-
public int onStartCommand(Intent intent, int flags, int startId)
This function gets the next alarm time from my Database, this I am sure is working correctly, it then sets a new alarm based on the date from the database like so:-
PendingIntent sender = PendingIntent.getBroadcast(this, 192837, newIntent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, mCal.getSendAt().getTimeInMillis(), sender);
This creates the alarm for the next message.
I have tested this over a short amount of time and it appears to work and have tested it over large amounts of time by changing my date and time within the phone. It appears to fire off successfully.
Then when this alarm goes off it gets the next alarm to go off and schedules this. I am nearly 100% sure these parts are working fine.
So I am stuck with only some theories of why it’s not working.
I thought it might be related to me disconnecting the phone from the debugger but the alarm seems to work over short amounts of time in that case.
So my main theory is that the alarmmanager I am creating is being deleted after a certain amount of time? If this is true this a big problem since I need this to be working no matter how much time has passed.
Any help into ensuring my alarm remains is greatly appreciated, thanks.
Alarms that are registered remain registered until you cancel them, or until the next reboot, or until the user kills your application with a "task killer" on Android 2.1 and earlier.
You have not indicated:
BroadcastReceiver
is doingWithout that information, it is impossible to say where you are going wrong.
Make sure that either you are doing all your work in the
BroadcastReceiver
(if the work is very quick) or that you are maintaining your ownWakeLock
as you pass control to theIntentService
that is doing the rest of the work. Check outWakefulIntentService
for more.Also, you might try to create unique
Intents
per alarm, rather than updating the current one in place. I am not aware that there is a particular problem here, but it makes me nervous.