[英]How to resolve the location permission error for Apps on Huawei device?
我的華為手機上的華為 HMS 核心沒有默認權限,我正在使用 HMS 定位工具包編寫我的應用程序,並且總是收到定位工具包的權限錯誤。 我按照他們的開發指南在 Manifest 文件中設置了位置權限。
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
並遵循他們的代碼示例:
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.P) {
Log.i(TAG, "sdk < 28 Q");
if (ActivityCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
|| ActivityCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
String[] strings =
{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION};
ActivityCompat.requestPermissions(this, strings, 1);
}
} else {
if (ActivityCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
|| ActivityCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED
|| ActivityCompat.checkSelfPermission(this,
"android.permission.ACCESS_BACKGROUND_LOCATION") != PackageManager.PERMISSION_GRANTED) {
String[] strings = {android.Manifest.permission.ACCESS_FINE_LOCATION,
android.Manifest.permission.ACCESS_COARSE_LOCATION,
"android.permission.ACCESS_BACKGROUND_LOCATION"};
ActivityCompat.requestPermissions(this, strings, 2);
}
}
有什么建議嗎?
必須始終允許 HMS 權限。 否則會報錯。
確保已將位置權限分配給 HMS Core (APK)。 為此,go 到Settings > Apps > Apps並找到HMS Core 。 (菜單路徑可能因操作系統版本而異。如果沒有找到HMS Core ,請點擊應用程序右上角的菜單圖標,然后點擊顯示系統進程。)然后,點擊HMS Core(APK)圖標,go到App info > Permissions > Location ,並驗證 location 權限是否已分配給HMS Core 。 在運行 EMUI 10.0 或更高版本的設備上, HMS Core的Location必須設置為Always 。
確保下拉通知欄中的位置信息開關已打開。
模擬位置需要模擬權限。 否則會報錯權限錯誤。
Shirley 的回答涵蓋了 HMS 定位權限的設備端。 為覆蓋所有EMUI版本較低且HMS Core默認沒有定位權限的用戶,可以通過API“settingsClient.checkLocationSettings(…)”獲取設備定位權限。 之后,即使HMS Core App的位置權限被禁用,你的應用也可以一鍵提示用戶開啟相關權限。
請參考HMS 定位指南
第一步:獲取SettingsClient的服務API。
SettingsClient settingsClient = LocationServices.getSettingsClient(this);
第 2 步:調用 checkLocationSettings() 檢查設備設置。
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder();
mLocationRequest = new LocationRequest();
builder.addLocationRequest(mLocationRequest);
LocationSettingsRequest locationSettingsRequest = builder.build();
//check Location Settings
settingsClient.checkLocationSettings(locationSettingsRequest)
.addOnSuccessListener(new OnSuccessListener<LocationSettingsResponse>() {
@Override
public void onSuccess(LocationSettingsResponse locationSettingsResponse) {
//Have permissions, send requests
fusedLocationProviderClient
.requestLocationUpdates(mLocationRequest, mLocationCallback, Looper.getMainLooper())
.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
//Interface call successfully processed
}
});
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(Exception e) {
//Settings do not meet targeting criteria
int statusCode = ((ApiException) e).getStatusCode();
switch (statusCode) {
case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
try {
ResolvableApiException rae = (ResolvableApiException) e;
//Calling startResolutionForResult can pop up a window to prompt the user to open the corresponding permissions
rae.startResolutionForResult(MainActivity.this, 0);
} catch (IntentSender.SendIntentException sie) {
//…
}
break;
}
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.