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