Dexter is an OpenHarmonyOS library that simplifies the process of requesting permissions at runtime.
Dexter to let users grant or deny permissions when running an app instead of granting them all when installing it. This approach gives the user more control over applications but requires developers to add lots of code to support it.
Dexter frees your permission code from your activities and lets you write that logic anywhere you want.
通过DevEco studio,并下载SDK 将项目中的build.gradle文件中dependencies→classpath版本改为对应的版本(即你的IDE新建项目中所用的版本)
方式二
allprojects{
repositories{
mavenCentral()
}
}
implementation 'io.openharmony.tpc.thirdlib:dexter:1.0.2'
To start using the library you just need to call Dexter
with a valid Context
,add onRequestPermissionsFromUserResult
public MainAbility extends Ability {
@Override
public void onStart(Intent intent) {
super.onStart(intent);
Dexter.withContext(this)
.withPermission(permission)
.withListener(listener)
.check();
}
}
@Override
public void onRequestPermissionsFromUserResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsFromUserResult(requestCode, permissions, grantResults);
Dexter.onRequestPermissionsFromUserResult(requestCode,permissions,grantResults);
}
For each permission, register a PermissionListener
implementation to receive the state of the request:
Dexter.withContext(this)
.withPermission(SystemPermission.CAMERA)
.withListener(new PermissionListener() {
@Override public void onPermissionGranted(PermissionGrantedResponse response) {/* ... */}
@Override public void onPermissionDenied(PermissionDeniedResponse response) {/* ... */}
@Override public void onPermissionRationaleShouldBeShown(PermissionRequest permission, PermissionToken token) {/* ... */}
}).check();
To make your life easier we offer some PermissionListener
implementations to perform recurrent actions:
BasePermissionListener
to make it easier to implement only the methods you want. Keep in mind that you should not call super
methods when overriding them.DialogOnDeniedPermissionListener
to show a configurable dialog whenever the user rejects a permission request:PermissionListener dialogPermissionListener =
DialogOnDeniedPermissionListener.Builder
.withContext(context)
.withTitle("Camera permission")
.withMessage("Camera permission is needed to take pictures of your cat")
.withButtonText("ok")
.withIcon(R.mipmap.my_icon)
.build();
PopupOnDeniedPermissionListener
to show a PopupDialog message whenever the user rejects a permission request:PermissionListener popupOnPermissionListener =
PopupOnDeniedPermissionListener.Builder
.with(view, "Camera access is needed to take pictures of your dog")
.withOpenSettingsButton("Settings")
.build();
CompositePermissionListener
to compound multiple listeners into one:PermissionListener popupOnPermissionListener = /*...*/;
PermissionListener dialogPermissionListener = /*...*/;
PermissionListener compositePermissionListener = new CompositePermissionListener(popupOnPermissionListener, dialogPermissionListener, /*...*/);
Last!
Single permission The best way.Dexter.withContext(this)
.withPermission(SystemPermission.CAMERA)
.withListener(compositePermissionListener)
.withErrorListener(new PermissionRequestErrorListener() {
@Override public void onError(DexterError error) {
Log.e("Dexter", "There was an error: " + error.toString());
}
})
.check();
//this is ability, if slice,please, at parents ability add this.
@Override
public void onRequestPermissionsFromUserResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsFromUserResult(requestCode, permissions, grantResults);
Dexter.onRequestPermissionsFromUserResult(requestCode,permissions,grantResults);
}
If you want to request multiple permissions you just need to call withPermissions
and register an implementation of MultiplePermissionsListener
:
Dexter.withContext(this)
.withPermissions(
SystemPermission.CAMERA,
SystemPermission.LOCATION
).withListener(new MultiplePermissionsListener() {
@Override public void onPermissionsChecked(MultiplePermissionsReport report) {/* ... */}
@Override public void onPermissionRationaleShouldBeShown(List<PermissionRequest> permissions, PermissionToken token) {/* ... */}
}).check();
The MultiplePermissionsReport
contains all the details of the permission request like the list of denied/granted permissions or utility methods like areAllPermissionsGranted
and isAnyPermissionPermanentlyDenied
.
As with the single permission listener, there are also some useful implementations for recurring patterns:
BaseMultiplePermissionsListener
to make it easier to implement only the methods you want. Keep in mind that you should not call super
methods when overriding them.DialogOnAnyDeniedMultiplePermissionsListener
to show a configurable dialog whenever the user rejects at least one permission:MultiplePermissionsListener dialogMultiplePermissionsListener =
DialogOnAnyDeniedMultiplePermissionsListener.Builder
.withContext(context)
.withTitle("Camera & audio permission")
.withMessage("Both camera and audio permission are needed to take pictures of your cat")
.withButtonText("ok")
.withIcon(R.mipmap.my_icon)
.build();
PopupOnAnyDeniedMultiplePermissionsListener
to show a PopupDialog message whenever the user rejects any of the requested permissions:MultiplePermissionsListener popupOnAnyDeniedMultiplePermissionsListener =
PopupOnAnyDeniedMultiplePermissionsListener.Builder
.with(view, "Camera and audio access is needed to take pictures of your dog")
.withOpenSettingsButton("Settings")
.build();
CompositePermissionListener
to compound multiple listeners into one:MultiplePermissionsListener popupOnAnyDeniedMultiplePermissionsListener = /*...*/;
MultiplePermissionsListener dialogMultiplePermissionsListener = /*...*/;
MultiplePermissionsListener compositePermissionsListener = new CompositeMultiplePermissionsListener(snackbarMultiplePermissionsListener, dialogMultiplePermissionsListener, /*...*/);
Last!
Multiple permission The best way.Dexter.withContext(this)
.withPermissions(SystemPermission.CAMERA,SystemPermission.LOCATION)
.withListener(compositePermissionsListener)
.withErrorListener(new PermissionRequestErrorListener() {
@Override public void onError(DexterError error) {
Log.e("Dexter", "There was an error: " + error.toString());
}
})
.check();
//this is ability, if slice,please, at parents ability add this.
@Override
public void onRequestPermissionsFromUserResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsFromUserResult(requestCode, permissions, grantResults);
Dexter.onRequestPermissionsFromUserResult(requestCode,permissions,grantResults);
}
If you want to receive permission listener callbacks on the same thread that fired the permission request, you just need to call onSameThread
before checking for permissions:
Dexter.withContext(context)
.withPermission(permission)
.withListener(listener)
.onSameThread()
.check();
harmony will notify you when you are requesting a permission that needs an additional explanation for its usage, either because it is considered dangerous, or because the user has already declined that permission once.
Dexter will call the method onPermissionRationaleShouldBeShown
implemented in your listener with a PermissionToken
. It's important to keep in mind that the request process will pause until the token is used, therefore, you won't be able to call Dexter again or request any other permissions if the token has not been used.
The most simple implementation of your onPermissionRationaleShouldBeShown
method could be:
@Override public void onPermissionRationaleShouldBeShown(PermissionRequest permission, PermissionToken token) {
token.continuePermissionRequest();
}
If you think there is an error in your Dexter integration, just register a PermissionRequestErrorListener
when calling Dexter:
Dexter.withContext(context)
.withPermission(permission)
.withListener(listener)
.withErrorListener(new PermissionRequestErrorListener() {
@Override public void onError(DexterError error) {
Log.e("Dexter", "There was an error: " + error.toString());
}
}).check();
The library will notify you when something bad happens. In general, it is a good practice to, at least, log every error Dexter may throw but is up to you, the developer, to do that.
Copyright 2015 Karumi
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。
1. 开源生态
2. 协作、人、软件
3. 评估模型