15

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?

Himanshu
  • 861
  • 10
  • 25
  • Do you find any feasible solution on this issue? – Himeshgiri gosvami Apr 20 '17 at 10:33
  • The only "proper" ways to terminate services are process-kill (SIGTERM) and programmatic stopping. If your service returns START_SICKY from onCreate is should be automatically restarted after process-kill, and programmatic stopping can be handled by overriding onTaskRemoved and/or onDestroy, If that does not work, that probably means, that the service (and the entire app along with it) has been placed into ["stopped state"](https://stackoverflow.com/a/8535062/1643723). In that case the ways to counter it should be the same. – user1643723 May 12 '17 at 07:39
  • @himCream did you find any solution for this problem – Dev May 17 '17 at 18:13
  • @Dev Yes, It's some what manual solution like...we need to make some manually settings from user's device according to device type....the common settings would be make app whitelisted in the device...... – Himanshu Sep 30 '17 at 08:58

3 Answers3

17

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

Vishal Yadav
  • 3,642
  • 3
  • 25
  • 42
Shaifali Rajput
  • 1,279
  • 12
  • 30
  • [Try this solution for xiaomi, oppo and vivo phones. Autostart screen will be launched if it exists](https://stackoverflow.com/a/44818978/7034327) – John Oct 27 '17 at 10:29
  • 1
    OPPO solutions is working only for android version 5.1.1, not working on marshmallow – user3069590 Nov 01 '17 at 08:19
  • This solution seems very specific to the device what if the new one comes up in future (maintenance nightmare :) ) I would look for more generic solution as recommended by Tim below – Naga Nov 16 '18 at 06:57
4

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.

Tim
  • 41,901
  • 18
  • 127
  • 145
  • 1
    I have used onTasRemoved() but it is only called when user clear specific app but not called when user did all clear app from the recent task list..... and also I have noticed when I force close Facebook or whatsapp after some time I can see service restarting in 'Running Services' tab in the settings... – Himanshu Jan 23 '17 at 10:52
  • It's beside the point. If the user wants to kill your app, you shouldn't try to go against it – Tim Jan 23 '17 at 10:57
  • I got your point but I have no problem If user 'Forced Close' my app but how to handle if user clear app from 'recent app list' and service has been kill all alarms has been canceled....That is my concern actually ? – Himanshu Jan 24 '17 at 06:23
-2

try running your service in the different process.

<service android:name=".YourBackgroundService"
        android:process=":service">
vikoo
  • 2,451
  • 20
  • 27