Ich arbeite in einer Android-App, und ich habe die Methode requestAppPermissions
in meinem Activity
erklärt.Android-Anfrage Berechtigungen nur die erste Anfrage
Nun, was mit diesem Code passiert ist, aus irgendeinem Grund, es fragt die erste (in diesem Fall, Ort) und überspringen Sie die anderen; aber ich bewege eine andere Erlaubnis auf die erste if
die Methode fordert die erste an und überspringt die anderen.
Was habe ich falsch mit meinem Code?
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;
}
}
}
Sie müssen sie in einer einzigen Anfrage togheter stapeln. Sie sollten den Status jeder Berechtigung überprüfen, sie in einem String-Array speichern, wenn die Berechtigung nicht angegeben wurde, und am Ende können Sie eine einzige Anfrage mit dem Array senden, das alle benötigten Berechtigungen enthält. Lesen Sie hier: https://Stackoverflow.com/a/34343101/2910520 – MatPag