Android运行时权限

时间:2022-05-31 23:37:33

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

Normal
0

7.8 磅
0
2

false
false
false

EN-US
ZH-CN
X-NONE

/* Style Definitions */
table.MsoNormalTable
{mso-style-name:普通表格;
mso-tstyle-rowband-size:0;
mso-tstyle-colband-size:0;
mso-style-noshow:yes;
mso-style-priority:99;
mso-style-parent:"";
mso-padding-alt:0cm 5.4pt 0cm 5.4pt;
mso-para-margin:0cm;
mso-para-margin-bottom:.0001pt;
mso-pagination:widow-orphan;
font-size:10.5pt;
mso-bidi-font-size:11.0pt;
font-family:"Calibri",sans-serif;
mso-ascii-font-family:Calibri;
mso-ascii-theme-font:minor-latin;
mso-hansi-font-family:Calibri;
mso-hansi-theme-font:minor-latin;
mso-bidi-font-family:"Times New Roman";
mso-bidi-theme-font:minor-bidi;
mso-font-kerning:1.0pt;}
table.MsoTableGrid
{mso-style-name:网格型;
mso-tstyle-rowband-size:0;
mso-tstyle-colband-size:0;
mso-style-priority:59;
mso-style-unhide:no;
border:solid windowtext 1.0pt;
mso-border-alt:solid windowtext .5pt;
mso-padding-alt:0cm 5.4pt 0cm 5.4pt;
mso-border-insideh:.5pt solid windowtext;
mso-border-insidev:.5pt solid windowtext;
mso-para-margin:0cm;
mso-para-margin-bottom:.0001pt;
mso-pagination:widow-orphan;
font-size:10.5pt;
mso-bidi-font-size:11.0pt;
font-family:"Calibri",sans-serif;
mso-ascii-font-family:Calibri;
mso-ascii-theme-font:minor-latin;
mso-hansi-font-family:Calibri;
mso-hansi-theme-font:minor-latin;
mso-bidi-font-family:"Times New Roman";
mso-bidi-theme-font:minor-bidi;
mso-font-kerning:1.0pt;}

其实我们无需关系某一个权限属于哪个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方法

Normal
0

7.8 磅
0
2

false
false
false

EN-US
ZH-CN
X-NONE

/* Style Definitions */
table.MsoNormalTable
{mso-style-name:普通表格;
mso-tstyle-rowband-size:0;
mso-tstyle-colband-size:0;
mso-style-noshow:yes;
mso-style-priority:99;
mso-style-parent:"";
mso-padding-alt:0cm 5.4pt 0cm 5.4pt;
mso-para-margin:0cm;
mso-para-margin-bottom:.0001pt;
mso-pagination:widow-orphan;
font-size:10.5pt;
mso-bidi-font-size:11.0pt;
font-family:"Calibri",sans-serif;
mso-ascii-font-family:Calibri;
mso-ascii-theme-font:minor-latin;
mso-hansi-font-family:Calibri;
mso-hansi-theme-font:minor-latin;
mso-bidi-font-family:"Times New Roman";
mso-bidi-theme-font:minor-bidi;
mso-font-kerning:1.0pt;}
table.MsoTableGrid
{mso-style-name:网格型;
mso-tstyle-rowband-size:0;
mso-tstyle-colband-size:0;
mso-style-priority:59;
mso-style-unhide:no;
border:solid windowtext 1.0pt;
mso-border-alt:solid windowtext .5pt;
mso-padding-alt:0cm 5.4pt 0cm 5.4pt;
mso-border-insideh:.5pt solid windowtext;
mso-border-insidev:.5pt solid windowtext;
mso-para-margin:0cm;
mso-para-margin-bottom:.0001pt;
mso-pagination:widow-orphan;
font-size:10.5pt;
mso-bidi-font-size:11.0pt;
font-family:"Calibri",sans-serif;
mso-ascii-font-family:Calibri;
mso-ascii-theme-font:minor-latin;
mso-hansi-font-family:Calibri;
mso-hansi-theme-font:minor-latin;
mso-bidi-font-family:"Times New Roman";
mso-bidi-theme-font:minor-bidi;
mso-font-kerning:1.0pt;}