Android 6.0添加
危险权限(9组24个)官方完整的权限列表
权限组名 | 权限名 |
---|---|
CALENDAR | READ_CALENDAR WRITE_CALENDAR |
CAMERA | CAMERA |
CONTACTS | READ_CONTACTS WRITE_CONTACTS GET_CONTACTS |
LOCATION | ACCESS_FINE_LOCATION ACCESS_COARSE_LOCATION |
MICROPHONE | RECORD_AUDIO |
PHONE | READ_PHONE_STATE CALL_PHONE READ_CALL_LOG WRITE_CALL_LOG ADD_VOICEMAIL USE_SIP PROCESS_OUTGOING_CALLS |
SENSORS | BODY_SENSORS |
SMS | SEND_SMS RECEIVE_SMS READ_SMS RECEIVE_WAP_PUSH RECEIVE_MMS |
STORAGE | READ_EXTERNAL_STORAGE WRITE_EXTERNAL_STORAGE |
温馨提示
- 危险权限也是要在AndroidManifest.xml中声明的;
- 授予权限给权限组内任何单一权限时,同组内的其他所有权限也会获得授权;
原生
public class MainActivity extends AppCompatActivity {
private static final int RC_PERMISSIONS= 0;
private Activity mActivity;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mActivity = this;
Button button = findViewById(R.id.btn);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//判断是否已授权
if (ContextCompat.checkSelfPermission(mActivity, Manifest.permission.WRITE_CALENDAR)
!= PackageManager.PERMISSION_GRANTED) {
//请求授权
ActivityCompat.requestPermissions(mActivity, new String[]{
Manifest.permission.WRITE_CALENDAR}, RC_PERMISSIONS);
} else {
//正常逻辑
}
}
});
}
//授权结果回调
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
@NonNull int[] grantResults) {
switch (requestCode) {
case RC_PERMISSIONS:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
//正常逻辑
} else {
//未获取权限逻辑
//(拒绝权限时,选择了“不再询问”,则后面不会再弹出权限申请弹框,而是直接执行这里的代码)
}
break;
default:
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
break;
}
}
}
EasyPermissions
public class MainActivity extends AppCompatActivity implements EasyPermissions.PermissionCallbacks {
private static final int RC_PERMISSIONS = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = findViewById(R.id.btn);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
requestPermissions();
}
});
}
@AfterPermissionGranted(RC_PERMISSIONS)
private void requestPermissions() {
String[] perms = {Manifest.permission.READ_CALENDAR};
if (EasyPermissions.hasPermissions(this, perms)) {
//正常逻辑
} else {
//未获取权限逻辑
//选择“不再询问”后,不会再显示此弹框
EasyPermissions.requestPermissions(this, "请授权必要的权限,拒绝可能无法正常使用该功能",
RC_PERMISSIONS, perms);
}
}
//授权结果回调
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
@NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
}
//申请权限成功后调用(如果有 AfterPermissionGranted 注解,此方法不会调用,直接调用注解的方法,推荐使用注解)
@Override
public void onPermissionsGranted(int requestCode, @NonNull List<String> perms) {
//正常逻辑
}
//拒绝权限
@Override
public void onPermissionsDenied(int requestCode, @NonNull List<String> perms) {
//拒绝权限且点击了“不再询问”,跳转应用设置页面
if (EasyPermissions.somePermissionPermanentlyDenied(this, perms)) {
new AppSettingsDialog.Builder(this).build().show();
}
}
}
RxPermissions
public class MainActivity extends AppCompatActivity {
final RxPermissions rxPermissions = new RxPermissions(this);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = findViewById(R.id.btn);
button.setOnClickListener(new View.OnClickListener() {
@SuppressLint("CheckResult")
@Override
public void onClick(View view) {
//申请多个权限时,只要有一个拒绝,都会返回false
rxPermissions.request(Manifest.permission.READ_CALENDAR)
.subscribe(new Consumer<Boolean>() {
@Override
public void accept(Boolean granted) throws Exception {
if (granted) {
//正常逻辑
} else {
//未获取权限逻辑(选择“不再询问”也是执行其逻辑)
}
}
});
//获取权限的具体信息 requestEach ensureEach requestEachCombined ensureEachCombined
rxPermissions.requestEach(Manifest.permission.READ_CALENDAR, Manifest.permission.CAMERA)
.subscribe(new Consumer<Permission>() {
@Override
public void accept(Permission permission) throws Exception {
if (permission.granted) { //多个权限全部同意才会执行
//正常逻辑
} else if (permission.shouldShowRequestPermissionRationale) { //只要有一个权限拒绝就会执行
//未获取权限逻辑
} else { //只要有一个权限拒绝且“不再询问”就会执行
//拒绝权限且选择“不再询问”逻辑
}
}
});
}
});
}
}