普通权限直接是在清单文件中注册使用即可,在应用安装应用时,会默认获得许可。
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
通过ADB命令查看所有的危险权限的列表
adb shell pm list permissions -d -g
Activity中使用:
if (EasyPermissions.hasPermissions(this, Manifest.permission.CAMERA)) {
// 已经拥有了权限,可以执行事件
Toast.makeText(this, "TODO: Camera things", Toast.LENGTH_LONG).show();
} else {
// 没有权限,动态的申请权限
EasyPermissions.requestPermissions(this, getString(R.string.rationale_camera),
RC_CAMERA_PERM, Manifest.permission.CAMERA);
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
// EasyPermissions 直接调用权限请求的结果即可
EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
}
@Override
public void onPermissionsGranted(int requestCode, List perms) {
//动态权限的申请获取成功
Log.d(TAG, "动态权限获取成功:" + requestCode + ":" + perms.size());
}
@Override
public void onPermissionsDenied(int requestCode, List perms) {
Log.d(TAG, "动态权限获取失败:" + requestCode + ":" + perms.size());
//(可选)检查用户是否禁止权限和勾选“不再询问”;
//这将显示一个对话框,指导他们在应用程序启用权限设置。
if (EasyPermissions.somePermissionPermanentlyDenied(this, perms)) {
new AppSettingsDialog.Builder(this).setRequestCode(requestCode).build().show();
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//从系统权限设置页面回到主页面,判断之前的权限申请是否成功
if(requestCode==RC_LOCATION_CONTACTS_PERM){
locationAndContactsTask();
}
}
在Fragment中使用
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
// EasyPermissions 直接调用权限请求的结果即可
EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
}
@AfterPermissionGranted(RC_SMS_PERM)
private void smsTask() {
if (EasyPermissions.hasPermissions(getContext(), Manifest.permission.READ_SMS)) {
// 已经拥有了权限,可以执行事件
Toast.makeText(getActivity(), "TODO: SMS things", Toast.LENGTH_LONG).show();
} else {
// 没有权限,动态的申请权限
EasyPermissions.requestPermissions(this, getString(R.string.rationale_sms),
RC_SMS_PERM, Manifest.permission.READ_SMS);
}
}
@Override
public void onPermissionsGranted(int requestCode, List perms) {
//动态权限的申请获取成功
Log.d(TAG, "动态权限获取成功:" + requestCode + ":" + perms.size());
}
@Override
public void onPermissionsDenied(int requestCode, List perms) {
//动态权限的申请获取失败
Log.d(TAG, "动态权限获取失败:" + requestCode + ":" + perms.size());
}
GitHub:easypermissions