Android 动态获取权限类 PermissionUtils
1、导入第三方库:
api 'com.yanzhenjie:permission:2.0.3'
2、使用Java8
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
3、新建PermissUtils类:
import com.yanzhenjie.permission.AndPermission;
/**
* Android权限申请工具类
*/
public class PermissionUtils {
public static final String PERMISSION_RECORD_AUDIO = Manifest.permission.RECORD_AUDIO;
public static final String PERMISSION_GET_ACCOUNTS = Manifest.permission.GET_ACCOUNTS;
public static final String PERMISSION_READ_PHONE_STATE = Manifest.permission.READ_PHONE_STATE;
public static final String PERMISSION_CALL_PHONE = Manifest.permission.CALL_PHONE;
public static final String PERMISSION_CAMERA = Manifest.permission.CAMERA;
public static final String PERMISSION_ACCESS_FINE_LOCATION = Manifest.permission.ACCESS_FINE_LOCATION;
public static final String PERMISSION_ACCESS_COARSE_LOCATION = Manifest.permission.ACCESS_COARSE_LOCATION;
public static final String PERMISSION_WRITE_SEIINGS = Manifest.permission.WRITE_SETTINGS;
public static final String PERMISSION_READ_EXTERNAL_STORAGE = Manifest.permission.READ_EXTERNAL_STORAGE;
public static final String PERMISSION_WRITE_EXTERNAL_STORAGE = Manifest.permission.WRITE_EXTERNAL_STORAGE;
/**
* 注释:权限申请回调
*/
public interface PermissionListener {
void onSuccess(Context context);
void onFailed(Context context);
}
/**
* 注释:应用相关组权限
*/
public static void applicationPermissions(Context context, PermissionListener listener, String[]... permissions) {
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M) {
if (!AndPermission.hasPermissions(context, permissions)) {
AndPermission.with(context)
.runtime()
.permission(permissions)
.onGranted((permission) -> {
listener.onSuccess(context);
})
.onDenied((permission) -> {
listener.onFailed(context);
})
.start();
} else {
listener.onSuccess(context);
}
} else {
listener.onSuccess(context);
}
}
/**
* 注释:应用相关单个权限
*/
@SuppressLint("WrongConstant")
public static void applicationPermissions(Context context, PermissionListener listener, String... permissions) {
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP_MR1) {
if (!AndPermission.hasPermissions(context, permissions)) {
AndPermission.with(context)
.runtime()
.permission(permissions)
.onGranted((permission) -> {
listener.onSuccess(context);
})
.onDenied((permission) -> {
listener.onFailed(context);
})
.start();
} else {
listener.onSuccess(context);
}
} else {
listener.onSuccess(context);
}
}
}
4、使用方法:
private void requestCameraPermission(){
PermissionUtils.applicationPermissions(this, new PermissionUtils.PermissionListener() {
@Override
public void onSuccess(Context context) {
//已获取权限
}
@SuppressLint("ShowToast")
@Override
public void onFailed(Context context) {
//未获取权限,需要获取权限
if (AndPermission.hasAlwaysDeniedPermission(context, PermissionUtils.PERMISSION_CAMERA)
&& AndPermission.hasAlwaysDeniedPermission(context, PermissionUtils.PERMISSION_READ_EXTERNAL_STORAGE)
&& AndPermission.hasAlwaysDeniedPermission(context, PermissionUtils.PERMISSION_WRITE_EXTERNAL_STORAGE)) {
AndPermission.with(context).runtime().setting().start(PERMISSION_MEDIA_REQUEST_CODE);
}else {
Toast.makeText(context, "拒绝授权,请授权使用",Toast.LENGTH_SHORT);
}
finish();
}
},PermissionUtils.PERMISSION_CAMERA,PermissionUtils.PERMISSION_READ_EXTERNAL_STORAGE,PermissionUtils.PERMISSION_WRITE_EXTERNAL_STORAGE);
}