检查有没有某项权限
/**
* 检查用户是否开启权限 Manifest.permission.ACCESS_COARSE_LOCATION
*
* @param activity
*/
public static Boolean checkPermission(Activity activity, String permission) {
try {
if (ContextCompat.checkSelfPermission(activity, permission)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(activity, new String[]{permission}, 1);
return false;
} else {
return true;
}
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
权限申请回调
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == 1) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
//同意授权
stopPublish();
startPublishRtmp();
} else {
//不同意授权
AllUtils.refuseVoicePermissionsDialog(this);
finish();
}
}
}
弹窗提示用户去设置权限
/**
* 用户拒绝了权限申请提醒
*/
public static void refuseVoicePermissionsDialog(final Context context) {
//当拒绝了授权后,为提升用户体验,可以以弹窗的方式引导用户到设置中去进行设置
new AlertDialog.Builder(context)
.setMessage("权限被禁止,需要开启权限才能使用此功能,请在授权管理或应用程序管理打开,否则无法正常使用")
.setPositiveButton("设置", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//引导用户到设置中去进行设置
Intent intent = new Intent();
intent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
intent.setData(Uri.fromParts("package", context.getPackageName(), null));
context.startActivity(intent);
}
})
.setNegativeButton("取消", null)
.create()
.show();
}
权限大全
http://www.cnblogs.com/classic/archive/2011/06/20/2085055.html