1.2 Android 6.0 新特性
1.2.2 运行时权限机制
1.处理不再询问选项
其实就是当你选择不再询问的时候,会弹出一个提示
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
if (requestCode == PERMISSIONS_REQUEST_CALL_PHONE) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
callPhone();
} else {
//当选择不再询问,shouldShowRequestPermissionRationale返回false
if (!ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.CALL_PHONE)) {
AlertDialog dialog = new AlertDialog.Builder(this)
.setMessage("该功能需要访问电话的权限,不开启将无法正常工作!")
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
}).create();
dialog.show();
}
return;
}
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
KDrqC8.png
KDrxDs.png
2.PermissionsDispatcher解析
1.什么是PermissionsDispatcher
其实就是一个权限申请框架,
2.怎么使用
1.导入依赖
implementation 'com.github.hotchemi:permissionsdispatcher:2.3.2'
annotationProcessor 'com.github.hotchemi:permissionsdispatcher-processor:2.3.2'
2.权限的讲解
- RuntimePermissions:必需的注释,它用来注册一个 Activity 或 Fragment,使它们可以处理权限。
- NeedsPermission:必需的注释,在需要获取权限的地方注释,用来获取权限。
- OnShowRationale:提示用户为何要开启此权限。在用户选择拒绝后,再次需要访问该权限时调用。
- OnPermissionDenied:用户选择拒绝时的提示。
- OnNeverAskAgain:用户选择不再询问后的提示。
3.在onRequestPermissionsResult中注册一下框架
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
//这里注意一下,要注册一下
ThirdPartyActivityPermissionsDispatcher.onRequestPermissionsResult(this, requestCode, grantResults);
}
4.代码:
//类的权限
@RuntimePermissions
public class ThirdPartyActivity extends AppCompatActivity {
private Button bt_call;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_third_party);
bt_call = (Button) this.findViewById(R.id.bt_call);
bt_call.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//检查权限
ThirdPartyActivityPermissionsDispatcher.callWithCheck(ThirdPartyActivity.this);
}
});
}
@NeedsPermission(Manifest.permission.CALL_PHONE)
//在需要获取权限的地方注释
void call() {
Intent intent = new Intent(Intent.ACTION_CALL);
Uri data = Uri.parse("tel:" + "10086");
intent.setData(data);
try {
startActivity(intent);
} catch (SecurityException e) {
e.printStackTrace();
}
}
@OnShowRationale(Manifest.permission.CALL_PHONE)
//提示用户为何要开启此权限
void showWhy(final PermissionRequest request) {
new AlertDialog.Builder(this)
.setMessage("提示用户为何要开启此权限")
.setPositiveButton("知道了", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
request.proceed();//再次执行权限请求
}
})
.show();
}
@OnPermissionDenied(Manifest.permission.CALL_PHONE)
//用户选择拒绝时的提示
void showDenied() {
Toast.makeText(this, "用户选择拒绝时的提示", Toast.LENGTH_SHORT).show();
}
@OnNeverAskAgain(Manifest.permission.CALL_PHONE)
//用户选择不再询问后的提示
void showNotAsk() {
new AlertDialog.Builder(this)
.setMessage("该功能需要访问电话的权限,不开启将无法正常工作!")
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
}).show();
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
//这里注意一下,要注册一下
ThirdPartyActivityPermissionsDispatcher.onRequestPermissionsResult(this, requestCode, grantResults);
}
}