onTaskRemoved() not getting called in HUAWEI and XIOMI devices

http://stackoverflow.com/questions/40660216/ontaskremoved-not-getting-called-in-huawei-and-xiomi-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 XIOMI 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

share
 

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.

share
 
 
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 at 7: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 at 7:12
 
I am not able to find any such options in lenovo phones.. –  Rahul Hawge  Feb 14 at 11:04 
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 forums.lenovo.com/t5/P1-P1m-P70-P90-Series/… –  David Wasser  Feb 14 at 14:36

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

share
 
 
How to enable autistart programmatic way, Is it possible in manifest at the time of installation time itself? –  Naveen Kumar M  Feb 14 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 at 15:08

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


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 XIOMI 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

share
 

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.

share
 
   
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 at 7: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 at 7:12
   
I am not able to find any such options in lenovo phones.. –  Rahul Hawge  Feb 14 at 11:04 
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 forums.lenovo.com/t5/P1-P1m-P70-P90-Series/… –  David Wasser  Feb 14 at 14:36

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

share
 
   
How to enable autistart programmatic way, Is it possible in manifest at the time of installation time itself? –  Naveen Kumar M  Feb 14 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 at 15:08

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值