https://github.com/googlesamples/easypermissions
dependencies {
// 运行时权限
implementation 'pub.devrel:easypermissions:1.0.1'
}
<uses-permission android:name="android.permission.READ_CALENDAR" />
<uses-permission android:name="android.permission.WRITE_CALENDAR" />
implements EasyPermissions.PermissionCallbacks
private static final int PRC_CALENDAR = 0;// 权限请求码:日历
@AfterPermissionGranted(PRC_CALENDAR)
private void requestPermissions() {// 此方法不能带参数
String[] perms = {Manifest.permission.READ_CALENDAR, Manifest.permission.WRITE_CALENDAR};
// EasyPermissions方法:检测权限
if (EasyPermissions.hasPermissions(this, perms)) {
Toast.makeText(this, "正在打开系统日历...", Toast.LENGTH_SHORT).show();// TODO
} else {
// EasyPermissions方法:请求权限
EasyPermissions.requestPermissions(this, "请授予「日历」所需要的相关权限!", PRC_CALENDAR, perms);
}
}
// 请求权限结果回调
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
// EasyPermissions方法:处理请求权限结果
EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
}
// EasyPermissions回调:权限被授予
@Override
public void onPermissionsGranted(int requestCode, List<String> perms) {
}
// EasyPermissions回调:权限被拒绝
@Override
public void onPermissionsDenied(int requestCode, List<String> perms) {
if (requestCode == PRC_CALENDAR) {
Toast.makeText(this, "您拒绝了「日历」所需要的相关权限!", Toast.LENGTH_SHORT).show();
}
}
网友评论