onTaskRemoved() not getting called in HUAWEI and XIAOMI devices - android

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

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.

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

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

Related

Not getting push notification when swipe from recent

I'm not getting notification when user swipe the app from recent. I've notice that behavior occurs in some specific device for instance I've tested it on nexus devices and HTC devices it's working fine but when user swipe the app from the recent in huawei and some samsung devices it doesn't show notification. I mean i don't get onMessageReceived() callback
After some research I found out that in these devices when user swipe it from recent it force stop the app (the process is completely killed) and I don't get any notification. In huawei they have protected and unprotected apps and if I add the app in protected list from the setting, I start getting notification because i this case it doesn't force stop the app. and I believe in samsung they have the same thing with blacklist and whitelist apps.
I've also tried some solutions like restart the service onTaskRemoved() callbacks or make your app START_STICKY. I've also tried WakefulBroadcastReceiver but nothing is working for me.
So my question is how can i get notification even if it gets forced stop, like it's happening when swiping app from recent.
An old question, but I think it’s important to know that HUAWEI has a feature called “power-intensive app monitor “. It kills every app that runs in the background for a long time unless user gives special permissions to it.
The path to do this:
Settings -> Security & privacy -> Location services -> recent location requests: YOUR APP NAME -> Battery -> uncheck Power-intensive prompt, App launch: Manage manually: check all three positions: Auto-launch, secondary launch, run in background.
I don’t know if there's a way to do this programmatically. I think the best way is to create a sort of help activity and explain the user what to do if application won’t work.

Service onTaskRemoved not call when kill app in some device [duplicate]

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

Android Lollipop - prevent task manager from ending my app

My goal is to make my app unkillable.
Application has admin permissions granted.
I have one activity and one service.
Applionation cannot be uninstalled while Admin permission is active (thats good).
Service is auto re-creating. That's also correct.
Unfortunately on Android 5 - Lollipop user can click on all apps button - then go to Task Manager and simply END my app without any problems. After that service is destroyed for good (activity too obviously...)
Is this a way to prevent that?
1) Xposed. At the cost of execution speed and dependency on the TaskManager that you will have to reverse-engineer a bit...
2) USER_PRESENT and friends. Your application can register a BroadcastReceiver and re-start on events. That is, it will be killable, but it will restart.
I think you are interested in something like a kiosk mode: displaying a single app and preventing to break out from it.
There are various tutorials out there. This one is quite good:
http://www.andreas-schrade.de/2015/02/16/android-tutorial-how-to-create-a-kiosk-mode-in-android/
Edit: if your main target are Android 5.0+ devices you can also make use of screen pinning. http://developer.android.com/about/versions/android-5.0.html#Enterprise

Android 5.0 force stop behavior

Just update to Android 5.0 by OTA. The biggest find is that if a user Force Stop my app(in System Setting), my app stops right now. And my app cannot receive any broadcast anymore, even if my receiver is registered in AndroidManifest.xml. More surprisingly, when the user reboot the device, my app even cannot receive android.permission.RECEIVE_BOOT_COMPLETED broadcast.
Does anyone pay attention to this?
Yes, I just noticed that on my Nexus 5, and I'm really loving it!
When the user "Force Stop" an app in Settings->Apps, it will be TOTALLY stopped and is black listed from receiving any broadcasts UNTIL the user open the app from the launcher.
I see it as a way like the "Disable" of system apps, you completely disable this app until you open it again.
I see this really very useful as I have lots of apps that are using unnecessary services in the background like Facebook for instance.

How to auto restart a service when killed?

I have already red many posts about the subject, some of these works, but only on some devices. For example it works on my Nexus 4, Nexus 5 with cyaogenmod, but not on my Xiaomi Mi2s (jelly beam 4.1.1)
I tried setting it sticky, using alarm, and broadcast receiver on destroy. But neither of them worked out. When I kill the application it never wake up again. But on the same device Whatsapp works perfectly, and even if I kill it, it wake up again..
Is there any way to do it without use GCM?
Some of the link I already looked up:
How to restart a killed service automatically?
Service that repeatedly runs a method, after an amount of time
https://stackoverflow.com/a/21551045/627307
I'm not sure if it will solve the issue for you or not but it worked for me.
I'm running MIUI 6.6.10
Open the Security app provided by Xiaomi
Click on permissions -> Autostart
Here allow your app to auto start
WhatsApp and all famous apps work because Xiaomi white listed them and allowed them to autostart.

Categories

Resources