Office Hours — Today, September 19

Thursday, September 15

Mark M.
has entered the room
Mark M.
turned on guest access
Sep 19
7:35 PM
Scott B.
has entered the room
Mark M.
hello, Scott!
how can I help you today?
Scott B.
Good evening, Mark
I've been developing an Android Wear app, and I'm having trouble with AlarmManager. I'm trying to get my app to fire off alarms based on schedules setup by the user. I'm using AlarmManager and finding the darn thing just won't fire at the right time. A 6:30pm alarm goes off at 6:42pm. A 7:20pm alarm might fire at 7:26pm. etc
7:40 PM
Scott B.
Any idea how to troubleshoot this? For what its worth, I've implemented alarms before on Android phones and never really had this problem.
Mark M.
conversely, I've never written a Wear app
Scott B.
Yeah, I had a feeling you would say that :)
Mark M.
my guess is that Wear has an equivalent of Doze mode
or something else that may be batching alarms
I don't know how well the standard tools work with Wear, but you might use adb shell dumpsys alarm
just to confirm your alarms are actually getting scheduled for the right time
what sort of PendingIntent are you using for the AlarmManager event?
Scott B.
Yes - I believe they are being set for the right time.
View paste
Intent intent = new Intent(context, AlarmReceiver.class);
        alarmIntent = PendingIntent.getBroadcast(context, num, intent, 0);
        
        Log.i(LOG_TAG,  "Setting alarm for " + calendar.getTime().toString());
        alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
                AlarmManager.INTERVAL_DAY, alarmIntent);
LogCat:
View paste
09-19 19:11:37.268 3178-3178/ I/AlarmHelper: Setting alarm for Mon Sep 19 19:14:00 EDT 2016
09-19 19:19:02.791 3178-3178/ I/AlarmReceiver: on Receive
Mark M.
AlarmReceiver is a WakefulBroadcastReceiver?
Scott B.
..no. extends BroastcastReceiver
7:45 PM
Mark M.
well, actually, in this case, that's not the problem
assuming that your second LogCat message is in onReceive(), then you're definitely not getting called at the right time
Scott B.
yes - that output is the first line of onReceive()
Mark M.
and so I'm back to it being something Doze-ish
what is your targetSdkVersion
?
Scott B.
So the funny thing is I actually have an Alarm app on the Playstore for phones, and it works great for all devices (including Doze). Never had a problem with it not waking up.
Mark M.
because setRepeating() is inexact on API Level 19+ if your targetSdkVersion is 19+
Scott B.
targetSdk is 23
I've also tried setInexactRepeating with similar results
Mark M.
try set() and manual rescheduling
sorry, setExact()
(or setWindow() if you can deal with some minor amount of jitter)
Scott B.
I believe thats probably the route I will take. Thats exactly what my alarm app uses. It does setExact(), and when that fires, it sets the next alarm.
7:50 PM
Mark M.
Wear is API 20 or higher, so setExact() should exist
Scott B.
yes, the min SDK is 21 for this project
Thankfully, don't have to worry about older APIs in this case
Thanks for the help.
Mark M.
there's also setAlarmClock(), which is even Doze-resistant, but I don't know what sort of UI that will add to the Wear experience, if any
Scott B.
Hmm. I might give that a try and see what that does.
Mark M.
on phones, it'll add stuff to the status bar and notification shade that may not be desirable
7:55 PM
Scott B.
Yeah, I wouldn't mind that on Wear if the user sees an upcoming alarm for my app. The pain part of doing this change is I have to determine when the next alarm is firing, and schedule that. But, if thats what it takes to make it reliable, I'm all in.
7:55 PM
Mark M.
the joys of doing periodic work on Android :-)
Scott B.
Yep. Thanks again for the help. I just bought your subscription for the purpose of this Office Hours.
Mark M.
hopefully you'll get more use out of it than just this one chat...
Scott B.
I'll have to check out your book, I know you've put a lot of work into it, and I've seen your github samples. It's a major bargain.
Have a good evening.
Mark M.
you too!
Scott B.
has left the room
8:30 PM
Mark M.
turned off guest access

Thursday, September 15

 

Office Hours

People in this transcript

  • Mark Murphy
  • Scott Bradshaw