0
我在Android應用程序中工作,並且我在Activity
中聲明瞭方法requestAppPermissions
。Android請求權限請求只有第一個
那麼,這個代碼發生了什麼,由於某種原因,它要求第一個(在這種情況下,位置)並跳過其他的;但是,我還可以將一些其他權限第一if
的方法是請求的第一個,跳過等。
我有什麼錯我的代碼?
public void requestAppPermissions(){
if (Build.VERSION.SDK_INT >= 21) {
if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(this, android.Manifest.permission.ACCESS_FINE_LOCATION)) {
//El usuario denegó los permisos con el botón "Don't show again!!!"
startActivity(new Intent(android.provider.Settings.ACTION_APPLICATION_DETAILS_SETTINGS, Uri.parse("package:" + BuildConfig.APPLICATION_ID)));
} else {
//Se solicita el permiso al usuario
ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION}, 1);
}
}else{
LocationManager lm = (LocationManager) this.getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
Location location = lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if(location != null){
Double latitude = location.getLatitude();
Double longitude = location.getLongitude();
//new MapFunctions().RecenterMap(googleMap, new LatLng(latitude, longitude), 14, "");
LOCATION_CENTER = new LatLng(latitude, longitude);
// Toast.makeText(MainActivity.this, "latitude: " + latitude + " - longitude" + longitude, Toast.LENGTH_LONG).show();
}
}
if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(this, android.Manifest.permission.CAMERA)) {
startActivity(new Intent(android.provider.Settings.ACTION_APPLICATION_DETAILS_SETTINGS, Uri.parse("package:" + BuildConfig.APPLICATION_ID)));
} else {
ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.CAMERA}, 2);
}
}
if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(this, android.Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
startActivity(new Intent(android.provider.Settings.ACTION_APPLICATION_DETAILS_SETTINGS, Uri.parse("package:" + BuildConfig.APPLICATION_ID)));
} else {
ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, 3);
}
}
if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(this, android.Manifest.permission.READ_EXTERNAL_STORAGE)) {
startActivity(new Intent(android.provider.Settings.ACTION_APPLICATION_DETAILS_SETTINGS, Uri.parse("package:" + BuildConfig.APPLICATION_ID)));
} else {
ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.READ_EXTERNAL_STORAGE}, 4);
}
}
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case 1: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission was granted, yay! Do the
// contacts-related task you need to do.
Toast.makeText(NSActivity.this, "Feature enabled", Toast.LENGTH_SHORT).show();
} else {
// permission denied, boo! Disable the
// functionality that depends on this permission.
Toast.makeText(NSActivity.this, "Feature disabled", Toast.LENGTH_SHORT).show();
}
return;
}
case 2: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(NSActivity.this, "Feature enabled", Toast.LENGTH_SHORT).show();
} else {
// permission denied, boo! Disable the
// functionality that depends on this permission.
Toast.makeText(NSActivity.this, "Feature disabled", Toast.LENGTH_SHORT).show();
}
return;
}
case 3: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission was granted, yay! Do the
// contacts-related task you need to do.
Toast.makeText(NSActivity.this, "Feature enabled", Toast.LENGTH_SHORT).show();
} else {
// permission denied, boo! Disable the
// functionality that depends on this permission.
Toast.makeText(NSActivity.this, "Feature disabled", Toast.LENGTH_SHORT).show();
}
return;
}
case 4: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission was granted, yay! Do the
// contacts-related task you need to do.
Toast.makeText(NSActivity.this, "Feature enabled", Toast.LENGTH_SHORT).show();
} else {
// permission denied, boo! Disable the
// functionality that depends on this permission.
Toast.makeText(NSActivity.this, "Feature disabled", Toast.LENGTH_SHORT).show();
}
return;
}
}
}
你需要togheter它們堆疊在一個單一的請求。您應該檢查每個權限的狀態,如果未提供權限,則將其存儲在字符串數組中,然後最後可以發送包含您需要的所有權限的數組的單個請求。請閱讀此處:https://stackoverflow.com/a/34343101/2910520 – MatPag