Nav
Nav

Reputation: 477

Android best way to get location repeatedly in background considering battery as well

I need to request for location continuously in my app and it should always be a background service.

I need latlng for like every two minute or so. I thought of using a Service and in onStartCommand i would use locationmanager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MINIMUM_TIME_INTERVAL, MINIMUM_DISTANCE, this); as well as locationmanager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MINIMUM_TIME_INTERVAL, MINIMUM_DISTANCE, this);

But i am afraid as time goes this requestLocationUpdates stops giving me location. So i thought of using AlarmManager so it keeps on giving me location.

My question is , is it necessary to use AlarmManager to remind to get location or if i use AlarmManager will it drain lot of battery.

I just wanted to know best approach to get location in background. Thanks in advance.

Upvotes: 12

Views: 5454

Answers (3)

Elango
Elango

Reputation: 411

this link useful for you?

http://wptrafficanalyzer.in/blog/android-gps-with-locationmanager-to-get-current-location-example/

use service to listen the location(LocationListener)

Upvotes: 0

Daniel Kopitchinski
Daniel Kopitchinski

Reputation: 525

The new google play location APIs are by far the best way to go. The fused location API takes into account all the location providers and the location accuracy you need, to provide the best location for your app.

The geofencing APIs (notify your app when the user enters/leaves a certain area), and the activity recognition APIs (is the user walking? sitting? running? driving?) can also be very helpful in saving battery and improving accuracy.

Of course this requires the user has an updated version of the Google Play App, but that shouldn't be much of a concern.

Upvotes: 0

horin
horin

Reputation: 1654

Why do you want to use a service for this? For me it is working without service. I have class called GPSlistener which implements LocationListener and has this line in its constructor:

locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 5, this);

I am using this approach because I dont want locationUpdates to stop but you can use this method to start listening:

public void startListening(){
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 5, this);
}

and then this to stop listening:

public void stopListening(){
    locationManager.removeUpdates(this)
}

You can save battery by setting higher minTime or minDistance. I belive that minDistance should be more relevant for you. So for example if you want to update location only every 10 meters then you call it like this

locationManager.requestLocationUpdates(LocationManager.GPS_Provider, 5000, 10, this)

Best setting depends on your app. For example if you want to build a GPS navigation for cycling then you should use location update after max 5m. On the other side if you are building app for some "slower" activity like hiking then 10 or 15 meters would be enough I think.

I dont think that locationManager should stop sending location updates and I also didnt have this problem in my app.

By using this approach I get GPS runing in background without use of service. But the truth is that my app consists only of one activity and 3 fragments so I dont need to use service.

Upvotes: 3

Related Questions