我嘗試將 GMS 應用程式遷移到 HMS 應用程式,包括地圖和位置服務,但據我所知,通過華為定位工具包獲取用戶位置,用戶需要為 HMS Core 應用程式分配位置權限,但我無法遵循此權限是否已分配在我的應用程式上,當地圖準備好時,我會檢查位置,但會提示并提醒將位置權限分配給 HMS Core,但我無法在分配的此權限中收聽,并且崩潰了,所以我想問一下如何收聽用戶將位置權限分配給 HMS Core 應用程式?或者我可以用另一種方式解決這個問題嗎?我可以在沒有 HMS Core 應用程式位置許可的情況下獲取用戶 GPS 位置嗎?或者有任何關于監聽用戶為HMS核心應用程式分配位置權限的回呼
uj5u.com熱心網友回復:
權限請求和管理與 GMS 應用程式完全相同,因為這是由 Android 作業系統處理的。如果您的原始 GMS 應用程式具有檢查位置權限的代碼,則應該沒問題。以下是如何設定 GMS 和 HMS 位置權限檢查的兩個示例。您可以看到它們彼此非常相似。
Stack Overflow - GMS 位置權限示例https://stackoverflow.com/a/33070595/14880637
華為開發者 - HMS Location Kit 設定指南https://developer.huawei.com/
uj5u.com熱心網友回復:
checkLocationSettings 方法可以幫到你。
- 呼叫該
getSettingsClient()方法獲取 SettingsClient 實體。
SettingsClient settingsClient = LocationServices.getSettingsClient(this);
- 致電
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;
}
}
});
可以查看此檔案以獲取更多資訊。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/329008.html
標籤:火力基地 huawei-mobile-services huawei-developers 华为地图套件 华为定位套件
