Reputation: 46
To quickly summarize the issue the alarm that is set disappears between when I set it and sometime the next day. My code all seems to work properly, alarms are set and go off at proper times. After the initial alarm fires and completes, it is automatically set for the next day. I can check and see it with adb shell's dumpsys alarm.
update: I have added some more log information just below here
[14/01/2017 20:57:41] Registering start alarm 1 with Alarm Manager event time: 14/01/2017 21:00:00
[14/01/2017 20:57:41] Registering stop alarm 1001 with Alarm Manager event time: 14/01/2017 21:01:00
[14/01/2017 21:00:00] Receiver has received INTENT_ACTION_ALARM_START and is launching the music player for task #1.
[14/01/2017 21:01:00] Receiver has received INTENT_ACTION_ALARM_STOP and is stopping the music player, and sending a restart request to the MusicService.
[14/01/2017 21:01:00] Restart requested on alarm 1, attempting to add new alarms for tomorrow.
[14/01/2017 21:01:00] Registering start alarm 1 with Alarm Manager event time: 15/01/2017 21:00:00
[14/01/2017 21:01:00] Registering stop alarm 1001 with Alarm Manager event time: 15/01/2017 21:01:00
Batch{43d23730 num=1 start=86684689 end=86684689}:
RTC_WAKEUP #0: Alarm{438c1740 type 0 org.hudsoft.music}
type=0 whenElapsed=86684689 when=+23h55m1s357ms window=0 repeatInterval=0 count=0
operation=PendingIntent{43af9370: PendingIntentRecord{43ddd9a8 org.hudsoft.music broadcastIntent}}
Batch{43d1e348 num=1 start=86744688 end=86744688}:
RTC_WAKEUP #0: Alarm{438c10d8 type 0 org.hudsoft.music}
type=0 whenElapsed=86744688 when=+23h56m1s357ms window=0 repeatInterval=0 count=0
operation=PendingIntent{438ea450: PendingIntentRecord{4393a970 org.hudsoft.music broadcastIntent}}
When I go to bed though and wake up and check the next day the Alarm is curiously missing from the adb shell dumpsys output, it also never executes. My question is where does the alarm go to without any user interaction? How can I set an alarm that will persist? The documentation claims that "The Alarm Manager is intended for cases where you want to have your application code run at a specific time, even if your application is not currently running"
My current design is that when the app opens or when BOOT event is received by receiver I run an IntentService that sets all my alarms and goes away, assuming Alarm Manager will handle the rest. My receiver should wait for the response, problem is each night I go to sleep I wake up and my alarm manager rtc wakeup's have mysteriously disappeared.
If you need any other information to help diagnose the issue please let me know this is my first post I have been struggling with and searching for a solution for about a week!
in my receiver this fires to stop the music playing service and also restart a new alarm for the next day:
if (intent.getAction().equals("INTENT_ACTION_ALARM_END")) {
Intent stopIntent = new Intent(context, MusicPlayer.class);
Task thisTask = (Task) intent.getSerializableExtra("thisTask");
stopIntent.putExtra("thisTask", thisTask);
context.stopService(stopIntent);
Intent restartAlarmIntent = new Intent(context, MusicService.class);
restartAlarmIntent.setAction("INTENT_ACTION_ALARM_RESTART");
restartAlarmIntent.putExtra("thisTask", thisTask);
context.startService(restartAlarmIntent);
}
Here is the code I use to build and start the alarms
Intent myIntent = new Intent(this, MusicReceiver.class);
myIntent.setAction("INTENT_ACTION_ALARM_START");
myIntent.putExtra("thisTask", task);
Log.d("Intent", "Sending start intent now");
PendingIntent pendingStartIntent = buildAlarmIntent(task, "INTENT_ACTION_ALARM_START");
setSingleExactAlarm(endTime, pendingStartIntent);
myIntent = new Intent(this, MusicReceiver.class);
myIntent.setAction("INTENT_ACTION_ALARM_END");
myIntent.putExtra("thisTask", task);
Log.d("Intent", "Sending end intent now");
PendingIntent pendingStopIntent = buildAlarmIntent(task, "INTENT_ACTION_ALARM_END");
setSingleExactAlarm(endTime, pendingStopIntent);
public PendingIntent buildAlarmIntent (Task task, String action) {
PendingIntent pendingIntent;
Intent myIntent = new Intent(this, MusicReceiver.class);
myIntent.setAction(action);
myIntent.putExtra("thisTask", task);
Integer idNum = task.getId();
if (action.equals("INTENT_ACTION_ALARM_END")) {
idNum += 1000;
pendingIntent = PendingIntent.getBroadcast(this, idNum, myIntent, PendingIntent.FLAG_UPDATE_CURRENT);
} else {
pendingIntent = PendingIntent.getBroadcast(this, idNum, myIntent, PendingIntent.FLAG_UPDATE_CURRENT);
}
return pendingIntent;
}
private void setSingleExactAlarm(long time, PendingIntent pIntent) {
AlarmManager mAlarmManager;
mAlarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
if (android.os.Build.VERSION.SDK_INT >= 19) {
mAlarmManager.setExact(AlarmManager.RTC_WAKEUP, time, pIntent);
} else {
mAlarmManager.set(AlarmManager.RTC_WAKEUP, time, pIntent);
}
}
Here is the receiver section of my AndroidManifest
<receiver
android:name=".MusicReceiver"
android:enabled="true"
android:exported="true"
android:process=":remote">
<intent-filter>
<action android:name="INTENT_ACTION_ALARM_START" />
<action android:name="INTENT_ACTION_ALARM_END" />
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
Thanks in advance for taking the time to look into my issue!
Upvotes: 2
Views: 681
Reputation: 46
I have found my issue and I thought I would post it in case anyone else runs into a similar issue. My alarms were restarting themselves with the previous times I had set before. Since the time had already passed they would resolve immediately and disappear. This also caused a loop where it would try to restart it again and again but I believe the alarm manager realizes this is happening after events and stops queuing them up to prevent an infinite loop of resetting alarms that have a start time in the past. Hope this helps someone!
Upvotes: 1
Reputation: 459
I do not have enough reputation to leave a comment, as I do not have a definite answer. However, Is there possibility that your phone is powering off? According to the Android documentation:
https://developer.android.com/reference/android/app/AlarmManager.html
"Registered alarms are retained while the device is asleep (and can optionally wake the device up if they go off during that time), but will be cleared if it is turned off and rebooted."
It is difficult to diagnose further without the code behind receiving BOOT_COMPLETED.
In the past I have used the following to set an alarm, and I do not have the issue you have described:
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, MyBroadcastReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, alarmStartTime.getTimeInMillis(), pendingIntent);
I hope this helps, and good luck.
Upvotes: 0