State-of-the-art Method For Ongoing, Frequently Updating Notifications ("notification Widget")
I am struggling to find a current state-of-the-art method for notifications that are frequently updated (e.g. every 3 minutes). There seem to have been so much power efficiency tweaks built into newer Android versions (fortunately!), that the method I earlier successfully used (using BroadcastReceiver
and AlarmManager.setRepeating
) does not seem to reliably work anymore on my newer phone (running with Android 8). At some point, the notification will not get updated anymore (e.g. after the screen has been off for a couple of minutes).
Now I have stumbled acrossmany different approaches in order to address this issue, that I am a little bit lost between them, e.g:
- using a chain of
AlarmManager.setAndAllowWhileIdle
instead ofsetRepeating
- using
WakefulBroadcastReceiver
s (being already deprecated again though) - using an
IntentService
instead of aBroadcastReceiver
- using a
JobIntentService
instead of aIntentService
- using the
JobScheduler
instead ofAlarmManager
Did anyone find a reliable way in order to frequently update a status bar notification? For me, it is not important to update it exactly every X seconds, it just should happen every few minutes and not being randomly broken after the device has been in deep sleep for some time.
Answer
I do not really understand why this question got downvoted because in my opinion there are legit reasons for the confusion if you stumble across all those different methods and it would have really saved me some time if somebody had shared his or her experience with me.
I understand that this question is difficult to answer, because of all the different manufacturers and phones, all including different types of power saving tweaks. But anyway, here is what I came up with after some time. The problem in my case was that in Android 8+, Google has strongly limited the granted possibility to do work for BroadcastReceiver
s, IntentServices
and such in favor of using the JobScheduler
. This makes it mandatory to use JobIntentService
, providing the needed features for Android 8+ as well as backwards-compatibility for Android versions < 8.
tl;dr:
- Have a
JobIntentService
, where you what you have to do insideonHandleWork
- Have a
BroadcastReceiver
, where inonReceive()
you just call yourJobIntentService
'senqueueWork()
method. - Use the
AlarmManager
insideJobIntentService#onHandleWork()
in order to schedule (e.g. usingsetInexactRepeating()
) and cancel aPendingIntent
broadcast for yourBroadcastReceiver
.
Related Questions
- → should I choose reactjs+f7 or f7+vue.js?
- → Phonegap Android write to sd card
- → Local reference jquery script in nanohttpd (Android)
- → Click to navigate on mobile devices
- → How to allow api access to android or ios app only(laravel)?
- → Access the Camera and CameraRoll on Android using React Native?
- → React native change listening port
- → What is the default unit of style in React Native?
- → Google play market autocomplete icon
- → Warning: Each child in an array or iterator should have a unique "key" prop. Check the render method of `ListView`
- → Using Laravel with Genymotion
- → react native using like web-based ajax function
- → react native pdf View