后台定位服务会耗尽电池电量

时间:2019-04-24 13:43:22

标签: android geolocation battery

除其他功能外,我的应用还用于跟踪执行送货工作的驾驶员,因此我们需要不断跟踪驾驶员的位置。为此,我们使用在后台运行的Service,然后在将坐标发送到服务器之前在内部保存坐标。

问题是,正如预期的那样,我们需要获得的精度越高,电池消耗的电量就越大。到了这样的程度,Google Play控制台生命周期显示的唤醒次数要比平均数高: walarm :com.google.android.location.ALARM_WAKEUP_LOCATOR *

我想知道我是否做错了什么,或者我能否改善代码的唯一方法是进行细微调整。

public class LocationService extends Service implements LocationListener {

    private Integer gpsFreqInMillis = 1000 * 10; //10 seconds
    private Integer gpsFreqInDistance = 10; //10 meters
    ...

    public void startUpdatingLocation() {

        ...


        final LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

        try {
            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, gpsFreqInMillis, gpsFreqInDistance, this);
        } catch (java.lang.SecurityException ex) {
            Log.i(TAG, "fail to request location update, ignore", ex);
        } catch (IllegalArgumentException ex) {
            Log.d(TAG, "gps provider does not exist " + ex.getMessage());
        }

        try {
            locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, gpsFreqInMillis, gpsFreqInDistance, this);
        } catch (java.lang.SecurityException ex) {
            Log.i(TAG, "fail to request location update, ignore", ex);
        } catch (IllegalArgumentException ex) {
            Log.d(TAG, "gps provider does not exist " + ex.getMessage());
        }

    }

    ...

    @Override
    public void onLocationChanged(final Location newLocation) {
        //saves location in internal database
    }

}

有没有一种方法可以跟踪准确的位置而不会耗尽用户的电池?我知道我可以更改最小距离和最小时间值,但是10m / 10s是我们提供的最精确的选择。

1 个答案:

答案 0 :(得分:0)

解决此问题的唯一方法是通过将代码中的这些值更新为

来优化请求回调
private Integer gpsFreqInMillis = 1000 * 40; //10 seconds
private Integer gpsFreqInDistance = 20; //10 meters