温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

android 6.0 权限授权方法

发布时间:2020-10-21 05:47:05 来源:脚本之家 阅读:119 作者:hhbbeijing 栏目:移动开发

这里介绍两种方式,如下:

一、逐次

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<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);
 }
}

@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;
  }
 }
}

以上这篇android 6.0 权限授权方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持亿速云。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI