目录
一、manifest 注册权限
二、代码动态申请权限
三、对勾选了“不在询问”的处理
一、manifest 注册权限
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="xxxxxxx">
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<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 String[] permissions = {
Manifest.permission.RECORD_AUDIO
, Manifest.permission.READ_EXTERNAL_STORAGE
, Manifest.permission.WRITE_EXTERNAL_STORAGE
, Manifest.permission.CAMERA
};
//未被授权的权限集合
private List<String> refusedList;
权限判断
//当前设备的系统版本>=23
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
for (String permission : permissions) {
if (ContextCompat.checkSelfPermission(getApplication(), permission)
== PackageManager.PERMISSION_DENIED) {
if (refusedList == null)
refusedList = new ArrayList<>();
refusedList.add(permission);
}
}
//当前设备系统版本<23 直接初始化
} else {
init();
}
//没有被拒绝授权的权限 直接初始化
if (refusedList == null || refusedList.size() == 0) {
init();
} else {
//请求权限
ActivityCompat.requestPermissions(this, refusedList.toArray(new String[0]), REQUEST_CODE);
}
权限申请的结果处理
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull @NotNull String[] permissions, @NonNull @NotNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
//清空
refusedList.clear();
if (requestCode == REQUEST_CODE) {
for (int i = 0; i < grantResults.length; i++) {
if (grantResults[i] == PackageManager.PERMISSION_DENIED) {
refusedList.add(permissions[i]);
}
}
}
if (refusedList.size() == 0)
init();
else
ActivityCompat.requestPermissions(this, refusedList.toArray(new String[0]), REQUEST_CODE);
}
三、对勾选了“不在询问”的处理
如果用户勾选了“不在询问”的处理
refusedList.clear();
//首次授权判断
if (requestCode == REQUEST_CODE) {
for (int i = 0; i < grantResults.length; i++) {
if (grantResults[i] == PackageManager.PERMISSION_DENIED) {
refusedList.add(permissions[i]);
}
}
}
//再次授权判断
if (requestCode == REQUEST_CODE_02) {
for (int i = 0; i < grantResults.length; i++) {
if (grantResults[i] == PackageManager.PERMISSION_DENIED) {
refusedList.add(permissions[i]);
if (!ActivityCompat.shouldShowRequestPermissionRationale(this, permissions[i])) {
AlertDialog dialog;
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("有必须权限未被授权,这将影响APP的使用,请手动授权!");
builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
//跳转到应用手动授权
Intent intent = new Intent();
intent.setAction(android.provider.Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
intent.setData(Uri.parse("package:" + MainActivity.this.getPackageName()));
startActivity(intent);
}
});
builder.setNegativeButton("退出", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
System.exit(0);
}
});
dialog = builder.create();
dialog.setCancelable(false);
dialog.show();
return;
}
}
}
}
if (refusedList.size() == 0) {
init();
} else {
ActivityCompat.requestPermissions(this, refusedList.toArray(new String[0]), REQUEST_CODE_02);
}
}
注意:
houldShowRequestPermissionRationale方法返回值分几种情况:
- 第一次请求该权限,返回false。
- 请求过该权限并被用户拒绝,返回true。
- 请求过该权限,但用户拒绝的时候勾选不再提醒,返回false。
上面的代码只是例子是存在bug的,仅供参考!
如果感觉对您有意义,就给博主一些鼓励(点赞、关注、收藏),如果有错误欢迎大家评论提出。
|