修改EasyPermissions默认的请求权限的弹窗,改自定义弹窗
方式一:本地加载库,直接修改源码,简单粗暴,这个就不描述了
方式二:使用hook,参考大佬的实现(还没测试,提供一个想法),传送门>>>https://juejin.cn/post/6954922378442833934
方式三:复制源码两个类改改,直接解决,废话少说,直接上代码
第一个类
//ActivityPermissionsHelper (来自源码)发起权限请求用的,我这里的host是Activity
class ActivityPermissionsHelper(
host: Activity
): PermissionsHelper<Activity>(host) {
companion object {
private var instance: ActivityPermissionsHelper? = null
fun newInstance(host: Activity): ActivityPermissionsHelper {
return instance?: synchronized(this){
instance?:ActivityPermissionsHelper(host).also {
instance = it
}
}
}
}
var dialog:RationaleDialog?=null
override var context: Context? = host
override fun directRequestPermissions(requestCode: Int, perms: Array<out String>) {
ActivityCompat.requestPermissions(host, perms, requestCode)
}
override fun shouldShowRequestPermissionRationale(perm: String): Boolean {
return ActivityCompat.shouldShowRequestPermissionRationale(host, perm)
}
override fun showRequestPermissionRationale(permissionRequest: PermissionRequest) {
dialog?.dismissDialog()
dialog = RationaleDialog(host, permissionRequest);
dialog?.showCompatDialog()
}
fun dismissDialog() {
dialog?.dismissDialog()
}
}
第二个类
//RationaleDialog(来自源码)这里就是重点了,把showDialog()方法里的Dialog,就是改成你自己的
class RationaleDialog(
private val context: Context,
private val model: PermissionRequest
) : DialogInterface.OnClickListener {
private val permissionCallbacks: EasyPermissions.PermissionCallbacks?
get() = if (context is EasyPermissions.PermissionCallbacks) context else null
private val rationaleCallbacks: EasyPermissions.RationaleCallbacks?
get() = if (context is EasyPermissions.RationaleCallbacks) context else null
private var dialog:AlertDialog? = null
/**
* Display the dialog.
*/
fun showCompatDialog() {
dialog?.dismiss()
dialog = AlertDialog.Builder(context, model.theme)
.setCancelable(false)
.setMessage(model.rationale)
.setPositiveButton(model.positiveButtonText, this)
.setNegativeButton(model.negativeButtonText, this)
.show()
dialog?.getButton(android.app.AlertDialog.BUTTON_POSITIVE)?.setTextColor(Color.BLUE)
dialog?.getButton(android.app.AlertDialog.BUTTON_NEGATIVE)?.setTextColor(Color.BLACK)
}
fun dismissDialog(){
dialog?.dismiss()
}
fun showDialog() {
android.app.AlertDialog.Builder(context, model.theme)
.setCancelable(false)
.setMessage(model.rationale)
.setPositiveButton(model.positiveButtonText, this)
.setNegativeButton(model.negativeButtonText, this)
.show()
}
override fun onClick(dialogInterface: DialogInterface?, buttonType: Int) {
when (buttonType) {
Dialog.BUTTON_POSITIVE -> {
rationaleCallbacks?.onRationaleAccepted(model.code)
when (context) {
is Fragment ->
PermissionsHelper
.newInstance(context)
.directRequestPermissions(model.code, model.perms)
is Activity ->
PermissionsHelper
.newInstance(context)
.directRequestPermissions(model.code, model.perms)
is AppCompatActivity ->
PermissionsHelper
.newInstance(context)
.directRequestPermissions(model.code, model.perms)
}
}
Dialog.BUTTON_NEGATIVE -> {
rationaleCallbacks?.onRationaleDenied(model.code)
permissionCallbacks?.onPermissionsDenied(model.code, model.perms.toList())
}
}
}
}
如何使用
//把默认使用方式:EasyPermissions.requestPermissions(this,request),换成下面这个
ActivityPermissionsHelper.newInstance(this).requestPermissions(request)
//完整的调用方式
@AfterPermissionGranted(CODE_PERMISSION_REQUEST)
private fun requestPermissions() {
if (EasyPermissions.hasPermissions(this, *perms)) {
//TODO 在这里处理你自己的业务
} else {
val request = PermissionRequest.Builder(this)
.code(CODE_PERMISSION_REQUEST)
.perms(perms)
.rationale("请授予权限")
.build()
// EasyPermissions.requestPermissions(this,request)
// 使用新增的类
ActivityPermissionsHelper.newInstance(this).requestPermissions(request)
}
}
override fun onPermissionsGranted(requestCode: Int, perms: List<String>) {
// 授权通过后,没有关闭弹窗,这里需要主动关闭一下弹窗
ActivityPermissionsHelper.newInstance(this).dismissDialog()
}
Tips:使用这个方式,要注意一下EasyPermissions.requestPermissions()方法里的 notifyAlreadyHasPermissions()方法,如果已有权限,需要处理一下权限刷新问题,可通过EasyPermissions.onRequestPermissionsResult()方法,主动处理一下。
简单思路的说明:
代码里通过EasyPermissions.requestPermissions方法,发起权限请求,让我们看看干了什么事
//默认的使用方式
val request = PermissionRequest.Builder(this)
.code(CODE_PERMISSION_REQUEST)
.perms(perms)
.rationale("请授予权限")
.build()
EasyPermissions.requestPermissions(this,request)
//EasyPermissions.requestPermissions 源码
fun requestPermissions(
host: Activity,
request: PermissionRequest
) {
// Check for permissions before dispatching the request
if (hasPermissions(host, *request.perms)) {
notifyAlreadyHasPermissions(host, request.code, request.perms)
} else {
PermissionsHelper.newInstance(host).requestPermissions(request)
}
}
这里我们只要关注这个一句代码 PermissionsHelper.newInstance(host).requestPermissions(request),这才是真正发起请求,触发弹窗的,所以我们重写这个类,照样画葫芦就可以实现了