我目前正在开发一个需要多个“危险”权限的应用程序。所以我尝试在Android Marshmallow(API Level 23)中添加“请求许可”,但找不到如何做到这一点。
如何在我的应用中使用新的权限模型请求权限?
使用以下代码打开一个对话框:
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 1);
获取活动结果如下:
@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. } else { // permission denied, boo! Disable the // functionality that depends on this permission. Toast.makeText(MainActivity.this, "Permission denied to read your External storage", Toast.LENGTH_SHORT).show(); } return; } // other 'case' lines to check for other // permissions this app might request } }
更多信息:https ://developer.android.com/training/permissions/requesting.html