I currently use the Firebase JobDipatcher to perform a periodic task in the background. The problem is the background service only gets executed when battery optimization is disabled and standby state of the app is manually set to ACTIVE under developer options. Is there a way to do the background task without manually changing all this, because i cannot ask every user of the app to do so. And saw many posts that google will suspend the app if the power management permissions are requested within the app. Any help would be much appreciated.
In one word answer is BIG NO
As developers remains unhappy with such small answers, so i am starting the HUGE
theory, here we go -
STRATEGY NO - 1 :
AlarmManager
Reboot
, so keep all alarms in app Sqlite
database, implement BOOT_COMPLETED
broadcastreceiver, which is still allowed to define statically & re-schedule all alarms by taking from apps database. emulators
BOOT_COMPLETED
broadcastreceiver. useless
( as per chinese ROMs ) , so they disallows all other packages background tasks..!!foreground
. Never triggers if app is removed from recents
STRATEGY NO - 2 :
Use FirebaseJobScedular
as you stated
If user updates Google Play Services
, then system forgets all jobs..!!
Again FirebaseJobScedular
was developed to run all jobs on every device in market, but as chinese ROMs never allows any processes / classes / Jobs from their own wishlist
, this makes no sense again.
This library is depriciated by google now.
STRATEGY NO - 3 :
Use WorkManager
, the new library which is taking place over the FireBaseJobScedular officially.
Again the same thing, WorkManager
is still in development, you can use it, but the case is same that many chinese ROMs discard this libraries jobs too..!!
STRATEGY NO - 4 :
Run all time foreground service.
BOOT_COMPLETED
from your broadcastreceiver
start it again.
But even if you use START_STICKY
, many chinese ROMs used to suspend its tasks, you can see it in foreground, but works gets suspended.
STRATEGY NO - 5 :
Use WakeLocks
But as the android version changes, it has changed to the newer newer implementational strategies in every versions
STRATEGY NO - 6 :
Programatically make battery optimisation off for your app, but google discontinues your app from app store in such case
Navigate user to battery optimisation settings intent, by opening it programatically & let him choose battery optimisations on his own, is allowed by google
But as no user is in market, who knows it, as even many developers dont know this yet, how end users can understand it..??
Again if power saving mode ( it is different than battery optimisation okay ) is made ON by user then, no way your battery optimisations is never considered.
What the ***** should i do then ...?
phone
, after which, users are unable to swipe it away from the recents, even if it is swiped away, your all above strategies will work & your jobs / alarms / tasks will be executed, as it happens on your android emulator or as on stock android
It is so out of the box solution..??
Yes, it is. As in the market you will notice 1% stock android devices and 99% chinese modified ROMs
This all knowledge came to me after i lost last 9 to 10 years resolving the same problem, Hussshhhh