目录
导入easypermissions依赖库
在模块的的build.gradle中添加以下代码。
implementation 'pub.devrel:easypermissions:0.3.0'
然后在需要进行权限请求的activity中实现 EasyPermissions.PermissionCallbacks接口
public class WelcomeActivity extends AppCompatActivity implements EasyPermissions.PermissionCallbacks {
private static final int PERMISSION_CODE = 10000;
private static final int REQUEST_CODE = 10001;
private static final int HANDLER_DELAYED = 1000;
private final String[] permissions = {Manifest.permission.SEND_SMS, Manifest.permission.CALL_PHONE,
Manifest.permission.CAMERA, Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 版本判断。当手机系统大于 23 时,才有必要去动态申请权限
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermissions();
}
}
/**
* 申请权限的方法这个是自定义的
*/
private void requestPermissions() {
//判断有没有权限
if (EasyPermissions.hasPermissions(this, permissions)) {
// 如果有权限了, 就做你该做的事情
} else {
// 如果没有权限, 就去申请权限
// this: 上下文
// Dialog显示的正文
// PERMISSION_CODE 请求码, 用于回调的时候判断是哪次申请