最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
android 6.0 权限授权方法
时间:2022-06-25 22:51:38 编辑:袖梨 来源:一聚教程网
这里介绍两种方式,如下:
一、逐次
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);
}
}
@Override
public 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 permissionsNeeded = new ArrayList();
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);
}
}
@Override
public 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;
}
}
}
相关文章
- 播种委员会公园解锁方法 公园怎么解锁 11-29
- pcl2启动器怎么加模组 模组添加方法汇总 11-29
- dnf神界版本影舞者装备搭配毕业攻略 影舞者装备怎么选 11-29
- dnf神界版本死灵术士装备搭配毕业攻略 死灵术士装备怎么选 11-29
- 洛克王国世界伏地兽进化路线是什么 洛克王国世界伏地兽进化路线攻略 11-29
- dnf神界版本刺客装备搭配毕业攻略 刺客装备怎么选 11-29