Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 908
Nav Avatar asked Dec 05 '14 10:12

Nav


People also ask

What are some ways to make location updates not drain the battery of the phone?

Adjust your location services settings​ But if these apps are running behind the scenes and you aren't traveling, location services can drain your battery. To adjust this: Go to Settings > Location. Disable location setting services by turning off the switch at the top of the screen.

How much battery does location use in Android?

Conclusion. Enabling GPS uses zero extra power except when location services are actually being used. You'll use more power turning it on and off than just leaving it on all the time. There is no point in keeping your GPS on if you aren't using any app that utilizes it.


1 Answers

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.

like image 199
horin Avatar answered Nov 03 '22 04:11

horin