【发布时间】:2018-02-14 13:03:20
【问题描述】:
我的 FusedLocationProviderClient 在我调用后没有停止
fusedLocationClient.removeLocationUpdates(locationCallback);
在我手动终止服务之前,通知栏中会无限期显示 GPS 位置图标。
我也在 onDestroy() 方法中调用 stopLocationUpdates。
要开始,我打电话:
fusedLocationClient.requestLocatonUpdates(locationRequest, locationCallback, null);
locationCallback 是:
locationCallback = new LocationCallback() {
@Override
public void onLocationResult(LocationResult locationResult) {
Location lastLocation = null;
for (Location location : locationResult.getLocations()) {
if (lastLocation != null) {
if (location.getTime() < lastLocation.getTime()) {
lastLocation = location;
}
} else {
lastLocation = location;
}
}
if (lastLocation != null) {
String msg1 = "Best location: http://www.google.com/maps/search/?api=1&query=" + lastLocation.getLatitude() + "%2C" + lastLocation.getLongitude();
sendSms(lastReceivedNumber, msg1);
}
stopLocationUpdates();
}
};
这里是stopLocationUpdates():
private void stopLocationUpdates() {
if (fusedLocationClient != null) {
fusedLocationClient.removeLocationUpdates(locationCallback);
}
fusedLocationClient = null;
}
我不明白为什么它不停。
任何帮助表示赞赏。
【问题讨论】:
-
1.) 确保您没有对可能创建重复侦听器的
startLocationUpdates()进行冗余调用 2.)removeLocationUpdates返回一个任务,它允许您检查调用是否成功 3. ) 我不认为在LocationCallback中调用stopLocationUpdates()是个好主意。
标签: android android-service fusedlocationproviderclient