PermissionsDispatcher
4.8.0
PermissionsDispatcher 提供了一个简单的基于注释的 API 来处理运行时权限。
该库减轻了编写一堆检查语句(无论是否已获得您的许可)带来的负担,以保持代码的干净和安全。
这是一个最小的示例,其中您注册了一个需要Manifest.permission.CAMERA
的MainActivity
。
将以下行添加到AndroidManifest.xml
:
<uses-permission android:name="android.permission.CAMERA" />
PermissionsDispatcher 仅引入了一些注释,使其通用 API 保持简洁:
注意:带注释的方法不能是
private
。
注解 | 必需的 | 描述 |
---|---|---|
@RuntimePermissions | ✓ | 注册一个Activity 或Fragment 来处理权限 |
@NeedsPermission | ✓ | 注释执行需要一个或多个权限的操作的方法 |
@OnShowRationale | 注释一个方法来解释为什么需要权限。它传入一个PermissionRequest 对象,该对象可用于根据用户输入继续或中止当前权限请求。如果您没有为方法指定任何参数,编译器将生成process${NeedsPermissionMethodName}ProcessRequest 并cancel${NeedsPermissionMethodName}ProcessRequest 。您可以使用这些方法代替PermissionRequest (例如:使用DialogFragment ) | |
@OnPermissionDenied | 注释一个方法,如果用户未授予权限,则调用该方法 | |
@OnNeverAskAgain | 注释如果用户选择让设备“不再询问”权限则调用的方法 |
@RuntimePermissions
class MainActivity : AppCompatActivity (), View.OnClickListener {
@NeedsPermission( Manifest .permission. CAMERA )
fun showCamera () {
supportFragmentManager.beginTransaction()
.replace( R .id.sample_content_fragment, CameraPreviewFragment .newInstance())
.addToBackStack( " camera " )
.commitAllowingStateLoss()
}
@OnShowRationale( Manifest .permission. CAMERA )
fun showRationaleForCamera ( request : PermissionRequest ) {
showRationaleDialog( R .string.permission_camera_rationale, request)
}
@OnPermissionDenied( Manifest .permission. CAMERA )
fun onCameraDenied () {
Toast .makeText( this , R .string.permission_camera_denied, Toast . LENGTH_SHORT ).show()
}
@OnNeverAskAgain( Manifest .permission. CAMERA )
fun onCameraNeverAskAgain () {
Toast .makeText( this , R .string.permission_camera_never_askagain, Toast . LENGTH_SHORT ).show()
}
}
现在生成的函数比 Java 版本更加简洁直观!
override fun onCreate ( savedInstanceState : Bundle ? ) {
super .onCreate(savedInstanceState)
setContentView( R .layout.activity_main)
findViewById( R .id.button_camera).setOnClickListener {
// NOTE: delegate the permission handling to generated function
showCameraWithPermissionCheck()
}
}
override fun onRequestPermissionsResult ( requestCode : Int , permissions : Array < String >, grantResults : IntArray ) {
super .onRequestPermissionsResult(requestCode, permissions, grantResults)
// NOTE: delegate the permission handling to generated function
onRequestPermissionsResult(requestCode, grantResults)
}
查看示例以了解更多详细信息。
笔记:
要将 PermissionsDispatcher 添加到您的项目,请在应用程序模块build.gradle
文件中包含以下内容:
${latest.version}
是
dependencies {
implementation " com.github.permissions-dispatcher:permissionsdispatcher: ${ latest.version } "
annotationProcessor " com.github.permissions-dispatcher:permissionsdispatcher-processor: ${ latest.version } "
}
使用科特林:
apply plugin : ' kotlin-kapt '
dependencies {
implementation " com.github.permissions-dispatcher:permissionsdispatcher: ${ latest.version } "
kapt " com.github.permissions-dispatcher:permissionsdispatcher-processor: ${ latest.version } "
}
Copyright 2016 Shintaro Katafuchi, Marcel Schnelle, Yoshinori Isogai
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.