1、方法一
private static final int PERMISSION_REQUEST_CODE = 1; private String[]
mPermissions = {Manifest.permission.CAMERA}; /** * 申请权限结果返回处理 */ @Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[]
permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
Log.i("lgq","onRequestPermissionsResult ....."+requestCode); if (requestCode ==
PERMISSION_REQUEST_CODE) { boolean isAllGranted = true; for (int grant :
grantResults) { // 判断是否所有的权限都已经授予了 Log.i("lgq","申请权限结果===="+grant); if (grant
!= PackageManager.PERMISSION_GRANTED) { isAllGranted = false; break; } } if
(isAllGranted) { // 所有的权限都授予了 // startCamera(); Log.i("lgq","同样同意kaiqil
..onRequestPermissionsResult..."); } else {// 提示需要权限的原因 Log.i("lgq","同样反对kaiqil
..onRequestPermissionsResult..."); AlertDialog.Builder builder = new
AlertDialog.Builder(this); builder.setMessage("拍照需要允许权限, 是否再次开启?反对")
.setTitle("提示") .setPositiveButton("确认", new DialogInterface.OnClickListener()
{ @Override public void onClick(DialogInterface dialog, int which) {
ActivityCompat.requestPermissions(MainActivity.this, mPermissions,
PERMISSION_REQUEST_CODE); } }) .setNegativeButton("取消", new
DialogInterface.OnClickListener() { @Override public void
onClick(DialogInterface dialog, int which) { dialog.dismiss(); finish(); } });
builder.create().show(); } } }
 

当第一次点击禁止时:

 

2、方法二

1、添加依赖
//RxPermission权限申请 implementation
'com.tbruyelle.rxpermissions2:rxpermissions:0.9.4@aar' //RxBinding
implementation 'com.jakewharton.rxbinding2:rxbinding:2.0.0'
2、申请权限,处理申请结果
<uses-permission android:name="android.permission.INTERNET"/> <uses-permission
android:name="android.permission.READ_EXTERNAL_STORAGE"/> <uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <uses-permission
android:name="android.permission.CAMERA"/> /** * 所有需要的权限类型 */ private static
final String[] PERMISSIONS = {Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.CAMERA}; /** *
动态获取权限 */ public void getPermission() { RxPermissions rxPermissions = new
RxPermissions(MainActivity.this);
rxPermissions.request(PERMISSIONS).subscribe(new Consumer<Boolean>() {
@Override public void accept(Boolean granted) throws Exception { if(granted){
//权限申请成功 Toast.makeText(MainActivity.this,"cg",Toast.LENGTH_SHORT).show();
}else{ //权限申请不成功,弹窗提示退出
Toast.makeText(MainActivity.this,"cg222",Toast.LENGTH_SHORT).show(); } } }); }
 

 进阶方法:
implementation 'com.yanzhenjie:permission:1.1.2' public void
checkPermission(){ //先判断是否有相机权限 AndPermission.with(this) .requestCode(100)
.permission(Permission.CAMERA) .callback(listener)
.rationale(mRationaleListener) .start(); }
//用户拒绝一次权限后,再次申请时检测到已经申请过一次该权限了,允许开发者弹窗说明申请权限的目的,获取用户的同意后再申请权限,避免用户勾选不再提示,导致不能再次申请权限
private RationaleListener mRationaleListener = new RationaleListener() {
@Override public void showRequestPermissionRationale(int requestCode, final
Rationale rationale) { if(100==requestCode){ Log.i("lgq","拒绝了。。。"); } } };
//相机权限监听 PermissionListener listener = new PermissionListener() { @Override
public void onSucceed(int requestCode, @NonNull List<String> grantPermissions)
{ if(100==requestCode){//打开摄像头 Log.i("lgq","同意了了。。。"); } } @Override public
void onFailed(int requestCode, @NonNull List<String> deniedPermissions) {
if(100==requestCode){//相机 AndPermission.defaultSettingDialog(MainActivity.this,
100) .setTitle("权限申请失败") .setMessage("需相机基本权限,否则您将无法正常使用,请在设置中授权")
.setPositiveButton("好,去设置") .setNegativeButton("", new
DialogInterface.OnClickListener() { @Override public void
onClick(DialogInterface dialog, int which) { return; } }) .show(); } } };
 

技术
©2020 ioDraw All rights reserved
PHP 先返回结果到前端,PHP代码继续执行后续的任务程序员升职记第28关三排序代码中介效应、调节效应与交互作用咋回事?详解PHP中的die、exit、returnpython中的gc回收机制k8s删除podVue2.0+jsonserver+axios模拟本地请求接口数据逆向工程核心原理笔记(一)——Hello World-1spark.sql.shuffle.partitions 和 spark.default.parallelism 的区别Keras输出模型的各层的结果