android 6.0 权限授权方法
这里介绍两种方式,如下:
一、逐次
privatestaticfinalintPERMISSION_READ_EXTERNAL_STORAGE=101;
privatestaticfinalintPERMISSION_WRITE_EXTERNAL_STORAGE=102;
privatestaticfinalintPERMISSION_CAMERA=103;
privatevoidrequestPermission(){
if(ContextCompat.checkSelfPermission(this,Manifest.permission.WRITE_EXTERNAL_STORAGE)==PackageManager.PERMISSION_DENIED){
ActivityCompat.requestPermissions(this,newString[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},PERMISSION_WRITE_EXTERNAL_STORAGE);
}
if(ContextCompat.checkSelfPermission(this,Manifest.permission.READ_EXTERNAL_STORAGE)==PackageManager.PERMISSION_DENIED){
ActivityCompat.requestPermissions(this,newString[]{Manifest.permission.READ_EXTERNAL_STORAGE},PERMISSION_READ_EXTERNAL_STORAGE);
}
if(ContextCompat.checkSelfPermission(this,Manifest.permission.CAMERA)==PackageManager.PERMISSION_DENIED){
ActivityCompat.requestPermissions(this,newString[]{Manifest.permission.CAMERA},PERMISSION_CAMERA);
}
}
@Override
publicvoidonRequestPermissionsResult(intrequestCode,Stringpermissions[],int[]grantResults){
switch(requestCode){
casePERMISSION_CAMERA:{
if(grantResults.length>0&&grantResults[0]==PackageManager.PERMISSION_GRANTED){
}else{
Toast.makeText(this,"没有摄像头权限我什么都做不了哦!",Toast.LENGTH_LONG).show();
}
break;
}
casePERMISSION_READ_EXTERNAL_STORAGE:{
if(grantResults.length>0&&grantResults[0]==PackageManager.PERMISSION_GRANTED){
}else{
Toast.makeText(this,"请打开存储读写权限,确保APP正常运行",Toast.LENGTH_LONG).show();;
}
break;
}
casePERMISSION_WRITE_EXTERNAL_STORAGE:{
if(grantResults.length>0&&grantResults[0]==PackageManager.PERMISSION_GRANTED){
}else{
Toast.makeText(this,"请打开存储读写权限,确保APP正常运行",Toast.LENGTH_LONG).show();;
}
break;
}
}
}
二、全部
privatevoidrequestPermission(){
ListpermissionsNeeded=newArrayList();
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(newString[permissionsNeeded.size()]),1);
}
}
@Override
publicvoidonRequestPermissionsResult(intrequestCode,Stringpermissions[],int[]grantResults){
switch(requestCode){
case1:{
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;
}
}
}
以上这篇android6.0权限授权方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持毛票票。