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!
|
Mark M. |
how can I help you today?
|
Scott B. |
Good evening, Mark
|
Scott B. |
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
|
Sep 19 | 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
|
Mark M. |
or something else that may be batching alarms
|
Mark M. |
I don't know how well the standard tools work with Wear, but you might use adb shell dumpsys alarm
|
Mark M. |
just to confirm your alarms are actually getting scheduled for the right time
|
Mark M. |
what sort of PendingIntent are you using for the AlarmManager event?
|
Scott B. |
Yes - I believe they are being set for the right time.
|
Scott B. |
View paste
|
Scott B. |
LogCat:
|
Scott B. |
View paste
|
Mark M. |
AlarmReceiver is a WakefulBroadcastReceiver?
|
Scott B. |
..no. extends BroastcastReceiver
|
Sep 19 | 7:45 PM |
Mark M. |
well, actually, in this case, that's not the problem
|
Mark M. |
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
|
Mark M. |
what is your targetSdkVersion
|
Mark M. |
?
|
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
|
Scott B. |
I've also tried setInexactRepeating with similar results
|
Mark M. |
try set() and manual rescheduling
|
Mark M. |
sorry, setExact()
|
Mark M. |
(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.
|
Sep 19 | 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
|
Scott B. |
Thankfully, don't have to worry about older APIs in this case
|
Scott B. |
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
|
Sep 19 | 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.
|
Sep 19 | 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.
|
Scott B. |
Have a good evening.
|
Mark M. |
you too!
|
Scott B. | has left the room |
Sep 19 | 8:30 PM |
Mark M. | turned off guest access |