Keep location service alive when the app is closed

后端 未结 2 525
长发绾君心
长发绾君心 2020-12-14 12:27

I have a service which sends a notification when the user changes his/her location. This service is working fine, but the problem arises when the user closes the app as the

2条回答
  •  囚心锁ツ
    2020-12-14 13:15

    There are two kinds of Android Services: started and bound. You need to use the first one(started Service). The documentation shows how to use it, there is a nice lifecycle diagram below.

    Instead of starting and binding the service in one step using bindService() you need to call startService() first. However startService() won't help you starting from Oreo. You need to usestartForegroundService() from there. Then it runs until you stop it, even if the app is closed.

    Start Sticky Service if you want Android OS to pick your service again if it killed it.

提交回复
热议问题