本文不对android权限部分的过多细节进行描述,主要写自己认为需要注意的点和用法:
普通权限和需要申请的权限
只需要在AndroidManifest.xml中声明即可,在安装应用时,会默认获得许可
需要在代码中进行权限的动态申请
该权限被分为 9 个权限组
同一组内的任意一个权限被授予,其他的权限也会被授予
例如: android.permission.READ_CONTACTS 权限被授予,那么
android.permission.WRITE_CONTACTS
android.permission.GET_ACCOUNTS
权限也别授予了
Permission Group | Permissions |
---|---|
android.permission-group.CALENDAR | android.permission.READ_CALENDAR |
android.permission.WRITE_CALENDAR | |
android.permission-group.CAMERA | android.permission.CAMERA |
android.permission-group.CONTACTS | android.permission.READ_CONTACTS |
android.permission.WRITE_CONTACTS | |
android.permission.GET_ACCOUNTS | |
android.permission-group.LOCATION | android.permission.ACCESS_FINE_LOCATION |
android.permission.ACCESS_COARSE_LOCATION | |
android.permission-group.MICROPHONE | android.permission.RECORD_AUDIO |
android.permission-group.PHONE | android.permission.READ_PHONE_STATE |
android.permission.CALL_PHONE | |
android.permission.READ_CALL_LOG | |
android.permission.WRITE_CALL_LOG | |
com.android.voicemail.permission.ADD_VOICEMAIL | |
android.permission.USE_SIP | |
android.permission.PROCESS_OUTGOING_CALLS | |
android.permission-group.SENSORS | android.permission.BODY_SENSORS |
android.permission-group.SMS | android.permission.SEND_SMS |
android.permission.RECEIVE_SMS | |
android.permission.READ_SMS | |
android.permission.RECEIVE_WAP_PUSH | |
android.permission.RECEIVE_MMS | |
android.permission.READ_CELL_BROADCASTS | |
android.permission-group.STORAGE | android.permission.READ_EXTERNAL_STORAGE |
android.permission.WRITE_EXTERNAL_STORAGE |
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.CALL_PHONE"/>
<uses-permission android:name="android.permission.SEND_SMS"/>
//所需要申请的权限数组
private static final String[] permissionsArray = new String[]{
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.CALL_PHONE,
Manifest.permission.SEND_SMS };
//还需申请的权限列表
private List permissionsList = new ArrayList();
//申请权限后的返回码
private static final int REQUEST_CODE_ASK_PERMISSIONS = 1;
private void checkRequiredPermission(final Activity activity){
for (String permission : permissionsArray) {
if (ContextCompat.checkSelfPermission(activity, permission) != PackageManager.PERMISSION_GRANTED) {
permissionsList.add(permission);
}
}
if (permissionsList.size() > 0) {
ActivityCompat.requestPermissions(activity, permissionsList.toArray(new String[permissionsList.size()]), REQUEST_CODE_ASK_PERMISSIONS);
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
switch (requestCode) {
case REQUEST_CODE_ASK_PERMISSIONS:
for (int i=0; iif (grantResults[i] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(MainActivity.this, "做一些申请成功的权限对应的事!"+permissions[i], Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "权限被拒绝: "+permissions[i], Toast.LENGTH_SHORT).show();
}
}
break;
default:
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
当要申请一些必须权限需要向用户解释下申请该权限的必要性
为了判断用户可能需要解释的情形,可以使用该方法
shouldShowRequestPermissionRationale()
当应用安装后第一次访问,返回 false
如果app之前请求过该权限,被用户拒绝, 这个方法就会返回 true.
如果用户之前拒绝权限的时候勾选了对话框中”Don’t ask again”的选项,那么这个方法会返回 false.
如果设备策略禁止应用拥有这条权限, 这个方法也返回 false.
具体是否需要向用户解释,可以根据具体情况使用该方法;但是不用使用着一步,基本都可以满足需求了