How to protect background service/alarms to be kill in newly launched devices in customized OS like oppo - coloros, vivo-funtouch os, Xiomi-MIUI os?

穿精又带淫゛_ 提交于 2019-12-18 12:16:46

问题


I have a WakefulBroadcastReceiver with IntentService, every half hour alarm called and doing some stuff. I have already handle doze mode with setExactAndAllowWhileIdle() method.

Some new smart phones with customized os recently launched in market they have their own customized os based on android os. Like oppo with color os, vivo with funtouch os, xiomi with MIUI os. In the OSs there is a feature to clear memory. They have one touch clear memory option. for example if user clear recent app list or remove app from recent app list app's all background service along with all alarms will be killed.

How to work with these new OS?


回答1:


For MIUI phones, you need to Off the POWER SAVING MODE then your app can run in the background.

Procedure : Go to Settings-> Battery-> Manage app's battery usage-> Click on Off or Choose your app

Programmatically:

 Intent intent = new Intent();
 intent.setClassName("com.miui.powerkeeper",
        "com.miui.powerkeeper.ui.HiddenAppsContainerManagementActivity");
 startActivity(intent);

For Oppo devices follow these steps:

  1. Settings-> Battery -> Your App-> Disallow both options.

Programmatically:

Intent intent = new Intent();
intent.setClassName("com.coloros.oppoguardelf",
       "com.coloros.powermanager.fuelgaue.PowerConsumptionActivity");
startActivity(intent);
  1. Security-> Privacy Permission-> StartUp manager-> Allow Your App.

Programmatically:

Intent intent = new Intent();
intent.setClassName("com.coloros.safecenter",  
       "com.coloros.safecenter.permission.startup.StartupAppListActivity");
startActivity(intent);
  1. Lock app in recent app's tab, by dragging it downwards.

This worked for me, hope this will work for you too :)




回答2:


For cases where your app is killed due to clearing it from the recent apps list, you can override android.app.Service.onTaskRemoved() to schedule an app restart or similar.

In all other cases where a user explicitly takes action to kill your app (e.g. doing a 'force stop' from the settings) --- so be it. Don't try to work around this. The user wants to kill your app. Let it die, and restore functionality the next time the app is started again by the user.




回答3:


try running your service in the different process.

<service android:name=".YourBackgroundService"
        android:process=":service">


来源:https://stackoverflow.com/questions/41804070/how-to-protect-background-service-alarms-to-be-kill-in-newly-launched-devices-in

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!