簡體   English   中英

無法在Android中添加Geofence,因為“網絡位置已禁用”

[英]Unable to add Geofence in Android because “network location is disabled”

我正在嘗試為Android應用程序添加地理圍欄。 我在日志中注意到以下內容:

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。

這是地理圍欄的預期功能嗎? 如果用戶的設備處於“高精度”模式,它們是否只能工作?

在此輸入圖像描述

首先,對於位置的所有方面都需要High accuracy模式,即使谷歌地圖現在也使用此模式。

因此,Geofencing將用戶當前位置的意識與用戶與可能感興趣的位置的接近度的意識相結合。 要標記感興趣的位置,請指定其緯度和經度。 要調整位置的接近度,請添加半徑。 緯度,經度和半徑定義了地理圍欄,在感興趣的位置周圍創建了一個圓形區域或圍欄。 這就是精確定位所必需的原因High accuracy模式是必要的。 這是官方文件

其次根據文檔需要可靠的數據連接,因為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版本。 從Android 4.3(API級別18)開始,我們添加了“Wi-Fi僅掃描模式”功能,允許用戶禁用Wi-Fi,但仍能獲得良好的網絡位置。 最好提示用戶並為用戶提供快捷方式,以便在禁用這兩種模式時啟用Wi-Fi或Wi-Fi掃描模式。 使用SettingsClient可確保正確配置設備的系統設置,以實現最佳位置檢測。

資料來源: https//developer.android.com/training/location/geofencing#Troubleshooting

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM