onTaskRemoved() not getting called in HUAWEI and XIAOMI devices
Asked Answered
C

4

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

Carpometacarpus answered 17/11, 2016 at 16:32 Comment(0)
I
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

Indisposed answered 15/2, 2017 at 7:17 Comment(4)
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.Carpometacarpus
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?Manta
over the years, implementing this could potentially require a lot of maintenanceWhipsaw
@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 dontkillmyapp.com, yet no one seems to be giving sh*t about itSchoolfellow
P
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.

Pettaway answered 8/2, 2017 at 17:54 Comment(6)
is there any way to do forcefully put the app in such list? Is any option to do in manifest?Flintlock
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.Pettaway
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 forums.lenovo.com/t5/P1-P1m-P70-P90-Series/…Pettaway
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 :)Carpometacarpus
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?Bore
@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.Pettaway
I
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

Indisposed answered 15/2, 2017 at 7:17 Comment(4)
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.Carpometacarpus
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?Manta
over the years, implementing this could potentially require a lot of maintenanceWhipsaw
@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 dontkillmyapp.com, yet no one seems to be giving sh*t about itSchoolfellow
R
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

Retrorse answered 14/2, 2017 at 12:33 Comment(3)
How to enable autistart programmatic way, Is it possible in manifest at the time of installation time itself?Flintlock
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.Retrorse
@NaveenKumarM no, this is not possible programatically on these devices. Some LG, Huawei and Lenovo devices have this feature as well.Pettaway
S
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..

Schock answered 18/11, 2017 at 8:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.