从Android 6.0(API 23)开始,对系统权限做了很大的改变。在之前用户安装APP前,只是把APP需要使用的权限列出来给用户告知一下,APP安装后都可以访问这些权限。
从6.0开始,一些敏感权限,需要在使用时动态申请,并且用户可以选择拒绝授权访问这些权限,已授予过的权限,用户也可以去APP设置页面去关闭授权。这对用户来说提高了安全性,可以防止一些应用恶意访问用户数据,但是对于开发来说,也增加了不少工作量,这块不做适配处理的话,APP在访问权限的时候会容易crash。
权限等级和权限组
权限主要分为normal、dangerous、signature和signatureOrSystem四个等级,常规情况下我们只需要了解前两种,即正常权限和危险权限。
危险权限
实际开发当中主要会涉及到危险权限的配置,所有只描述有哪些危险权限的配置。
权限组 | 权限 |
---|---|
CALENDAR | READ_CALENDAR |
\ | WRITE_CALENDAR |
CAMERA | CAMERA |
CONTACTS | READ_CONTACTS |
\ | WRITE_CONTACTS |
\ | GET_ACCOUNTS |
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 |
EasyPermissions的使用
依赖
implementation 'pub.devrel:easypermissions:2.0.1'
使用案例
public class MainActivity extends AppCompatActivity implements EasyPermissions.PermissionCallbacks, EasyPermissions.RationaleCallbacks {
private Button mClick;
private int REQUEST_FILE_CODE = 99;
String[] permissions = {READ_EXTERNAL_STORAGE,WRITE_EXTERNAL_STORAGE,INTERNET};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (EasyPermissions.hasPermissions(this, permissions)) {
initView();
} else {
EasyPermissions.requestPermissions(
new PermissionRequest.Builder(this, REQUEST_FILE_CODE, permissions)
.setRationale("请确认相关权限!!")
.setPositiveButtonText("ok")
.setNegativeButtonText("cancal")
// .setTheme(R.style.my_fancy_style)
.build());
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
// Forward results to EasyPermissions
EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
}
private static final String TAG = "MainActivity";
@Override
public void onPermissionsGranted(int requestCode, @NonNull List<String> perms) {
Log.d(TAG, "onPermissionsGranted: 用户接受确认权限");
initView();
}
@Override
public void onPermissionsDenied(int requestCode, @NonNull List<String> perms) {
Log.d(TAG, "onPermissionsDenied: 用户拒绝权限确认");
}
@Override
public void onRationaleAccepted(int requestCode) {
Log.d(TAG, "onRationaleAccepted: ");
}
@Override
public void onRationaleDenied(int requestCode) {
Log.d(TAG, "onRationaleDenied: ");
}
private void initView() {
mClick = (Button) findViewById(R.id.click);
mClick.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d(TAG, "onClick: ");
}
});
}
}
网友评论