Android 权限申请分享

要求:

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)
        }
    }
}
相关推荐
2501_9160074719 分钟前
Fastlane 结合 开心上架(Appuploader)命令行实现跨平台上传发布 iOS App 的完整方案
android·ios·小程序·https·uni-app·iphone·webview
listhi5202 小时前
Vue.js 3的组合式API
android·vue.js·flutter
用户69371750013842 小时前
🚀 Jetpack MVI 实战全解析:一次彻底搞懂 MVI 架构,让状态管理像点奶茶一样丝滑!
android·android jetpack
2501_915918414 小时前
iOS 上架应用市场全流程指南,App Store 审核机制、证书管理与跨平台免 Mac 上传发布方案(含开心上架实战)
android·macos·ios·小程序·uni-app·cocoa·iphone
峥嵘life4 小时前
Android EDLA 打开5G热点失败分析解决2
android·5g
消失的旧时光-19435 小时前
webkitx(Android WebView 最佳实践库)--> 上
android·webview
安卓兼职framework应用工程师6 小时前
android 15.0 app应用安装黑名单
android·pms·install·rom·安装黑名单
泷羽Sec-静安6 小时前
Less-7 GET-Dump into outfile-String
android·前端·网络·sql·安全·web安全
花花鱼7 小时前
html5与android之间相互调用
android
aqi008 小时前
FFmpeg开发笔记(八十八)基于Compose的国产电视直播开源框架MyTV
android·ffmpeg·音视频·直播·流媒体