- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的 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/
我现在正在创建一个位置客户端,目前我已经 public void startUpdatingLocationProcess(Context context) { parentContext
我的 FusedLocationProviderClient 在我调用后没有停止 fusedLocationClient.removeLocationUpdates(locationCallback)
我制作了单个 Activity 应用程序,它可以获取用户的当前位置,并在用户跑大约 3 米时更新当前位置,我不知道如何在最小化或销毁时保存该应用程序的状态,以便从原来的位置开始,如果在此期间一切发生变
如何强制 FusedLocationProviderClient 显式请求新位置? 几个月前,我升级了 FusedLocationProviderClient。从那以后,请求采样新位置将不会提供新位置
在 Android 中选择 LocationManager 还是 FusedLocationClient 非常简单,只需选择 FusedLocationClient,因为它可以节省电量,并且推荐作为最
我是一名优秀的程序员,十分优秀!