During the project development process, I came across a 6.0 system mobile phone. Then read the relevant information about 6.0 runtime permissions, and summarized as follows:
Runtime permission related API
1. Constant
PackageManager.PERMISSION_DENIED: This permission is denied.
PackageManager.PERMISSION_GRANTED: This permission is authorized.
2. Method
ContextCompat.checkSelfPermission(): 检查权限
Explanation: To check the current status of a certain permission, you should check whether the permission has been authorized by the user when requesting a certain permission. One parameter of this method is the permission name, and there is an int return value. This value can be compared with the above constant to judge the current status of the checked permission. example:
if (ContextCompat.checkSelfPermission(context, Manifest.permission.READ_CONTACTS)
!= PackageManager.PERMISSION_GRANTED) {
// 没有权限,申请权限。
}else{
// 有权限,去做该做的事。
}
ActivityCompat.requestPermissions() : 申请权限
parameter:
int requestCode: It will be returned when the callback onRequestPermissionsResult()
is used to determine which authorization request is the callback.
String[] permissions: Permission array, an array of permissions you need to apply for.
Explanation: Since this method is asynchronous, there is no return value. When the user finishes processing the authorization operation, the Activity or Fragment method will be called back onRequestPermissionsResult()
. example:
ActivityCompat.requestPermissions(context, new String[]{Manifest.permission.READ_CONTACTS}, 1);
onRequestPermissionsResult() :处理权限结果回调
parameter:
int requestCode: parameter when callingrequestPermissions()
String[] permissions: array of permissions
int[] grantResults: authorization result array
example:
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case 1: {
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// 权限被用户同意
} else {
// 权限被用户拒绝了
}
return;
}
}
}
shouldShowRequestPermissionRationale() :是否应该显示请求权限的说明
Explanation: Only if the user has already rejected your permission request last time, that is, the user has already rejected it once, and you pop up an authorization box. You need to give the user an explanation, why you want to authorize, then use this method.
private void requestPermission(Activity context, String permission) {
if (Build.VERSION.SDK_INT >= 23) {
if (ActivityCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) {//没有授权
if (ActivityCompat.shouldShowRequestPermissionRationale(context, permission)) {
showMessageDialog(context);//用户拒绝过这个权限了,应该提示用户,为什么需要这个权限
} else {
ActivityCompat.requestPermissions(activity, new String[]{requestPermission}, 1);//申请授权
}
} else {
//已经授权了,直接做事
}
} else {//小于23
//直接做该做的事
}
}
private void showMessageDialog(Activity context) {
//自定义权限弹出框
//点击确定,执行下面代码
ActivityCompat.requestPermissions(activity, new String[]{requestPermission}, 1);
}
.........
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
switch (requestCode) {
case 1:
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {//已授权
//直接操作
} else {//未授权
// 打开本应用信息界面,由用户自己手动开启这个权限
Intent intent = new Intent();
intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
Uri uri = Uri.fromParts("package", getActivity().getPackageName(), null);//this.getPackageName()
intent.setData(uri);
startActivity(intent);
}
break;
default:
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
Finally, add some knowledge:
新的权限策略讲权限分为两类,第一类是不涉及用户隐私的,只需要在Manifest中声明即可,比如网络、蓝牙、NFC等;第二类是涉及到用户隐私信息的,需要用户授权后才可使用,比如SD卡读写、联系人、短信读写等。我们在项目中,只处理危险权限,所有危险的Android系统权限属于权限组,如果APP运行在Android 6.0 (API level 23)
或者更高级别的设备中,而且targetSdkVersion>=23
时,系统将会自动采用动态权限管理策略,如果你在涉及到特殊权限操作时没有做动态权限的申请将会导致App崩溃,因此你需要注意:
1 .此类权限也必须在Manifest中申明,否则申请时不提示用户,直接回调开发者权限被拒绝。
2.同一个权限组的任何一个权限被授权了,这个权限组的其他权限也自动被授权。
需要注意的危险权限组:
group:android.permission-group.CONTACTS(联系人)
permission:android.permission.WRITE_CONTACTS
permission:android.permission.GET_ACCOUNTS
permission:android.permission.READ_CONTACTS
group:android.permission-group.PHONE(手机)
permission:android.permission.READ_CALL_LOG
permission:android.permission.READ_PHONE_STATE
permission:android.permission.CALL_PHONE
permission:android.permission.WRITE_CALL_LOG
permission:android.permission.USE_SIP
permission:android.permission.PROCESS_OUTGOING_CALLS
permission:com.android.voicemail.permission.ADD_VOICEMAIL
group:android.permission-group.CALENDAR(日历)
permission:android.permission.READ_CALENDAR
permission:android.permission.WRITE_CALENDAR
group:android.permission-group.CAMERA(相机)
permission:android.permission.CAMERA
group:android.permission-group.SENSORS(传感器)
permission:android.permission.BODY_SENSORS
group:android.permission-group.LOCATION(位置)
permission:android.permission.ACCESS_FINE_LOCATION
permission:android.permission.ACCESS_COARSE_LOCATION
group:android.permission-group.STORAGE(存储卡)
permission:android.permission.READ_EXTERNAL_STORAGE
permission:android.permission.WRITE_EXTERNAL_STORAGE
group:android.permission-group.MICROPHONE(麦克风)
permission:android.permission.RECORD_AUDIO
group:android.permission-group.SMS(短信)
permission:android.permission.READ_SMS
permission:android.permission.RECEIVE_WAP_PUSH
permission:android.permission.RECEIVE_MMS
permission:android.permission.RECEIVE_SMS
permission:android.permission.SEND_SMS
permission:android.permission.READ_CELL_BROADCASTS