androidalarmmanagerandroid-workmanagerandroid-backgroundfirebase-job-dispatcher

How to perform background service while battery optimization is active


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.


Solution

  • 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 :

    1. Use AlarmManager
    2. All alarms are forgotten by the android on 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.
    3. All your project & hard work will work as per your plan, but only on emulators
    4. Because in real world, 99% devices are with chinese ROMs like MIUI, OxygenOs, ColorOs, FunTouchOs, .. which never triggers your apps BOOT_COMPLETED broadcastreceiver.
    5. Because they all are ahead in saving battery life, by stopping all apps background services..!!
    6. Nice question,:- Then why whatsapp, facebook, tiktok, twitter processes are getting executed normally...?? And why not mine...??
    7. 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..!!
    8. All Chinese ROMs never triggers AlarmManagers if the app is not in foreground. Never triggers if app is removed from recents
    9. In short & sweet treat Alarmmanager as depriciated.

    STRATEGY NO - 2 :

    1. Use FirebaseJobScedular as you stated

    2. If user updates Google Play Services , then system forgets all jobs..!!

    3. 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.

    4. This library is depriciated by google now.

    STRATEGY NO - 3 :

    1. Use WorkManager, the new library which is taking place over the FireBaseJobScedular officially.

    2. 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 :

    1. Run all time foreground service.

    2. BOOT_COMPLETED from your broadcastreceiver start it again.

    3. 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 :

    1. Use WakeLocks

    2. But as the android version changes, it has changed to the newer newer implementational strategies in every versions

    STRATEGY NO - 6 :

    1. Programatically make battery optimisation off for your app, but google discontinues your app from app store in such case

    2. Navigate user to battery optimisation settings intent, by opening it programatically & let him choose battery optimisations on his own, is allowed by google

    3. But as no user is in market, who knows it, as even many developers dont know this yet, how end users can understand it..??

    4. 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 ...?

    1. Nothing as developer.
    2. 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
      1. 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