要求:
1.不用三方框架,减小安装包的大小
2.请求权限的时候,显示请求内容,配合应用市场上架要求
代码实现
调用的代码
kotlin
//初始化
private val readLauncher = PermissionInfoLauncher(Manifest.permission.READ_CONTACTS) {
"读取联系人".toast()
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
installSplashScreen()
setContentView(binding.root)
//注册
lifecycle.addObserver(readLauncher)
binding.btnReadContacts.setOnClickListener {
//具体调用
readLauncher.request(this)
}
}
override fun onDestroy() {
super.onDestroy()
//解除绑定
lifecycle.removeObserver(readLauncher)
}
核心实现类,其中 PermissionInfoDialog 、SettingPermissionDialog 是显示信息的弹框,跟设置的弹框,可以根据项目UI要求自行实现
注意Launcher 绑定,不能在OnCreate 中默认调用request,那时候Launcher还未初始化 如果遇到地图定位,在修改权限之后,还要重新自动定位,再加入
ini
setLauncher = owner.registerForActivityResult(ActivityResultContracts.StartActivityForResult())
回调即可
kotlin
class PermissionInfoLauncher(private val permission: String, private val function: () -> Unit) :
DefaultLifecycleObserver {
private lateinit var permissionLauncher: ActivityResultLauncher<String>
@StringRes
var infoRes: Int = R.string.permission_refuse_info_default
@StringRes
var setRes: Int = R.string.permission_set_default
private val infoDialog: PermissionInfoDialog by lazy {
PermissionInfoDialog.Builder().apply {
content = CoreUtils.getApp().getString(infoRes)
}.create()
}
override fun onCreate(owner: LifecycleOwner) {
super.onCreate(owner)
when (owner) {
is FragmentActivity -> {
permissionLauncher = owner.registerForActivityResult(ActivityResultContracts.RequestPermission()) { granted ->
infoDialog.dismiss()
if (granted) {
//处理业务逻辑
function()
} else {
val settingDialog = SettingPermissionDialog.Builder().apply {
content = owner.resources.getString(setRes)
}.create()
settingDialog.show(owner.supportFragmentManager)
}
}
}
else -> {
throw NullPointerException("${this.javaClass.name}观察的生命周期不是Activity或者Fragment")
}
}
}
override fun onDestroy(owner: LifecycleOwner) {
super.onDestroy(owner)
permissionLauncher.unregister()
}
fun request(activity: FragmentActivity) {
infoDialog.show(activity.supportFragmentManager)
if (::permissionLauncher.isInitialized) {
permissionLauncher.launch(permission)
}
}
}