String[] perms = {Manifest.permission.READ_CONTACTS,Manifest.permission.READ_PHONE_STATE,Manifest.permission.CALL_PHONE,Manifest.permission.MODIFY_PHONE_STATE};
然后检查它们是否已被授予我运行:
for (int i = 0; i < perms.length; i++) { if(ContextCompat.checkSelfPermission(this,perms[i])!=PackageManager.PERMISSION_GRANTED) { ActivityCompat.requestPermissions(this,perms,permsRequestCode); break; } }
出于某种原因,这不起作用.它请求一次权限,然后如果我从设置中手动禁用它,它会多次调出对话框.
我该如何解决?
解决方法 使用此选项可以检查所需的任何权限,一次性是单个权限还是多个权限.public class PermissionsUtils {public static final int REQUEST_PERMISSION_MulTIPLE = 0;public static final int REQUEST_PERMISSION_CAMERA = 1;public static final int REQUEST_PERMISSION_LOCATION = 2;public static final int REQUEST_WRITE_EXTERNAL = 3;public static boolean checkAndRequestPermissions(Activity activity) { System.out.println("PermissionsUtils checkAndRequestPermissions()"); int permissionCamera = ContextCompat.checkSelfPermission(activity,Manifest.permission.CAMERA); int permissionLocation = ContextCompat.checkSelfPermission(activity,Manifest.permission.ACCESS_FINE_LOCATION); int permissionWriteExternal = ContextCompat.checkSelfPermission(activity,Manifest.permission.WRITE_EXTERNAL_STORAGE); // Permission List List<String> ListPermissionsNeeded = new ArrayList<>(); // Camera Permission if (permissionCamera != PackageManager.PERMISSION_GRANTED) { // Should we show an explanation? if (ActivityCompat.shouldShowRequestPermissionRationale(activity,Manifest.permission.CAMERA)) { Toast.makeText(activity,"Camera Permission is required for this app to run",Toast.LENGTH_SHORT) .show(); } ListPermissionsNeeded.add(Manifest.permission.CAMERA); } // Read/Write Permission if (permissionWriteExternal != PackageManager.PERMISSION_GRANTED) { ListPermissionsNeeded.add(Manifest.permission.WRITE_EXTERNAL_STORAGE); } // Location Permission if (permissionLocation != PackageManager.PERMISSION_GRANTED) { ListPermissionsNeeded.add(Manifest.permission.ACCESS_FINE_LOCATION); } if (!ListPermissionsNeeded.isEmpty()) { ActivityCompat.requestPermissions(activity,ListPermissionsNeeded.toArray(new String[ListPermissionsNeeded.size()]),REQUEST_PERMISSION_MulTIPLE); return false; } return true;}/** * Requests the Camera permission. If the permission has been denIEd * prevIoUsly,a Snackbar will prompt the user to grant the permission,* otherwise it is requested directly. */public static voID requestCameraPermission(Activity activity) { // Here,thisActivity is the current activity // System.out.println("requestCameraPermission() INITIAL"); // Toast.makeText(this,"requestCameraPermission() INITIAL",// Toast.LENGTH_LONG).show(); if (ContextCompat.checkSelfPermission(activity,Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) { // Should we show an explanation? if (ActivityCompat.shouldShowRequestPermissionRationale(activity,Manifest.permission.CAMERA)) { // Toast.makeText(activity,"Camera permission is // needed for this app to run ",// Toast.LENGTH_SHORT).show(); // System.out.println("requestCameraPermission() SHOW INFO"); // Show an explanation to the user *asynchronously* -- don't // block // this thread waiting for the user's response! After the user // sees the explanation,try again to request the permission. ActivityCompat.requestPermissions(activity,new String[] { Manifest.permission.CAMERA },REQUEST_PERMISSION_CAMERA); } else { // No explanation needed,we can request the permission. // System.out.println("requestCameraPermission() ASK // PERMISSION"); ActivityCompat.requestPermissions(activity,REQUEST_PERMISSION_CAMERA); } // Permission is granted } else { System.out.println("requestCameraPermission() PERMISSION ALREADY GRANTED"); }}public static voID requestLocationPermission(Activity activity) { if (ContextCompat.checkSelfPermission(activity,Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) { // Should we show an explanation? if (ActivityCompat.shouldShowRequestPermissionRationale(activity,Manifest.permission.ACCESS_FINE_LOCATION)) { Toast.makeText(activity,"LOCATION permission is needed to display location info ",Toast.LENGTH_SHORT) .show(); // Show an explanation to the user *asynchronously* -- don't // block this thread waiting for the user's response! After the // user sees the explanation,try again to request the // permission. ActivityCompat.requestPermissions(activity,new String[] { Manifest.permission.ACCESS_FINE_LOCATION },REQUEST_PERMISSION_LOCATION); Toast.makeText(activity,"REQUEST LOCATION PERMISSION",Toast.LENGTH_LONG).show(); } else { // No explanation needed,we can request the permission. ActivityCompat.requestPermissions(activity,REQUEST_PERMISSION_LOCATION); Toast.makeText(activity,Toast.LENGTH_LONG).show(); // MY_PERMISSIONS_REQUEST_READ_CONTACTS is an // app-defined int constant. The callback method gets the // result of the request. } // Permission is granted } else { }}public static voID requestWriteExternalPermission(Activity activity) { if (ContextCompat.checkSelfPermission(activity,Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { // Should we show an explanation? if (ActivityCompat.shouldShowRequestPermissionRationale(activity,Manifest.permission.WRITE_EXTERNAL_STORAGE)) { Toast.makeText(activity,"Write permission is needed to create Excel file ",Toast.LENGTH_SHORT).show(); // Show an explanation to the user *asynchronously* -- don't // block this thread waiting for the user's response! After the // user sees the explanation,new String[] { Manifest.permission.WRITE_EXTERNAL_STORAGE },REQUEST_WRITE_EXTERNAL); Toast.makeText(activity,we can request the permission. ActivityCompat.requestPermissions(activity,REQUEST_WRITE_EXTERNAL); } }}public static boolean hasPermissions(Context context,String... permissions) { if (androID.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && context != null && permissions != null) { for (String permission : permissions) { if (ActivityCompat.checkSelfPermission(context,permission) != PackageManager.PERMISSION_GRANTED) { return false; } } } return true;}
}
总结以上是内存溢出为你收集整理的检查用户是否已在Android中授予权限全部内容,希望文章能够帮你解决检查用户是否已在Android中授予权限所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)