gpt4 book ai didi

android - FusedLocationClient 在请求后不会停止搜索 gps

转载 作者:行者123 更新时间:2023-11-29 01:04:11 31 4
gpt4 key购买 nike

我的 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;

}

我不明白为什么它不停下来。

感谢任何帮助。

最佳答案

这是我做的:

private void startLocationUpdates() {
fusedLocationClient = new FusedLocationProviderClient(this);
locationRequest = new LocationRequest().setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
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();
}
};
try {
fusedLocationClient.requestLocationUpdates(locationRequest, locationCallback, null);
} catch (SecurityException e) {
e.printStackTrace();
}
}

private void stopLocationUpdates() {
if (fusedLocationClient != null) {
fusedLocationClient.removeLocationUpdates(locationCallback);
}
fusedLocationClient = null;
locationRequest = null;
locationCallback = null;
}

我只是取消了客户端,取消所有内容似乎可以解决问题。现在我所要做的就是调用 startLocationUpdates() 它会自行处理,然后销毁所有内容。

关于android - FusedLocationClient 在请求后不会停止搜索 gps,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48787940/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com