【发布时间】:2019-04-24 13:43:21
【问题描述】:
我的应用程序除了其他功能外,还用于跟踪执行送货工作的司机,因此我们需要不断跟踪司机的位置。为此,我们使用在后台运行的服务,并在将坐标发送到服务器之前在内部保存坐标。
问题是,正如预期的那样,我们需要获得的精度越高,电池消耗的增加就越多。 Google Play Console Vitals 显示的唤醒次数高于平均唤醒次数,如下所示: 警告: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 是我们提供的最精确的选项。
【问题讨论】:
-
使用 JobService 启动定位服务
-
@GaneshPokale 这有什么帮助?泰
标签: android geolocation battery