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方法
