Office Hours — Today, January 21

Tuesday, January 19

Jan 21
8:25 AM
Mark M.
has entered the room
Mark M.
turned on guest access
8:45 AM
Grigore C.
has entered the room
Grigore C.
Hello!
Mark M.
hello, Grigore!
how can I help you today?
Grigore C.
I'm having a hard time with AlarmManager
Mark M.
you and everyone else :-)
Grigore C.
I'm trying to set a reminder, but I guess it's too often for the Android OS
Sometimes it works sometimes it doesn't
Mark M.
the "repeating" methods have a minimum repeat interval of 15 minutes, if I recall correctly
Grigore C.
I've read about this yesterday in your book, they seem to work fine while app is in foreground
Mark M.
also, Doze mode and related power management stuff may cause alarms to be delayed
Grigore C.
but doze mode delays them
yes
But how does whatsapp always "wake up" the screen?
Mark M.
WhatsApp is almost certainly not using AlarmManager
I would expect them to be using Firebase Cloud Messaging (FCM), on Google Play ecosystem devices
Grigore C.
I get some notifications that even on Android 11 wake up my device
Yes, I read something about that, they might use FCM
8:50 AM
Grigore C.
So I need a 1 minute reminder no matter the doze mode.. what should I do? switch to FCM?
It's killing me D:
Mark M.
you need to do something every minute? you will need to be in the foreground
Grigore C.
It's a reminder, user can set it to 1 min, 2, 3 .. or 10
Mark M.
or at least have a foreground service
is this repeating, or is this a one-off event?
Grigore C.
repeating
a notification should pop up every x minutes
and update it's content
Mark M.
that is bad for the battery, which is why Android pretty much stops it from happening
Grigore C.
yes... I understand that but gotta do it more or less
Mark M.
then I don't know what to tell you
there is simply no way to do that reliably over an extended period of time
Grigore C.
not even with a foreground service?
8:55 AM
Grigore C.
I guess keeping the app in the foreground with the screen unlocked could allow me to do that, right?
Mark M.
you are welcome to try a foreground service, a partial WakeLock, and ScheduledExecutorService for the periodic timing
many device manufacturers, particularly from China, will still interfere with your operatino
er, operation
Doze mode will break it on the rest
so, if you are looking to do this for a short period of time (every few minutes for an hour), you might be able to get away with that
after that, reliability will decline
Grigore C.
could some kind of refresh be implemented?
or it wouldn't matter?
Mark M.
I am uncertain what you mean by "refresh" in this context
Grigore C.
maybe restart the foreground service
Mark M.
if your foreground service is not running, your code to restart the foreground service also will not be running
if the user interacts with the phone every few minutes, in response to your notifications, that will disable Doze mode, which is based on user inactivity
Grigore C.
sounds like an impossible task
Mark M.
users have screamed very loudly over the years that they want longer battery life and fewer apps pinging them with notifications
9:00 AM
Mark M.
your app appears to runs counter to those user desires
(er, "run counter")
Android is aiming to satisfy users in general, at the cost of breaking certain possible types of apps
now, if this is for custom hardware, you could modify Android on that hardware to allow for your app to bypass these restrictions
but if your app is to be deployed on ordinary consumer phones and tablets... you are going to have problems
Grigore C.
does this imply rooting the device?
oh custom hardware.. no, that's not the case
Mark M.
yeah, I was implying a custom build of the Android OS
that you would be putting on your own hardware
and if that's not the case... I do not know what to tell you
Grigore C.
no, that's not the case
what about the phone's settings?
Mark M.
you can ask the user to opt your app out of some of the battery optimization effects
Grigore C.
or rooting?
Mark M.
users do not have to do this
there may be possibilities of bypassing the restrictions on a rooted device, but that is way outside my area of expertise
and users may not want to root the device
Grigore C.
hm
9:05 AM
Grigore C.
well, thank you, Mark :)
I'm going to read some more on the things you specified
Mark M.
sorry I do not have better answers for you... the "War on Background Processing", as I have called it, has been going on for about six years
Grigore C.
I'll let you know if I come up with a solution
don't worry, I am aware that my app is trying the opposite
and that there are a lot of custom android OS
like the huawei one, the samsung one etc
Mark M.
Huawei, Xiaomi, Oppo, and similar firms are even more aggressive than Google is regarding power management
a solution that works on a Samsung might not work on devices from those manufacturers
Grigore C.
great news! haha
yes, I've noticed that
but sometimes even FCM notifications won't wake up the device
Mark M.
they have to be "high-priority", and on newer versions of Android, effectively they are throttled in terms of waking up the device
I presume that too many developers were trying to use FCM for... ummm... the sort of thing that you are trying to do :-)
Grigore C.
I still don't get how whatsapp is doing this
9:10 AM
Mark M.
I do not use it, but I would be astounded if they are trying to send high-priority FCM messages every minute
plus, bear in mind that WhatsApp/Facebook is slightly larger than you or I
they can strike deals with manufacturers for preferential behavior
Grigore C.
that's right, they don't send notifications every minute
I've also thought about "super permissions" for big apps
Mark M.
in particular, pre-installed apps routinely can do things that ordinary user-installed apps cannot
Grigore C.
that makes sense
Mark M.
another advantage that a WhatsApp has is a large engineering department
they can have a team focused just on dealing with this issue, finding what amounts to security exploits to bypass limitations
so, aiming for WhatsApp-level results, for most firms, is unrealistic
Grigore C.
doze mode turns on when there is no user interaction right?
Mark M.
correct
I think there is an hour before it starts to have an effect
Grigore C.
soo if app is in foreground and the phone will not lock
does that mean there is interaction?
Mark M.
roughly speaking, if the screen is on, Doze mode should not take effect
9:15 AM
Grigore C.
disabled screen lock and/or developer options with the stay awake while charging option enabled
Mark M.
I do not know if the screen lock itself affects Doze mode, though it could
but, yes, the "stay awake" developer option, or a full WakeLock, would keep the screen on
in those cases, Android assumes you have a good reason for that, such as playing a video
of course, having the screen on all the time is bad for the device
(battery, screen burn-in, etc.)
Grigore C.
yes, but there are some trades off if you want 1 minute notifications
Mark M.
well, bear in mind that the user gets a vote, assuming that the app is being distributed to ordinary people
Grigore C.
that won't be a problem
Mark M.
ah, OK
Grigore C.
I mean this reminder is one of the "core" functionality of the app
Mark M.
oh
Grigore C.
sorry for not mentioning this plot twist earlier
Mark M.
well, you are certainly welcome to try a full WakeLock and see how your app behaves
bear in mind that you need a running process for that, so you would want to go the foreground service route
Grigore C.
I'll have a read on these
9:20 AM
Grigore C.
and for the repeating task should I use Handler?
Mark M.
I would use ScheduledExecutorService
despite the name, it is unrelated to an Android service
but it is a nice option for in-process periodic work when you do not care about the main application thread
Grigore C.
hm I see that this could "beep every ten seconds for an hour"
might be what I need
Mark M.
with ScheduledExecutorService, the "every ten seconds" part is easy
"for an hour" is where you are trying to defeat Doze mode and manufacturer-specific power management
9:25 AM
Grigore C.
I'm going to try that, worth a try
thank you, Mark! Hopefully, I'll come back with a working solution
Mark M.
I wish you luck!
Grigore C.
this chat will be archived, right?
Mark M.
yes, the transcript will appear on https://commonsware.com/office-hours/ shortly after the end of the chat
(basically, as soon as I upload it!)
Grigore C.
great! it's a great feature so I won't have to printscreen all of this :D
9:30 AM
Mark M.
and that is a wrap for today's chat
the next one is Saturday at 4pm US Eastern
have a pleasant day!
Grigore C.
has left the room
Mark M.
turned off guest access

Tuesday, January 19

 

Office Hours

People in this transcript

  • Grigore Cristian-Andrei
  • Mark Murphy