38

I've been using onTaskRemoved() method in a Service to detect when an app was removed from device RECENT list by swiping it away. I preform some logging and some other operations that need to take place when this happens. It works perfectly.

Then I checked this method in an HUAWEI device running Android 6.0. The method never gets called. I also added a Log.d call and as expected, this log never appeared. The same happens on a XIAOMI device.

Any ideas why this happens and how to resolve this? Or is there another way to detect app was removed from RECENT list with out relying on onTaskRemoved() ?

Thanks

zhangxaochen
  • 32,744
  • 15
  • 77
  • 108
Alon Minski
  • 1,571
  • 2
  • 19
  • 32

4 Answers4

32

On some devices (some LG, Huawei, Xiaomi, and others) your app needs to be manually added to a list of "protected apps" or "apps that are allowed to run in the background" in order for Android to restart STICKY services. If your app has not been manually added to this list, Android just kills your processes and does not restart them and also does not call onTaskRemoved(). This is done to preserve battery life by limiting the number of apps that can have STICKY services running in the background.

On such devices you should see a page in the "Settings", sometimes under "power management", sometimes other places, where you need to explicitly add your application. You'll also need to tell your users to explicitly add your app to this list.

David Wasser
  • 93,459
  • 16
  • 209
  • 274
  • is there any way to do forcefully put the app in such list? Is any option to do in manifest? – Naveen Kumar M Feb 14 '17 at 07:03
  • No. The user must add the app himself. You cannot do it programatically. On some devices, the app may be automatically added if it is installed from the Play store (instead of being installed via ADB), but I'm not sure which devices do this. Not all do. – David Wasser Feb 14 '17 at 07:12
  • 1
    Some Lenovo phones have a "background app management" setting. However, it looks like lots of folks have this problem with Lenovo phones. It is possible that Lenovoa has broken something. See https://forums.lenovo.com/t5/P1-P1m-P70-P90-Series/Apps-stop-running-in-background-in-VIBE-P1m/td-p/2250275 – David Wasser Feb 14 '17 at 14:36
  • 1
    The answer is sound and valid. It led me in the right direction, but Vishnu Prasad's answer had an actual action that solved the issue, so I gave him the mark :) – Alon Minski May 23 '17 at 15:05
  • 1
    What still baffles me is how apps like facebook and whatsapp do this without much stress of asking the user to manually do it, are the having a higher level of permission or what? – Tosin John Mar 21 '18 at 22:39
  • 2
    @TosinJohn These companies have a deal with the manufacturers to permit their apps to be "preapproved" for this feature. These apps are usually preinstalled on the devices anyway. – David Wasser Mar 22 '18 at 12:41
16

When user has installed your app on xiaomi device, redirect user to auto start activity and tell user to switch on:

if (Build.BRAND.equalsIgnoreCase("xiaomi")) {
                Intent intent = new Intent();
                intent.setComponent(new ComponentName("com.miui.securitycenter", "com.miui.permcenter.autostart.AutoStartManagementActivity"));
                startActivity(intent);
            }

Use the above code to launch autostart activity page on xiaomi

Vishnu Prasad
  • 584
  • 4
  • 11
  • Thank you, combined with David Wasser's answer I managed to resolve this issue. I'm explaining to the user he needs to manually activate this system feature and then I can also send him to the right Settings screen. – Alon Minski May 23 '17 at 15:06
  • Me also get stuck in that please guide me,The above solution where we need to put and more over for HUAWEI device what we need to do? – Rajaji subramanian Oct 25 '17 at 14:06
  • 1
    over the years, implementing this could potentially require a lot of maintenance – Someone Somewhere Jun 26 '18 at 11:20
  • @SomeoneSomewhere As if going other direction will require something less. Everything in this matter seems to be a plain workaround. Even apps with millions of installs suffer that's why a team created https://dontkillmyapp.com, yet no one seems to be giving sh*t about it – Farid May 25 '21 at 11:59
6

I've been using onTaskRemoved() method in a Service to detect when an app was removed from device RECENT list by swiping it away.

With giving more light to the answer provided by David Wasser

It doesn't new on Xiaomi because Xiaomi has a feature called app permission, where a user has to allow the app to start automatically (Service). In your case the Service is not called, once its terminated from stack.

Go like this and allow your app to autostart:

Settings > permissions > Autostart

Community
  • 1
  • 1
W4R10CK
  • 5,502
  • 2
  • 19
  • 30
  • How to enable autistart programmatic way, Is it possible in manifest at the time of installation time itself? – Naveen Kumar M Feb 14 '17 at 14:49
  • First of all, this only happens on Xiaomi and some other Japanese and Chinese handset. This is not possible to enable programmatic because this is not a global handset issue. Preferred way is to add a message at `onDeatroy` to notify user. I'm still finding solutions to it. – W4R10CK Feb 14 '17 at 15:08
  • @NaveenKumarM no, this is not possible programatically on these devices. Some LG, Huawei and Lenovo devices have this feature as well. – David Wasser Mar 22 '18 at 12:42
3

In My Huawei also i was facing porblem, Just go Setting => Power Saving => Protect App => find your app and enable it.. Service will start running..

Sanjay Goswami
  • 191
  • 2
  • 7