English Documentation | 中文文档
PowerPermission
主要针对 Android
开发者在面对请求RuntimePermission时的复杂流程的使用进行简化,来提高代码的便携效率。
在这个Repo已经有个示例的
application
或者你可以通过这个链接直接下载APK. 它和其他的第三方请求库不同的有以下几点:
- 可在
AppCompatActivity
和Fragment(包括ChildFragment)
中请求 - 不固定了用户拒绝权限后的说明内容表现形式,可自行化定制
- 可自主选择那些权限再次显示解释界面
- 支持多种接口配置(RxJava2和RxJava3,Coroutines,LiveData)
implementation "com.qifan.powerpermission:powerpermission:1.5.0"
implementation "com.qifan.powerpermission:powerpermission-rxjava2:1.5.0"
implementation "com.qifan.powerpermission:powerpermission-rxjava3:1.5.0"
implementation "com.qifan.powerpermission:powerpermission-coroutines:1.5.0"
implementation "com.qifan.powerpermission:powerpermission-livedata:1.5.0"
Package Name | Role | Usage |
---|---|---|
powerpermission | Basic core package | implementation "com.qifan.powerpermission:powerpermission:1.5.0" |
powerpermission-rxjava2 | Support RxJava2 | implementation "com.qifan.powerpermission:powerpermission-rxjava2:1.5.0" |
powerpermission-rxjava3 | Support RxJava3 | implementation "com.qifan.powerpermission:powerpermission-rxjava3:1.5.0" |
powerpermission-coroutines | Support Kotlin Coroutine | implementation "com.qifan.powerpermission:powerpermission-coroutines:1.5.0" |
powerpermission-livedata | Support Android LiveData | implementation "com.qifan.powerpermission:powerpermission-livedata:1.5.0" |
PowerPermission.init()
.requestPermissions(
context = this@ExampleActivity,
permissions = *arrayOf(
Manifest.permission.CAMERA
)
) { permissionResult ->
when {
permissionResult.hasAllGranted() -> {
doPermissionAllGrantedWork(permissionResult.granted())
}
permissionResult.hasRational() -> {
doPermissionReasonWork(permissionResult.rational())
}
permissionResult.hasPermanentDenied() -> {
doPermissionPermanentWork(permissionResult.permanentDenied())
}
}
}
- 在Activity请求
askPermissions(
Manifest.permission.CAMERA,
Manifest.permission.READ_CALENDAR
) { permissionResult ->
//do whatever you want
}
- 在Fragment请求
askPermissions(
Manifest.permission.CAMERA,
Manifest.permission.READ_CALENDAR
) { permissionResult ->
//do whatever you want
}
- 在ChildFragment中请求
askPermissions(
Manifest.permission.CAMERA,
Manifest.permission.READ_CALENDAR
) { permissionResult ->
//do whatever you want
}
simpleRequestButton.setOnClickListener {
askPermissionsRx(Manifest.permission.CAMERA)
}
rxBindingRequestButton.clicks()
.throttleFirst(1L, TimeUnit.SECONDS)
.flatMap {
askPermissionsRx(Manifest.permission.CAMERA)
}
simpleRequestButton.setOnClickListener {
launch {
val result = awaitAskPermissions(
Manifest.permission.CAMERA,
rationaleDelegate = dialogRationaleDelegate
)
//do something for this permission result
// handlePermissionRequest(result)
}
}
simpleRequestButton.setOnClickListener {
observeAskPermissions(
Manifest.permission.CAMERA,
rationaleDelegate = dialogRationaleDelegate
).observe(this, Observer{ result->
//do something for this permission result
// handlePermissionRequest(result)
})
}
在 PowerPermission
中它包含了一个 interface
叫做RationaleDelegate
, 你可以把它当作一个桥梁来创造你的Custom view
。
它提供了两个公开的方法,
- displayRationale
//aims to display a view to explain reason to user why request permissions
fun displayRationale(
vararg permission: Permission,
message: String,
actionCallback: RationaleActionCallback
)
- onDismissView
//aims to simply disappear view and do some clean work etc.
fun onDismissView()
PS: PowerPermission
内置一个默认的 DialogRationaleDelegate.kt
来展示android经典的dialog界面向用户解释为什么我们需要这个permission
的原因。
- RationaleData
此外他还有个
data class
被用于向用户选择那些permission
请求应该显示界面和相应的解释信息是什么。 example:
RationaleData(
rationalPermission = Manifest.permission.CAMERA,
message = message
)
RationaleData(
rationalPermissions = listOf(Manifest.permission.CAMERA),
message = message
)
Copyright (C) 2020 Qifan Yang
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.