I am creating a background service that will run in its own process. It should allow me to listen if the device location has changed. I should be able to change criteria like the distance moved before notifying the UI.
How can I do the same? I have a little knowledge of service and LocationListener
implementations. Any tutorials around the net would be appreciated.
I got one back-link from stack overflow, but I didn't understand much of it.
First you need to create a
Service
. In thatService
, create a class extendingLocationListener
. For this, use the following code snippet ofService
:}
Add this
Service
any where in your project, the way you want! :)Very easy no need create class extends LocationListener 1- Variable
2- onStartService()
3- Method addListenerLocation()
4- onDestroy()
Background location service and i will be start even after kill app.
MainActivity.java
BookingTrackingService.java
AlarmReceive.java (BroadcastReceiver)
AndroidManifest.xml
I know I am posting this answer little late, but I felt it is worth using Google's fuse location provider service to get the current location.
Main features of this api are :
1.Simple APIs: Lets you choose your accuracy level as well as power consumption.
2.Immediately available: Gives your apps immediate access to the best, most recent location.
3.Power-efficiency: It chooses the most efficient way to get the location with less power consumptions
4.Versatility: Meets a wide range of needs, from foreground uses that need highly accurate location to background uses that need periodic location updates with negligible power impact.
It is flexible in while updating in location also. If you want current location only when your app starts then you can use
getLastLocation(GoogleApiClient)
method.If you want to update your location continuously then you can use
requestLocationUpdates(GoogleApiClient,LocationRequest, LocationListener)
You can find a very nice blog about fuse location here and google doc for fuse location also can be found here.
Update
According to developer docs starting from Android O they have added new limits on background location.