6.0权限设置
(模拟打电话权限申请与调试)
<uses-permission android:name="android.permission.CALL_PHONE"/>
private final int CALL_PHONE_CODE = 1;
RxPermissions rxPermissions = new RxPermissions(this);
- 判断当前设备的版本号(版本兼容适配,通过代码适配方式去适配)
if(Build.VERSION.SDK_INT >= Build.VERSION_CODE.M){
//检验是否拥有以下权限
if(ContextCompat.checkSelfPermission(this,,Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED){
//未获得权限
//请求权限,调用请求权限方法,会弹出系统授权窗口
ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.CALL_PHONE},CALL_PHONE_CODE);
}else{
//已授权
Toast.makeText(this,"已授权",Toast.LENGTH_LONG).show();
callPhone();
}
}
private void callPhone() {
Intent intent = new Intent(Intent.ACTION_CALL);
Uri data = Uri.parse("tel:" + "xxxxxxxxxxx");
intent.setData(data);
startActivity(intent);
}
适配回调
/**
* 权限适配回调方法
* @param requestCode 请求码
* @param permissions 权限数组
* @param grantResults 申请权限结果
*/
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (CALL_PHONE_CODE==requestCode){//拨打电话
if(grantResults.length>0&&grantResults[0]!=PackageManager.PERMISSION_GRANTED){//拒绝了
//隐患,可能会崩溃
//引导用户去开启(1.再申请一遍权限系统窗口 2.引导手动开启)
Toast.makeText(this,"用户点击决绝",Toast.LENGTH_LONG).show();
//是否点击不再询问的选择按钮
if (!ActivityCompat.shouldShowRequestPermissionRationale(this,Manifest.permission.CALL_PHONE)){
gotoPerssionSettinng();
}else{
//只点击拒绝,,不勾选不再询问,再次请求权限
ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.CALL_PHONE},CALL_PHONE_CODE);
}
}else{//通过,,直接拨打
Toast.makeText(this,"用户点击允许",Toast.LENGTH_LONG).show();
callPhone();
}
}
}
指引跳转到设置页面
private void gotoPerssionSettinng() {
new AlertDialog.Builder(this).setTitle("权限").setMessage("如果没有权限问题")
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
gotoPerssionSettinng();//跳转到权限设置页面,让用户手动开启
}
}).show();
}
网友评论