Jan 21 | 8:25 AM |
Mark M. | has entered the room |
Mark M. | turned on guest access |
Jan 21 | 8:45 AM |
Grigore C. | has entered the room |
Grigore C. |
Hello!
|
Mark M. |
hello, Grigore!
|
Mark M. |
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
|
Grigore C. |
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
|
Grigore C. |
yes
|
Grigore C. |
But how does whatsapp always "wake up" the screen?
|
Mark M. |
WhatsApp is almost certainly not using AlarmManager
|
Mark M. |
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
|
Grigore C. |
Yes, I read something about that, they might use FCM
|
Jan 21 | 8:50 AM |
Grigore C. |
So I need a 1 minute reminder no matter the doze mode.. what should I do? switch to FCM?
|
Grigore C. |
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
|
Mark M. |
is this repeating, or is this a one-off event?
|
Grigore C. |
repeating
|
Grigore C. |
a notification should pop up every x minutes
|
Grigore C. |
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
|
Mark M. |
there is simply no way to do that reliably over an extended period of time
|
Grigore C. |
not even with a foreground service?
|
Jan 21 | 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
|
Mark M. |
many device manufacturers, particularly from China, will still interfere with your operatino
|
Mark M. |
er, operation
|
Mark M. |
Doze mode will break it on the rest
|
Mark M. |
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
|
Mark M. |
after that, reliability will decline
|
Grigore C. |
could some kind of refresh be implemented?
|
Grigore C. |
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
|
Mark M. |
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
|
Jan 21 | 9:00 AM |
Mark M. |
your app appears to runs counter to those user desires
|
Mark M. |
(er, "run counter")
|
Mark M. |
Android is aiming to satisfy users in general, at the cost of breaking certain possible types of apps
|
Mark M. |
now, if this is for custom hardware, you could modify Android on that hardware to allow for your app to bypass these restrictions
|
Mark M. |
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?
|
Grigore C. |
oh custom hardware.. no, that's not the case
|
Mark M. |
yeah, I was implying a custom build of the Android OS
|
Mark M. |
that you would be putting on your own hardware
|
Mark M. |
and if that's not the case... I do not know what to tell you
|
Grigore C. |
no, that's not the case
|
Grigore C. |
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
|
Mark M. |
there may be possibilities of bypassing the restrictions on a rooted device, but that is way outside my area of expertise
|
Mark M. |
and users may not want to root the device
|
Grigore C. |
hm
|
Jan 21 | 9:05 AM |
Grigore C. |
well, thank you, Mark :)
|
Grigore C. |
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
|
Grigore C. |
don't worry, I am aware that my app is trying the opposite
|
Grigore C. |
and that there are a lot of custom android OS
|
Grigore C. |
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
|
Mark M. |
a solution that works on a Samsung might not work on devices from those manufacturers
|
Grigore C. |
great news! haha
|
Grigore C. |
yes, I've noticed that
|
Grigore C. |
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
|
Mark M. |
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
|
Jan 21 | 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
|
Mark M. |
plus, bear in mind that WhatsApp/Facebook is slightly larger than you or I
|
Mark M. |
they can strike deals with manufacturers for preferential behavior
|
Grigore C. |
that's right, they don't send notifications every minute
|
Grigore C. |
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
|
Mark M. |
they can have a team focused just on dealing with this issue, finding what amounts to security exploits to bypass limitations
|
Mark M. |
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
|
Mark M. |
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
|
Grigore C. |
does that mean there is interaction?
|
Mark M. |
roughly speaking, if the screen is on, Doze mode should not take effect
|
Jan 21 | 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
|
Mark M. |
but, yes, the "stay awake" developer option, or a full WakeLock, would keep the screen on
|
Mark M. |
in those cases, Android assumes you have a good reason for that, such as playing a video
|
Mark M. |
of course, having the screen on all the time is bad for the device
|
Mark M. |
(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
|
Mark M. |
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
|
Jan 21 | 9:20 AM |
Grigore C. |
and for the repeating task should I use Handler?
|
Mark M. |
I would use ScheduledExecutorService
|
Mark M. |
despite the name, it is unrelated to an Android service
|
Mark M. |
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"
|
Grigore C. |
might be what I need
|
Mark M. |
with ScheduledExecutorService, the "every ten seconds" part is easy
|
Mark M. |
"for an hour" is where you are trying to defeat Doze mode and manufacturer-specific power management
|
Jan 21 | 9:25 AM |
Grigore C. |
I'm going to try that, worth a try
|
Grigore C. |
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
|
Mark M. |
(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
|
Jan 21 | 9:30 AM |
Mark M. |
and that is a wrap for today's chat
|
Mark M. |
the next one is Saturday at 4pm US Eastern
|
Mark M. |
have a pleasant day!
|
Grigore C. | has left the room |
Mark M. | turned off guest access |