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.
1 Answers
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 :
- Use
AlarmManager
- All alarms are forgotten by the android on
Reboot
, so keep all alarms in appSqlite
database, implementBOOT_COMPLETED
broadcastreceiver, which is still allowed to define statically & re-schedule all alarms by taking from apps database. - All your project & hard work will work as per your plan, but only on
emulators
- Because in real world, 99% devices are with chinese ROMs like MIUI, OxygenOs, ColorOs, FunTouchOs, .. which never triggers your apps
BOOT_COMPLETED
broadcastreceiver. - Because they all are ahead in saving battery life, by stopping all apps background services..!!
- Nice question,:- Then why whatsapp, facebook, tiktok, twitter processes are getting executed normally...?? And why not mine...??
- Nice answer,:- If Chinese ROMs disallowed above apps background services then no one will buy their devices..!! And lets do not talk about your application as it does not affect the world in any way, your app is
useless
( as per chinese ROMs ) , so they disallows all other packages background tasks..!! - All Chinese ROMs never triggers AlarmManagers if the app is not in
foreground
. Never triggers if app is removed fromrecents
- In short & sweet treat Alarmmanager as depriciated.
STRATEGY NO - 2 :
Use
FirebaseJobScedular
as you statedIf 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 ownwishlist
, 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 yourbroadcastreceiver
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 ...?
- Nothing as developer.
- Train your support / sales team to "LOCK" your app in the recents of every
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- Train your support / sales team how to make battery optimisations to "Do not optimise for this app" too..
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

- 1,334
- 12
- 19
-
2What do you mean by "LOCK your app in the recents"? – pegnose Dec 07 '21 at 12:48