Android运行时权限
Android 6.0
加入了运行时权限这一概念。对于危险权限,应用必须在使用的时候进行申请。可以使用命令行查看危险权限:adb shell pm list permissions -d -g
CALENDAR |
日历
|
android.permission.READ_CALENDAR |
android.permission.WRITE_CALENDAR |
||
CAMERA |
相机 |
android.permission.CAMERA |
CONTACTS |
通讯录
|
android.permission.READ_CONTACTS |
android.permission.WRITE_CONTACTS |
||
android.permission.GET_ACCOUNTS |
||
LOCATION |
位置信息
|
android.permission.ACCESS_FINE_LOCATION |
android.permission.ACCESS_COARSE_LOCATION |
||
MICROPHONE |
麦克风 |
android.permission.RECORD_AUDIO |
PHONE |
电话
|
android.permission.READ_PHONE_STATE |
android.permission.CALL_PHONE |
||
android.permission.USE_SIP |
||
com.android.voicemail.permission.ADD_VOICEMAIL |
||
android.permission.READ_CALL_LOG |
||
android.permission.WRITE_CALL_LOG |
||
android.permission.PROCESS_OUTGOING_CALLS |
||
SENSORS |
身体传感器 |
android.permission.BODY_SENSORS |
SMS |
短信
|
android.permission.READ_SMS |
android.permission.SEND_SMS |
||
android.permission.RECEIVE_SMS |
||
android.permission.RECEIVE_MMS |
||
android.permission.RECEIVE_WAP_PUSH |
||
STORAGE |
存储空间
|
android.permission.READ_EXTERNAL_STORAGE |
android.permission.WRITE_EXTERNAL_STORAGE |
在Android P即9.0之后将原来属于Phone中的一些权限归到CALL_LOG组,如下:
CALL_LOG | 通话记录 |
android.permission.READ_CALL_LOG |
android.permission.WRITE_CALL_LOG | ||
android.permission.PROCESS_OUTGOING_CALLS |
其实我们无需关系某一个权限属于哪个Group,可以通过调用系统方法获取指定权限对应的Group名称,如下:
public static List<String> transformText(Context context, List<String> permissions) { List<String> textList = new ArrayList<>(); PackageManager pm=context.getPackageManager(); for (String permission : permissions) { String message=""; try { PermissionInfo permissionInfo = pm.getPermissionInfo(permission, 0); PermissionGroupInfo groupInfo=pm.getPermissionGroupInfo(permissionInfo.group,0); if(groupInfo!=null){ message=groupInfo.loadLabel(pm).toString(); } } catch (PackageManager.NameNotFoundException e) { e.printStackTrace(); } if (!textList.contains(message)) { textList.add(message); } } return textList; }
注意点
1.申请权限时,在Fragment中就调用Fragment对应的requestPermissions方法,而不是调用Fragment对应的Activity对应的requestPermissions方法
2.检查程序是否获得某一个权限,如果targetSdkVersion<23,使用PermissionChecker.checkSelfPermission方法,如果targetSdkVersion>=23,使用ActivityCompat.checkSelfPermission方法