Home > Enterprise >  How can I listen assigned location permisson for the HMS Core?
How can I listen assigned location permisson for the HMS Core?

Time:10-21

I try to migrate and GMS app to HMS app included maps and location service,But as I understand to get user location via Huawei Location kit, User need to assign location permission to HMS Core app but I can not follow this permission is assigned or not on my app , When the maps ready I check the locations but prompt and an allert about assign the location permission to HMS Core and I can not listen in this permission assigned or not and it is crash so I want to ask how can I listen User assign the location permission to HMS Core app ? or can i solve this issue with onether way ? can i get the user GPS location without location permission for HMS Core app ? or there is any callback about listen user assign the location permission for the HMS core app

CodePudding user response:

Permission request and management is exactly the same as a GMS app, as this is handled by the Android OS. If your original GMS app had the code to check the location permission, it should be fine. Here are two examples of how to setup GMS and HMS location permission checks. You can see that they are quite similar to each other.

Stack Overflow - GMS Location permission example https://stackoverflow.com/a/33070595/14880637

Huawei Developer - HMS Location Kit setup guide https://developer.huawei.com/

CodePudding user response:

checkLocationSettings method may help you.

  1. Call the getSettingsClient() method to obtain a SettingsClient instance.
SettingsClient settingsClient = LocationServices.getSettingsClient(this);
  1. Call checkLocationSettings() to check the device location settings.
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;
            }
        }
    });

Could check this Docs for more info.

  • Related