我使用權限ACCESS_FINE_LOCATION
但是當run.error:要求ACCESS_FINE_LOCATION permission
爲什麼?錯誤permision ACCESS_FIND_LOCATION
在此先感謝。
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
我使用權限ACCESS_FINE_LOCATION
但是當run.error:要求ACCESS_FINE_LOCATION permission
爲什麼?錯誤permision ACCESS_FIND_LOCATION
在此先感謝。
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
ACCESS_FINE_LOCATION
和ACCESS_COARSE_LOCATION
不是添加此運行permsiion像下面的代碼時需要試圖讓位置 所以加這兩個清單文件的權限,以及如果您的定位的Android 6.0及以上版本都允許
String permission = android.Manifest.permission.ACCESS_FINE_LOCATION;
if (ActivityCompat.checkSelfPermission(SearchCityClass.this, permission)
!= PackageManager.PERMISSION_GRANTED && ActivityCompat.
checkSelfPermission(SearchCityClass.this, android.Manifest.permission.ACCESS_COARSE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(SearchCityClass.this, new String[]
{permission}, PERMISSION_GPS_CODE);
}
現在處理permisiion結果
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
@NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == PERMISSION_GPS_CODE) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(this, "location_permission_granted ", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "location_permission_not_granted ", Toast.LENGTH_SHORT).show();
}
}
}
您是否在代碼中編寫運行時權限?從Android 6.0開始,對於清單中聲明的每個權限,還必須向用戶提出運行時間。
private void requestLocationPermission() {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, LOCATION_PERMISSION_CODE);
}
}
也處理用戶的響應,以及:
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case LOCATION_PERMISSION_CODE: {
//If permission is granted
if (grantResults.length > 0 && grantResults[0] != PackageManager.PERMISSION_GRANTED) {
Toast.makeText(this, "Oops you just denied the permission", Toast.LENGTH_LONG).show();
}
return;
}
}
}
你可以分享你的代碼和錯誤? – sumit
[檢查此](https://developer.android.com/guide/topics/permissions/requesting.html) –