onTaskRemoved() not getting called in HUAWEI and XIAOMI devices
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
onTaskRemoved() not getting called when Home press & kill the app
Based on your use case, you should be able to meet the criteria for the White listing on Android N and above. You can follow this link to Whitelist your app. By requesting this permission, you can relax some restrictions (like accessing network or holding partial lock) implied by Doze mode and Android O. These restrictions are imposed by OS on the app those are not white-listed.
For Lollipop: Certain manufacturers using cyanogenmod or other custom implementation, could have impact on the intended behavior of START_STICKY. Workaround in this case would be to rely on onDestroy() method of service to:
- Restart the service.
- Trigger an AlarmManager which will trigger after few seconds and start the service.
If you use approach 2:
On normal devices where the START_STICKY behaves as intended, you can use the AlarmManager to check if service is running by:
- Maintain a static variable in service to check if service has been started
- Cancel the AlarmManager onStartCommand() of the service.
Service: onTaskRemoved not called if started with bindService
A service can be bound or started or both. It depends on how you implement onStartCommand
and onBind
say the docs.
http://developer.android.com/guide/components/services.html
But if you want your service to do both stay around and talk with clients over an IBinder then simply start the service and then bind to it.
startService(new Intent(context, CustomerService.class));
// Bind to the service
bindService(new Intent(context, CustomerService.class),
mConnection, Context.BIND_AUTO_CREATE);
Edit 20220314. "future versions of the os" may end the service when it doesn't have a notification card for the user to interact with the service.
...
lots have changed since I answered this I got a recent vote so here I am. You want your service to run in the background have a notification going and a way for the user to interact with the service in the notification. You've seen them the play pause controls on music and video notifications. Without a notification the os might end your service.
Related Topics
How to Make My Android App Appear in the Share List of Another Specific App
Android:Static Fields and Memory Leaks
Change Background Color of the Action Bar Using Appcompat
Androidstudio Cannot Find Layout
Android: What Is Android.R.Id.Content Used For
Encryption Error on Android 4.2
How to Set a Font for the Options Menu
Using Picasso with Custom Disk Cache
Removecallbacks Not Stopping Runnable
Android Maps API V2 with Custom Markers
How to Show Multiple Markers on Mapfragment in Google Map API V2
Linkedlist Put into Intent Extra Gets Recast to Arraylist When Retrieving in Next Activity
How to Send Multiple Sms to Single Number
Sqlite Android Database Cursor Window Allocation of 2048 Kb Failed