public class PermissionUtil {
/**
* 检查某个权限。返回true表示已启用该权限,返回false表示未启用该权限
*/
public static boolean checkPermission(Activity act, String permission, int requestCode) {
boolean result = true;
// 只对Android6.0及以上系统进行校验
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
// 检查当前App是否开启了名称为permission的权限
int check = ContextCompat.checkSelfPermission(act, permission);
if (check != PackageManager.PERMISSION_GRANTED) {
// 未开启该权限,则请求系统弹窗,好让用户选择是否立即开启权限
ActivityCompat.requestPermissions(act, new String[]{permission}, requestCode);
result = false;
}
}
return result;
}
/**
* 检查多个权限。返回true表示已完全启用权限,返回false表示未完全启用权限
*/
public static boolean checkMultiPermission(Activity act, String[] permissions, int requestCode) {
boolean result = true;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
int check = PackageManager.PERMISSION_GRANTED;
// 通过权限数组检查是否都开启了这些权限
for (String permission : permissions) {
check = ContextCompat.checkSelfPermission(act, permission);
if (check != PackageManager.PERMISSION_GRANTED) {
break;
}
}
if (check != PackageManager.PERMISSION_GRANTED) {
// 未开启该权限,则请求系统弹窗,好让用户选择是否立即开启权限
ActivityCompat.requestPermissions(act, permissions, requestCode);
result = false;
}
}
return result;
}
/**
* 检查是否允许修改系统设置
*/
public static boolean checkWriteSettings(Activity act, int requestCode) {
boolean result = true;
// 只对Android6.0及Android7.0系统进行校验
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M
&& Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
// 检查当前App是否允许修改系统设置
if(!Settings.System.canWrite(act)){
Intent intent = new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS,
Uri.parse("package:" + act.getPackageName()));
act.startActivityForResult(intent, requestCode);
result = false;
}
}
return result;
}
}
首先在AndroidManifest 文件中将权限写好,以录音权限为例
<uses-permission android:name="android.permission.RECORD_AUDIO" />
if (PermissionUtil.checkPermission(this, Manifest.permission.RECORD_AUDIO, 100)) {
//操作
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == 100) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
//操作
} else {
Toast.makeText(this, "需要允许录音权限才能录音噢", Toast.LENGTH_SHORT).show();
}
}
}