How to restart service after the app is killed from recent tasks

后端 未结 4 735
执念已碎
执念已碎 2020-12-01 04:43

I have created a service to fetch current location of the device in periodic intervals. I want the service to run in the background even if the app is cleared from recently

4条回答
  •  不知归路
    2020-12-01 05:11

    If you ONLY want to restart service after it kill from Recent Task, simple use

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return START_STICKY;
    }
    

    If you use START_STICKY, when you kill app from recent task, your service will be killed (onTaskRemoved fired, onDestroy NOT fired) THEN it will auto start again (onCreate fired, onStartComand fired)

提交回复
热议问题