簡體   English   中英

如何監聽 HMS Core 的分配位置許可?

[英]How can I listen assigned location permisson for the HMS Core?

我嘗試將 GMS 應用程序遷移到 HMS 應用程序,包括地圖和位置服務,但據我所知,通過華為定位工具包獲取用戶位置,用戶需要為 HMS Core 應用程序分配位置權限,但我無法遵循此權限是否已分配在我的應用程序上,當地圖准備好時,我會檢查位置,但會提示並提醒將位置權限分配給 HMS Core,但我無法在分配的此權限中收聽,並且崩潰了,所以我想問一下如何收聽用戶將位置權限分配給 HMS Core 應用程序? 或者我可以用另一種方式解決這個問題嗎? 我可以在沒有 HMS Core 應用程序位置許可的情況下獲取用戶 GPS 位置嗎? 或者有任何關於監聽用戶為HMS核心應用程序分配位置權限的回調

權限請求和管理與 GMS 應用程序完全相同,因為這是由 Android 操作系統處理的。 如果您的原始 GMS 應用程序具有檢查位置權限的代碼,則應該沒問題。 以下是如何設置 GMS 和 HMS 位置權限檢查的兩個示例。 您可以看到它們彼此非常相似。

Stack Overflow - GMS 位置權限示例https://stackoverflow.com/a/33070595/14880637

華為開發者 - HMS Location Kit 設置指南https://developer.huawei.com/

checkLocationSettings方法可能會對您有所幫助。

  1. 調用getSettingsClient()方法以獲取 SettingsClient 實例。
SettingsClient settingsClient = LocationServices.getSettingsClient(this);
  1. 調用checkLocationSettings()檢查設備位置設置。
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder();
mLocationRequest = new LocationRequest();
builder.addLocationRequest(mLocationRequest);
LocationSettingsRequest locationSettingsRequest = builder.build();
// Check the device location settings.
settingsClient.checkLocationSettings(locationSettingsRequest)
    // Define the listener for success in calling the API for checking device location settings.
    .addOnSuccessListener(new OnSuccessListener<LocationSettingsResponse>() {
        @Override
        public void onSuccess(LocationSettingsResponse locationSettingsResponse) {
                LocationSettingsStates locationSettingsStates =
                        locationSettingsResponse.getLocationSettingsStates();
                StringBuilder stringBuilder = new StringBuilder();
                // Checks whether the location function is enabled. 
                stringBuilder.append(",\nisLocationUsable=")
                        .append(locationSettingsStates.isLocationUsable());
                // Checks whether HMS Core (APK) is available. 
                stringBuilder.append(",\nisHMSLocationUsable=")
                        .append(locationSettingsStates.isHMSLocationUsable());
                Log.i(TAG, "checkLocationSetting onComplete:" + stringBuilder.toString());
        }
    })
    // Define callback for failure in checking the device location settings.
    .addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(Exception e) {
            // Processing when the device is a Huawei device and has HMS Core (APK) installed, but its settings do not meet the location requirements.
            int statusCode = ((ApiException) e).getStatusCode();
            switch (statusCode) {
                case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
                    try {
                        ResolvableApiException rae = (ResolvableApiException) e;
                        // Call startResolutionForResult to display a popup asking the user to enable related permission.
                        rae.startResolutionForResult(MainActivity.this, 0);
                    } catch (IntentSender.SendIntentException sie) {
                        // TODO
                    }
                break;
            }
        }
    });

可以查看此文檔以獲取更多信息。

暫無
暫無

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

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