如何在Android M的单个请求中检查多个权限?
我想用这个
- android.permission.CAMERA
- android.permission.WRITE_EXTERNAL_STORAGE
在单个请求中使用
ActivityCompat.requestPermissions(Activity activity,new String permisionList[],int permissionRequestcode);
但我的问题是我只要求一个权限,我读了关于组的权限,但是它只为开发人员决定的相同的组,像CONTACT_GROUP : read_contact,write_contact
等。
我想创build自定义组的权限,只要求我一个请求,只给我一个响应。
谢谢
您可以在一个请求中询问多个权限(来自不同的组)。 为此,您需要将所有权限添加到您提供的string数组作为第一个参数到requestPermissions API,如下所示:
requestPermissions(new String[]{ Manifest.permission.READ_CONTACTS, Manifest.permission.ACCESS_FINE_LOCATION}, ASK_MULTIPLE_PERMISSION_REQUEST_CODE);
在这样做的时候,你会看到权限popup窗口作为多个权限popup的堆栈。 当然,您需要处理每个权限的接受和拒绝(包括“永不再问”)选项。 这里也有相同的解释。
首先初始化权限请求代码
public static final int PERMISSIONS_MULTIPLE_REQUEST = 123;
检查Android版本
private void checkAndroidVersion() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { checkPermission(); } else { // write your logic here } }
检查多个权限代码
private void checkPermission() { if (ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.READ_EXTERNAL_STORAGE) + ContextCompat .checkSelfPermission(getActivity(), Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) { if (ActivityCompat.shouldShowRequestPermissionRationale (getActivity(), Manifest.permission.READ_EXTERNAL_STORAGE) || ActivityCompat.shouldShowRequestPermissionRationale (getActivity(), Manifest.permission.CAMERA)) { Snackbar.make(getActivity().findViewById(android.R.id.content), "Please Grant Permissions to upload profile photo", Snackbar.LENGTH_INDEFINITE).setAction("ENABLE", new View.OnClickListener() { @Override public void onClick(View v) { requestPermissions( new String[]{Manifest.permission .READ_EXTERNAL_STORAGE, Manifest.permission.CAMERA}, PERMISSIONS_MULTIPLE_REQUEST); } }).show(); } else { requestPermissions( new String[]{Manifest.permission .READ_EXTERNAL_STORAGE, Manifest.permission.CAMERA}, PERMISSIONS_MULTIPLE_REQUEST); } } else { // write your logic code if permission already granted } }
用户授予许可后回拨方法
@Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { switch (requestCode) { case PERMISSIONS_MULTIPLE_REQUEST: if (grantResults.length > 0) { boolean cameraPermission = grantResults[1] == PackageManager.PERMISSION_GRANTED; boolean readExternalFile = grantResults[0] == PackageManager.PERMISSION_GRANTED; if(cameraPermission && readExternalFile) { // write your logic here } else { Snackbar.make(getActivity().findViewById(android.R.id.content), "Please Grant Permissions to upload profile photo", Snackbar.LENGTH_INDEFINITE).setAction("ENABLE", new View.OnClickListener() { @Override public void onClick(View v) { requestPermissions( new String[]{Manifest.permission .READ_EXTERNAL_STORAGE, Manifest.permission.CAMERA}, PERMISSIONS_MULTIPLE_REQUEST); } }).show(); } } break; } }
在这个阶段没有黑客可以绕过要求来自不同群体的许可。 这就是android如何开发运行时权限的本质,让用户可以select接受哪些权限。 当然,不接受一个应用程序所需的所有权限,可能会使应用程序无法正常工作。
CAMERA和WRITE_EXTERNAL_STORAGE都被认为是危险的权限 ,并且在不同的组中,因此都需要运行时权限请求。
一旦授予了特定组的权限,就不需要在应用程序运行的整个生命周期中再次请求,或者直到它作为默认设置被撤销为止。
唯一可以做的是要求用户接受默认的决定,可以通过使用“永不再问”
如前所述,目前每个权限组都有自己的权限对话框,必须单独调用。
每个权限组都有不同的对话框,但您可以一起在onRequestPermissionsResult()callback方法中检查结果。
这里是一个工作示例链接,可能对某人有用。
我有同样的问题,并偶然发现这个图书馆 。
基本上你可以依次要求多个权限,如果用户拒绝你的许可,你可以添加监听器来popup快餐栏。
https://raw.githubusercontent.com/Karumi/Dexter/master/art/sample.gif
(抱歉,没有足够的信誉来包含图片。)
我面临同样的问题,下面是我想出的解决方法:
public boolean checkForPermission(final String[] permissions, final int permRequestCode, int msgResourceId) { final List<String> permissionsNeeded = new ArrayList<>(); for (int i = 0; i < permissions.length; i++) { final String perm = permissions[i]; if (ContextCompat.checkSelfPermission(getActivity(), permissions[i]) != PackageManager.PERMISSION_GRANTED) { if (shouldShowRequestPermissionRationale(permissions[i])) { final AlertDialog dialog = AlertDialog.newInstance( getResources().getString(R.string.permission_title), getResources().getString(msgResourceId) ); dialog.setPositiveButton("OK", new View.OnClickListener() { @Override public void onClick(View view) { // add the request. permissionsNeeded.add(perm); dialog.dismiss(); } }); dialog.show( getActivity().getSupportFragmentManager(), "HCFAlertDialog" ); } else { // add the request. permissionsNeeded.add(perm); } } } if (permissionsNeeded.size() > 0) { // go ahead and request permissions requestPermissions(permissionsNeeded.toArray(new String[permissionsNeeded.size()]), permRequestCode); return false; } else { // no permission need to be asked so all good...we have them all. return true; } }
你可以这样调用上面的方法:
if ( checkForPermission( new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.CAMERA}, REQUEST_PERMISSION_EXTERNAL_STORAGE_RESULT, R.string.permission_image) ) { // DO YOUR STUFF }
对于多个权限,您可以使用此代码:
final private int REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS = 124; private void insertDummyContactWrapper() { List<String> permissionsNeeded = new ArrayList<String>(); final List<String> permissionsList = new ArrayList<String>(); if (!addPermission(permissionsList, Manifest.permission.ACCESS_FINE_LOCATION)) permissionsNeeded.add("GPS"); if (!addPermission(permissionsList, Manifest.permission.READ_CONTACTS)) permissionsNeeded.add("Read Contacts"); if (!addPermission(permissionsList, Manifest.permission.WRITE_CONTACTS)) permissionsNeeded.add("Write Contacts"); if (permissionsList.size() > 0) { if (permissionsNeeded.size() > 0) { // Need Rationale String message = "You need to grant access to " + permissionsNeeded.get(0); for (int i = 1; i < permissionsNeeded.size(); i++) message = message + ", " + permissionsNeeded.get(i); showMessageOKCancel(message, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { requestPermissions(permissionsList.toArray(new String[permissionsList.size()]), REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS); } }); return; } requestPermissions(permissionsList.toArray(new String[permissionsList.size()]), REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS); return; } insertDummyContact(); } private boolean addPermission(List<String> permissionsList, String permission) { if (checkSelfPermission(permission) != PackageManager.PERMISSION_GRANTED) { permissionsList.add(permission); // Check for Rationale Option if (!shouldShowRequestPermissionRationale(permission)) return false; } return true; }
// **For multiple permission you can use this code :** // **First:** //Write down in onCreate method. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { requestPermissions(new String[]{ android.Manifest.permission.READ_EXTERNAL_STORAGE, android.Manifest.permission.CAMERA}, MY_PERMISSIONS_REQUEST); } //**Second:** //Write down in a activity. @Override public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) { switch (requestCode) { case MY_PERMISSIONS_REQUEST: if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { new Handler().postDelayed(new Runnable() { @Override public void run() { progressBar.setVisibility(View.GONE); Intent i = new Intent(SplashActivity.this, HomeActivity.class); startActivity(i); finish(); } }, SPLASH_DISPLAY_LENGTH); } else { finish(); } return; } }