Dexter
介绍:
Dexter是一个简化运行时权限请求过程的库。
运行效果:
使用说明:
要使用这个library, 你只需用Context初始化Dexter就可以了,可以在Application中,因为它不会在app的生命周期中被销毁:
public MyApplication extends Application {
@Override public void onCreate() {
super.onCreate();
Dexter.initialize(context);
}
}
初始化之后,你就可以检查权限了。对于每个权限,注册PermissionListener来接收请求的状态:
Dexter.checkPermission(Manifest.permission.CAMERA, new PermissionListener() {
@Override public void onPermissionGranted(String permission) {/* ... */}
@Override public void onPermissionDenied(String permission) {/* ... */}
@Override public void onPermissionRationaleShouldBeShown(String permission, PermissionToken token) {/* ... */}
});
为了方便使用,我们提供了一些实现了的PermissionListener来执行重复的操作:
-
EmptyPermissionListener :方便只实现自己需要的方法。
-
DialogOnDeniedPermissionListener 当用户拒绝一个权限请求的时候,显示可配置的对话框。
PermissionListener dialogPermissionListener =
DialogOnDeniedPermissionListener.Builder.withContext(context)
.withTitle("Camera permission")
.withMessage("Camera permission is needed to take pictures of your cat")
.withButtonText(android.R.string.ok)
.withIcon(R.mipmap.my_icon)
.build();
Dexter.checkPermission(Manifest.permission.CAMERA, dialogPermissionListener);
- SnackbarOnDeniedPermissionListener 当用户拒绝一个权限请求的时候,显示一个snackbar消息。
PermissionListener snackbarPermissionListener =
SnackbarOnDeniedPermissionListener.Builder.with(rootView, "Camera access is needed to take pictures of your dog")
.withOpenSettingsButton("Settings")
.build();
Dexter.checkPermission(Manifest.permission.CAMERA, snackbarPermissionListener);
- MultiPermissionListener 把多种listeners组合成一个:
PermissionListener snackbarPermissionListener = /*...*/;
PermissionListener dialogPermissionListener = /*...*/;
Dexter.checkPermission(Manifest.permission.CAMERA, new MultiPermissionListener(snackbarPermissionListener, dialogPermissionListener, /*...*/));
IMPORTANT: Remember to follow the Google design guidelines to make your application as user-friendly as possible.
添加到项目
build.gradle
dependencies{
compile 'com.karumi:dexter:1.0.3'
}
如果使用的Maven
pom.xml
<dependency>
<groupId>com.karumi</groupId>
<artifactId>dexter</artifactId>
<version>1.0.3</version>
<type>aar</type>
</dependency>
已下载
0