前几天遇到一个需求强制用户去申请某个权限,大家都知道(Api 23)以后官方提出动态权限,所以我们要提示用户打开权限,实现功能后总结了一下实现方法,下面来说一下我是怎么强制用户去申请权限的:
1.先介绍一下平时申请动态权限常用的一些API:
//用于检测权限是否打开
ContextCompat.checkSelfPermission(@NonNull Context context, @NonNull String permission) == PackageManager.PERMISSION_DENIED
//去申请权限
ActivityCompat.requestPermissions(final @NonNull Activity activity,final @NonNull String[] permissions, final @IntRange(from =0)int requestCode)
//返回权限申请结果
onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults)
2.几句话说明强制用户申请权限流程:
1.判断用户是否打开权限 checkSelfPermission()
2.如果用户没有打开权限则去申请权限 requestPermissions()
3.返回申请结果 onRequestPermissionsResult()
4.判断用户是否决绝权限 grantResult == PackageManager.PERMISSION_DENIED
5.如果用户拒绝则提示弹出Dialog提示去设置页面打开权限
6.在onActivityResult()判断全是是否打开,如果没有调用 requestPermissions()去申请权限
上面是一个循环申请的过程
3.代码图片
4.直接上代码了
public class MainActivityextends Activity {
private String[]PERMISSION =new String[]{Manifest.permission.ACCESS_FINE_LOCATION};
private static final StringTAG ="";
private static final int OPEN_SET_REQUEST_CODE =15;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initData();
}
private void initData() {
if (lacksPermissions(PERMISSION)) {
Log.i(TAG, "缺少权限去申请");
ActivityCompat.requestPermissions(this, PERMISSION, OPEN_SET_REQUEST_CODE);
}else {
Log.i(TAG, "已经全部打开权限");
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode ==OPEN_SET_REQUEST_CODE) {
if (lacksPermissions(PERMISSION)) {
Log.i(TAG, "缺少权限去申请");
ActivityCompat.requestPermissions(this, PERMISSION, OPEN_SET_REQUEST_CODE);
}
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode ==OPEN_SET_REQUEST_CODE) {
if (hasAllPermissionsGranted(grantResults)) {
Log.i(TAG, "用户允许打开权限");
}else {
Log.i(TAG, "用户拒绝打开权限");
showPermissionDialog("请去设置页面打开***权限", OPEN_SET_REQUEST_CODE);
}
}
}
/**
* 判断权限申请结果
*
* @param grantResults
* @return
*/
private boolean hasAllPermissionsGranted(@NonNull int[] grantResults) {
for (int grantResult : grantResults) {
if (grantResult == PackageManager.PERMISSION_DENIED) {//PERMISSION_GRANTED 授予
return false;
}
}
return true;
}
// 判断是否缺少权限权限集合 true=缺少权限
public boolean lacksPermissions(String... permissions) {
for (String permission : permissions) {
if (lacksPermission(permission)) {
return true;
}
}
return false;
}
// 判断是否缺少权限 true=缺少权限
public boolean lacksPermission(String permission) {
return ContextCompat.checkSelfPermission(this, permission) == PackageManager.PERMISSION_DENIED;
}
private void showPermissionDialog(String text, int requestCode) {
/*此处弹出Dialog显示内容为text(需要去设置页面打开***权限)具体Dialog我就不写了,
点击确定按钮调用toSetActivity(OPEN_SET_REQUEST_CODE)方法*/
toSetActivity(requestCode);
}
private void toSetActivity(int requestCode) {
Intent intent =new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
Uri uri = Uri.fromParts("package", getPackageName(), null);
intent.setData(uri);
startActivityForResult(intent, requestCode);
}
}