时间:2021-05-20
这里介绍两种方式,如下:
一、逐次
private static final int PERMISSION_READ_EXTERNAL_STORAGE = 101;private static final int PERMISSION_WRITE_EXTERNAL_STORAGE = 102;private static final int PERMISSION_CAMERA = 103;private void requestPermission() { if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)== PackageManager.PERMISSION_DENIED) { ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, PERMISSION_WRITE_EXTERNAL_STORAGE); } if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_DENIED) { ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, PERMISSION_READ_EXTERNAL_STORAGE); } if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) == PackageManager.PERMISSION_DENIED) { ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, PERMISSION_CAMERA); }}@Overridepublic void onRequestPermissionsResult(int requestCode, String permissions[], int []grantResults) { switch (requestCode) { case PERMISSION_CAMERA: { if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { } else { Toast.makeText(this, "没有摄像头权限我什么都做不了哦!", Toast.LENGTH_LONG).show(); } break; } case PERMISSION_READ_EXTERNAL_STORAGE: { if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { } else { Toast.makeText(this, "请打开存储读写权限,确保APP正常运行", Toast.LENGTH_LONG).show();; } break; } case PERMISSION_WRITE_EXTERNAL_STORAGE: { if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { } else { Toast.makeText(this, "请打开存储读写权限,确保APP正常运行", Toast.LENGTH_LONG).show();; } break; } }}二、全部
private void requestPermission() { List<String> permissionsNeeded = new ArrayList<String>(); if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)== PackageManager.PERMISSION_DENIED) { permissionsNeeded.add(Manifest.permission.WRITE_EXTERNAL_STORAGE); } if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_DENIED) { permissionsNeeded.add(Manifest.permission.READ_EXTERNAL_STORAGE); } if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) == PackageManager.PERMISSION_DENIED) { permissionsNeeded.add(Manifest.permission.CAMERA); } if(permissionsNeeded.size()>0){ ActivityCompat.requestPermissions(this, permissionsNeeded.toArray(new String[permissionsNeeded.size()]), 1); }}@Overridepublic void onRequestPermissionsResult(int requestCode, String permissions[], int []grantResults) { switch (requestCode) { case 1: { if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { if (grantResults.length > 0 && grantResults[1] == PackageManager.PERMISSION_GRANTED) { if (grantResults.length > 0 && grantResults[2] == PackageManager.PERMISSION_GRANTED) { } else { dialog(); } } else { dialog(); } } else { dialog(); } break; } }}以上这篇android 6.0 权限授权方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
前言:上篇重点学习了Android6.0的运行时权限,今天还是围绕着Android6.0权限适配来总结学习,这里主要介绍一下我们公司解决Android6.0权限
最近客户更新系统发现,以前的项目在调用相机的时候,闪退掉了,很奇怪,后来查阅后发现,Android6.0以后需要程序授权相机权限,默认会给出提示,让用户授权,个
一、Android危险权限,来自官方文档的坑Android开发者都知道,Android6.0之前,权限申请只需要在AndroidManifest.xml文件中声
本文实例讲述了Android编程检测手机录音权限是否打开的方法。分享给大家供大家参考,具体如下:6.0之前的权限检测只是检测到是否在清单文件中注册Boolean
安卓手机版本在6.0以后需要动态获取相机权限1.获取相机权限2.添加依赖implementation'cn.yipianfengye.android:zxing