Restarting killed (stopped by task-killers) services in android like Whatsapp

限于喜欢 提交于 2019-12-19 19:46:25

问题


I am working on a app locker which has some background services (to lock apps). I am using Xiaomi Redmi Note 4g for the development. The MIUI has an in-built task manager which can be used to kill running apps. When I kill apps with this, all services, broadcastreceivers and alarmmanagers are getting removed (as expected from a task manager). My requirement is to prevent my background services from getting cleared from task-killers, but if the user navigates to settings and force closes, the service should stop. How do I achieve this?

Btw, I know that its not impossible to achieve this since Whatsapp and Truedialer (some egs) are already doing this on the latest android versions.

My observations from Whatsapp:

  • I kill the app from the task manager
  • All services in Whatsapp are stopped except for "MessagingService"
  • The list view of the running apps show status as "Restarting"
  • "AlarmService" service starts up (not sure if MessagingService is starting it or from an AlarmManager)
  • After a time lag, all services are up and running

Note: The timer on the MessagingService isn't reset and it still keeps on ticking. But if I force stop the service, its not started again until I manually fire the app.

Similar observations are made for TrueDialer.

I have tried to replicate this with START_STICKY, AlarmManager, BroadcastReceiver, but was unsuccessful. Can someone help me on this? I just want to understand how Whatsapp is achieving this. Feel free to ask if you need more info.


回答1:


I've solved the problem.

Solution for MIUI 7.0 => Security => Autostart => select Apps that you want to run in background => Reboot After reboot your device should able to run your application services in background like other android devices do.

MIUI 4.0 settings

MIUI AutoStart Detailed Description



来源:https://stackoverflow.com/questions/31558866/restarting-killed-stopped-by-task-killers-services-in-android-like-whatsapp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!