我正在尝试向Android应用程序添加一个geofence。我在日志中注意到以下内容:
07-04 11:41:19.201 1945-2271/com.google.android.gms.persistent W/GeofencerStateMachine: Ignoring addGeofence because network location is disabled.
我还注意到,当我在系统设置中启用“高精度”模式时,它可以工作(这被描述为GPS、Wi-Fi、蓝牙或蜂窝网络)。它之前处于“仅限设备”模式,也就是仅限GPS。
这是geofences的预期功能吗?只有当用户的设备处于“高精度”模式时,它们才能工作吗?

发布于 2019-03-01 17:19:47
首先,对于High accuracy来说,位置的方方面面都是必要的,甚至现在谷歌地图也在使用这种模式。
因此,地理围栏将用户当前位置的感知与用户对可能感兴趣的位置的接近的感知结合在一起。要标记感兴趣的位置,请指定其纬度和经度。若要调整该位置的接近度,请添加半径。纬度、经度和半径定义了地理围栏,在感兴趣的位置周围创建了圆形区域或栅栏。这就是为什么精确定位High accuracy模式是必要的。这是官方的Doc
其次,根据文档,需要可靠的数据连接,因为Geofence依赖于它。根据this的说法,它依赖于可靠的数据连接。
根据地理围栏的配置,它可以提示移动推送通知、触发短信或警报、在社交媒体上发送有针对性的广告、允许跟踪车队、禁用某些技术或提供基于位置的营销数据。此外,有关地理围栏的详细信息,请阅读this。
发布于 2019-03-06 17:52:31
同时,为了Geofence的目的,建议从您的用户那里获得高精度权限。你可以确保提供一个高精确度的对话框,如果改变了你的用户,类似于其他位置应用程序目前所做的,如uber或ola等。
如下所示:
public void displayLocationSettingsRequest(final Activity activity, final int requestCode) {
final Task<LocationSettingsResponse> result = createLocationRequestForDialogDisplay(activity);
result.addOnCompleteListener(new OnCompleteListener<LocationSettingsResponse>() {
@Override
public void onComplete(Task<LocationSettingsResponse> task) {
try {
LocationSettingsResponse response = task.getResult(ApiException.class);
if(!InventaSdk.locUpdateStarted) {
try {
initAndCheckEligibility(); //start location updates when all settings are satisfied
} catch (SecurityException s) {
P2pLogHelper.e(TAG, s.getMessage());
}
}
// All location settings are satisfied. The client can initialize location
// requests here.
} catch (ApiException exception) {
switch (exception.getStatusCode()) {
case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
// Location settings are not satisfied. But could be fixed by showing the
// user a dialog.
try {
// Cast to a resolvable exception.
ResolvableApiException resolvable = (ResolvableApiException) exception;
// Show the dialog by calling startResolutionForResult(),
// and check the result in onActivityResult().
resolvable.startResolutionForResult(
activity,
requestCode);
} catch (IntentSender.SendIntentException e) {
// Ignore the error.
} catch (ClassCastException e) {
// Ignore, should be an impossible error.
}
break;
case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
// Location settings are not satisfied. However, we have no way to fix the
// settings so we won't show the dialog.
break;
}
}
}
});
}
private static Task<LocationSettingsResponse> createLocationRequestForDialogDisplay(Context context) {
// Create LocationSettingsRequest object using location request
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder();
builder.setNeedBle(true);
builder.setAlwaysShow(true);
builder.addLocationRequest(createLocationRequest());
LocationSettingsRequest locationSettingsRequest = builder.build();
// Check whether location settings are satisfied
// https://developers.google.com/android/reference/com/google/android/gms/location/SettingsClient
SettingsClient settingsClient = LocationServices.getSettingsClient(context);
return settingsClient.checkLocationSettings(locationSettingsRequest);
}除此之外,如果你的用户有可用的WiFi,准确率也会更高。
开启Wi-Fi的
可以显著提高定位精度,因此如果Wi-Fi关闭,您的应用程序可能永远不会收到地理围栏警报,这取决于几个设置,包括地理围栏半径、设备型号或安卓版本。从Android 4.3 (API级别18)开始,我们添加了“Wi-Fi扫描模式”的功能,允许用户禁用Wi-Fi,但仍然可以获得良好的网络位置。如果同时禁用Wi-Fi或Wi-Fi scan only模式,则最好提示用户并为其提供启用Wi-Fi或Wi-Fi scan only模式的快捷方式。使用SettingsClient确保设备的系统设置已正确配置,以实现最佳位置检测。
来源:https://developer.android.com/training/location/geofencing#Troubleshooting
https://stackoverflow.com/questions/51253428
复制相似问题