189 8069 5689

android6.0权限授权方法

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

为疏勒等地区用户提供了全套网页设计制作服务,及疏勒网站建设行业解决方案。主营业务为网站设计制作、成都网站设计、疏勒网站设计,以传统方式定制建设网站,并提供域名空间备案等一条龙服务,秉承以专业、用心的态度为用户提供真诚的服务。我们深信只要达到每一位用户的要求,就会得到认可,从而选择与我们长期合作。这样,我们也可以走得更远!

一、逐次

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

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


名称栏目:android6.0权限授权方法
网站路径:http://cdxtjz.cn/article/gsjeho.html

其他资讯