6

I'm developing an Android application; this App needs to send periodically (every 10 minutes) the current position (coordenates) to a web service. But ... I'm a little confused about the more correct way (and friendlier to the device battery) to do that.

I read this answer and her method _getLocation() looks well; but I don't know whether that method could get the availability of the location I need; total availability...

I would like, if the location is not available using GSM / WIFI, application choose the GPS method.

Is that what makes this method?

private void _getLocation() {
    // Get the location manager
    LocationManager locationManager = (LocationManager) 
            getSystemService(LOCATION_SERVICE);
    Criteria criteria = new Criteria();
    String bestProvider = locationManager.getBestProvider(criteria, false);
    Location location = locationManager.getLastKnownLocation(bestProvider);
    try {
        lat = location.getLatitude();
        lon = location.getLongitude();
    } catch (NullPointerException e) {
        lat = -1.0;
        lon = -1.0;
    }
}

Anybody know one way to get the coordinates of the device periodically... without dramatically increase battery consumption?

Community
  • 1
  • 1
tomloprod
  • 7,472
  • 6
  • 48
  • 66
  • If you are worried about battery consumption and so worried about getting your location updates a bit late, you should check the PassiveProvider. It's piggybacking on other apps who use the Location Services. – Ohad Zadok Jan 29 '15 at 04:44
  • Before all; thanks for your comment. And yes, I'm so worried about this things. May you give me a example for `PassiveProvider`? – tomloprod Jan 29 '15 at 06:11
  • see my answer below. – Ohad Zadok Jan 29 '15 at 14:34
  • possible duplicate of [What's the most battery-efficient approach of using LocationClient to periodically get updates?](http://stackoverflow.com/questions/17139866/whats-the-most-battery-efficient-approach-of-using-locationclient-to-periodical) – Sean Barbeau Jan 29 '15 at 14:37
  • This is a duplicate of http://stackoverflow.com/questions/17139866/whats-the-most-battery-efficient-approach-of-using-locationclient-to-periodical/17155460#17155460 – Sean Barbeau Jan 29 '15 at 14:37

2 Answers2

4

If you are worried about battery and not so stricted on the 10 minutes interval you could try to use PassiveProvider instead of GPS/Coarse.
Usually other applications request locations often enough so you don't need to worry about it.
If you are strict, than you could try to ask for location yourself in case one hasn't received in the past interval.
Here is an example for using the Passive Provider.

LocationManager locationManager = (LocationManager) this
        .getSystemService(Context.LOCATION_SERVICE);
LocationListener locationListener = new LocationListener() {

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {}

    @Override
    public void onProviderEnabled(String provider) {}

    @Override
    public void onProviderDisabled(String provider) {}

    @Override
    public void onLocationChanged(Location location) {
        // Do work with new location. Implementation of this method will be covered later.
        doWorkWithNewLocation(location);
    }
};

long minTime = 10*60*1000;
long minDistance = 0;

locationManager.requestLocationUpdates(LocationManager.PASSIVE_PROVIDER, minTime, minDistance, locationListener);
Ohad Zadok
  • 3,452
  • 1
  • 22
  • 26
  • This snipet will get the coords even when the phone is suspended? – Eric Freitas May 28 '16 at 20:11
  • What do you mean by phone is suspended? if the location services is off you will not get location updates. You can still register the locationListener but it will not be called. – Ohad Zadok May 30 '16 at 12:01
  • Hi @Ohad, thank you for your reply, what do I meant was: blank screen, locked screen, black screen, idk the correct term for it... like when u press the shutdown button, the screen will turn off, but my phone still on. – Eric Freitas May 31 '16 at 20:21
3

Play Services has a Low Consumption location API. You can found more info in Android Developer Site

UPDATE

Here you can found a example of Play Location Service stored in Github. Look the LocationUpdates example.

When you setup you Location Request you can change the priority, see more info here. I think that you use PRIORITY_BALANCED_POWER_ACCURACY

ClarkXP
  • 1,223
  • 10
  • 23
  • Thanks for your help @ClarkXP. May I ask you about the comment of @Ohad Zadok? Is your example better than `PassiveProvider` with respect to the battery saving and location availability of the devices? – tomloprod Jan 29 '15 at 06:20
  • @tomloprod I think that Play Services is more easy to setup and more efficient to manage the location sources and energy draining. See my update – ClarkXP Jan 29 '15 at 14:10
  • I don't agree, since the PassiveProvider does not start the GPS on it's own, it doesn't use energy at all. – Ohad Zadok Jan 31 '15 at 22:16
  • 1
    @OhadZadok This feature may be a problem when other location sources are unavailable, if the location is not available using GSM / WIFI, Play Service can activate the GPS provider, ensuring the application functionality. – ClarkXP Feb 04 '15 at 02:45
  • 1
    That is correct, and he'll have to cover that case. But in my experience for 10 minutes interval and low accuracy this rarely happens. – Ohad Zadok Feb 04 '15 at 03:57