欢迎来到代码驿站!

Android代码

当前位置:首页 > 移动开发 > Android代码

android 6.0 权限授权方法

时间:2020-11-22 23:16:53|栏目:Android代码|点击:

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

一、逐次

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实现分享长图并且添加全图水印

栏    目:Android代码

下一篇:理解Android中的自定义属性

本文标题:android 6.0 权限授权方法

本文地址:http://www.codeinn.net/misctech/25285.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有