Get location (coordinates) periodically without dramatically increase battery consumption

江枫思渺然 提交于 2019-12-01 04:43:45

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);

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!