I am working on an application that has some background services (for blocking applications). I am using Xiaomi Redmi Note 4g for development. MIUI has a built-in task manager that can be used to destroy running applications. When I kill applications with this, all services, translators and alarms are deleted (as expected from the task manager). My requirement is that my background services not be cleared of killer tasks, but if the user goes to settings and force shuts down, the service should stop. How to achieve this?
By the way, I know that this cannot be done since Whatsapp and Truedialer (some egs) already do this on the latest versions of Android.
My observations from Whatsapp:
- I will kill the application from the task manager
- All services on Whatsapp stopped, except for "MessagingService"
- The list of running applications displays the status "Restart"
- The AlarmService service is starting (not sure if the MessagingService is starting it or from the AlarmManager)
- After a time delay, all services are up and running
Note. The timer in the MessagingService is not reset, and it still continues to tick. But if I forcibly stop the service, it wonβt start again until I manually started the application.
Similar observations were made for TrueDialer.
I tried to reproduce this with START_STICKY, AlarmManager, BroadcastReceiver, but was unsuccessful. Can someone help me with this? I just want to understand how Whatsapp accomplishes this. Feel free to ask if you need more information.
source share