Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

onTaskRemoved() not getting called in HUAWEI and XIAOMI devices

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

like image 839
Alon Minski Avatar asked Nov 17 '16 16:11

Alon Minski


3 Answers

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.

like image 108
David Wasser Avatar answered Nov 08 '22 20:11

David Wasser


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

like image 16
Vishnu Prasad Avatar answered Nov 08 '22 20:11

Vishnu Prasad


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

like image 6
W4R10CK Avatar answered Nov 08 '22 19:11

W4R10CK