Reputation: 71
I want to write a Service which keeps track of my current location (and a different Service for my speed). I've seen some code examples, but all them are for Activities and not for Services. I want the Service to keep track until I stop it. How do I do it?
Upvotes: 0
Views: 988
Reputation: 1006584
I want to write a SERVICE which keeps track of my current location (and a different Service for my speed).
You can do both tasks with one service. You do not need two services.
How do i do it?
Call requestLocationUpdates()
on a LocationManager
in onCreate()
of your Service
. Call removeUpdates()
in onDestroy()
of your Service
. Collect the location and speed data in onLocationChanged()
of the LocationListener
that you supplied to requestLocationUpdates()
. Make sure that the user has complete control over when startService()
and stopService()
are called. Also, request the appropriate permission (e.g., ACCESS_FINE_LOCATION
).
Upvotes: 1