android 6.0 权限授权方法
更新时间:2018年07月30日 14:34:00 作者:hhbbeijing
今天小编就为大家分享一篇android 6.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<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 权限授权方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。
相关文章
Android编程基础之获取手机屏幕大小(DisplayMetrics应用)示例
这篇文章主要介绍了Android获取手机屏幕大小的方法,结合实例形式分析了TextView ,Button ,以及DisplayMetrics的使用方法,并重点分析了DisplayMetrics的应用技巧,需要的朋友可以参考下2016-10-10Android AndBase框架使用封装好的函数完成Http请求(三)
这篇文章主要介绍了Android AndBase框架使用封装好的函数完成Http请求的相关资料,感兴趣的小伙伴们可以参考一下2016-03-03android 获取APP的唯一标识applicationId的实例
下面小编就为大家分享一篇android 获取APP的唯一标识applicationId的实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧2018-02-02在Android中使用Anntation来代替ENUM的方法
本篇文章主要介绍了在Android中使用Anntation来代替ENUM的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧2018-02-02
最新评论