Android权限管理permissions4m
程序员文章站
2022-06-05 14:19:29
...
开源permissions4m
build.gradle(project)
allprojects {
repositories {
google()
jcenter()
maven { url 'https://jitpack.io' }
}
}
build.gradle(app)
implementation 'com.github.jokermonn:permissions4m:2.1.2-lib'
annotationProcessor 'com.github.jokermonn:permissions4m:2.1.2-processor'
java
Permissions4M.get(MainActivity.this)
.requestPermissions(Manifest.permission.BLUETOOTH, Manifest.permission.BLUETOOTH_ADMIN, Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION)
.requestCodes(1, 2, 3, 4)
.requestListener(new ListenerWrapper.PermissionRequestListener() {
@Override
public void permissionGranted(int code) {
Log.e(TAG,"permissionGranted "+code);
}
@Override
public void permissionDenied(int code) {
Log.e(TAG,"permissionDenied "+code);
switch(code){
case 1:
ToastUtil.show("蓝牙通信权限被禁止~1");
break;
case 2:
ToastUtil.show("蓝牙通信权限被禁止~2");
break;
case 3:
ToastUtil.show("蓝牙定位权限被禁止~3");
break;
case 4:
ToastUtil.show("蓝牙定位权限被禁止~4");
break;
}
}
@Override
public void permissionRationale(int code) {
Log.e(TAG,"permissionRationale "+code);
switch(code){
case 1:
ToastUtil.show("申请蓝牙通信权限被拒绝~1");
break;
case 2:
ToastUtil.show("申请蓝牙通信权限被拒绝~2");
break;
case 3:
ToastUtil.show("申请蓝牙定位权限被拒绝~3");
break;
case 4:
ToastUtil.show("申请蓝牙定位权限被拒绝~4");
break;
}
}
})
.requestPageType(Permissions4M.PageType.MANAGER_PAGE)
.requestPage(new Wrapper.PermissionPageListener() {
@Override
public void pageIntent(int code, final Intent intent) {
new AlertDialog.Builder(MainActivity.this)
.setMessage("App需要开启蓝牙权限:\n请点击前往设置页面\n(in activity with listener)")
.setPositiveButton("前往设置页面", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
startActivity(intent);
}
})
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
})
.show();
}
})
.request();
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
Permissions4M.onRequestPermissionsResult(MainActivity.this, requestCode, grantResults);
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}