直接使用庫的方法
compile 'pub.devrel:easypermissions:0.1.9'
- 檢查權限
String[] perms = {Manifest.permission.CAMERA, Manifest.permission.CHANGE_WIFI_STATE};
if (EasyPermissions.hasPermissions(this, perms)) {
//...
} else {
//...
}
- 申請權限
EasyPermissions.requestPermissions(this, "拍照 權限",
RC_CAMERA_AND_WIFI, perms);
- 實現 EasyPermissions.PermissionCallbacks 接口,直接處理權限是否成功申請
@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);
}
//成功
@Override
public void onPermissionsGranted(int requestCode, List<String> list) {
// Some permissions have been granted
// ...
}
//失敗
@Override
public void onPermissionsDenied(int requestCode, List<String> list) {
// Some permissions have been denied
// ...
}