https://github.com/permissions-dispatcher/PermissionsDispatcher
参考 https://blog.csdn.net/totond/article/details/73648103
导包
maven { url 'http://oss.jfrog.org/artifactory/oss-snapshot-local/' }
api("com.github.hotchemi:permissionsdispatcher:3.2.0") {
exclude module: "support-v13"
}
annotationProcessor "com.github.hotchemi:permissionsdispatcher-processor:3.2.0"
http://plugins.jetbrains.com/plugin/8349-permissionsdispatcher-plugin
@NeedsPermission(Manifest.permission....)
表示需要请求权限的方法。对于mvp模式,就自定义一个该方法,在其中mPresenter. 即可,尽量是无参方法,否则xxxWithPermissionCheck方法也需要带有参数;
@NeedsPermission(Manifest.permission.CALL_PHONE)
void dial() {
startActivity(new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + 10086)));
}
@OnShowRationale(Manifest.permission....)
@OnShowRationale(Manifest.permission.CALL_PHONE)
void showMessageForDial(final PermissionRequest request) {
new AlertDialog.Builder(getActivity())
.setMessage("告知用户为什么需要该权限")
.setPositiveButton("继续", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
request.proceed(); //点击确定则继续请求
}
})
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
request.cancel(); //否则取消请求
}
})
.show();
}
@OnPermissionDenied
在这个方法做申请权限失败之后的处理,如像用户解释为什么要申请,或者重新申请操作等
@OnPermissionDenied({Manifest.permission.CALL_PHONE})
void noCallPermission() {
TUtil.show("没有拨号权限");
}
@OnNeverAskAgain
在这个方法做申请权限失败并选择不再询问之后的处理。例如,可以告诉作者想开启权限的就从手机设置里面开启。
@OnNeverAskAgain({Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION})
void dontAsk() {
TUtil.show("如需要开启权限,请前往设置进行手动开启");
}
添加@RuntimePermissions
@RuntimePermissions
public class MarketFragment extends BaseFragment {...}
填写@NeedsPermission
方法,该方法表示有权限的时候执行的方法,写方法内容之后不需要其他操作
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
MainActivityPermissionsDispatcher.onRequestPermissionsResult(this, requestCode, grantResults);
}
MainActivityPermissionsDispatcher.dialWithPermissionCheck(this);
此处是标注了@NeedsPermission的dial方法,如果dial方法有参数,则该语句也含有参数。