使用Kotlin写的Android动态权限检查

沉舟侧畔千帆进,病树前头万木春。这篇文章主要讲述使用Kotlin写的Android动态权限检查相关的知识,希望能为你提供帮助。
一:权限检测器

class PermissionsChecker(context: Context) {private var mContext: Context = context.applicationContext// 判断权限集合 fun lacksPermissions(vararg permissions: String): Boolean { for (permission in permissions) { if (lacksPermission(permission)) { return true } } return false }// 判断是否缺少权限 private fun lacksPermission(permission: String): Boolean { return ContextCompat.checkSelfPermission(mContext, permission) == PackageManager.PERMISSION_DENIED } }

二:获取权限的页面
class PermissionsActivity : AppCompatActivity() {private var mChecker: PermissionsChecker? = null // 权限检测器 private var isRequireCheck: Boolean = false // 是否需要系统权限检测// 返回传递的权限参数 private val permissions: Array< String> get() = intent.getStringArrayExtra(EXTRA_PERMISSIONS)override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) if (intent == null || !intent.hasExtra(EXTRA_PERMISSIONS)) { throw RuntimeException("PermissionsActivity需要使用静态startActivityForResult方法启动!") }setContentView(R.layout.activity_permissions)//布局默认就行,不用有View,就是为了弹出框提示mChecker =PermissionsChecker(this) isRequireCheck = true }override fun onResume() { super.onResume() if (isRequireCheck) { val permissions = permissions if (mChecker!!.lacksPermissions(*permissions)) { requestPermissions(*permissions) // 请求权限 } else { allPermissionsGranted() // 全部权限都已获取 } } else { isRequireCheck = true } }// 请求权限兼容低版本 private fun requestPermissions(vararg permissions: String) { requestPermissions(this, permissions, PERMISSION_REQUEST_CODE) }// 全部权限均已获取 private fun allPermissionsGranted() { setResult(PERMISSIONS_GRANTED) finish() }/** * 用户权限处理, * 如果全部获取, 则直接过. * 如果权限缺失, 则提示Dialog. * * @param requestCode请求码 * @param permissions权限 * @param grantResults 结果 */ override fun onRequestPermissionsResult(requestCode: Int, permissions: Array< String> , grantResults: IntArray) { if (requestCode == PERMISSION_REQUEST_CODE & & hasAllPermissionsGranted(grantResults)) { isRequireCheck = true allPermissionsGranted() } else { isRequireCheck = false showMissingPermissionDialog() } }// 含有全部的权限 private fun hasAllPermissionsGranted(grantResults: IntArray): Boolean { for (grantResult in grantResults) { if (grantResult == PackageManager.PERMISSION_DENIED) { return false } } return true }// 显示缺失权限提示 private fun showMissingPermissionDialog() { val builder = AlertDialog.Builder(this@PermissionsActivity) builder.setTitle("标题") builder.setMessage("权限缺失") // 拒绝, 退出应用 builder.setNegativeButton("退出") { _, _ -> setResult(PERMISSIONS_DENIED) finish() } builder.setPositiveButton("设置") { _, _ -> startAppSettings() } builder.show() }// 启动应用的设置 private fun startAppSettings() { val intent = Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS) intent.data = https://www.songbingjia.com/android/Uri.parse(PACKAGE_URL_SCHEME + packageName) startActivity(intent) }companion object {const val PERMISSIONS_GRANTED = 0 // 权限授权 const val PERMISSIONS_DENIED = 1 // 权限拒绝private const val PERMISSION_REQUEST_CODE = 0 // 系统权限管理页面的参数 private const val EXTRA_PERMISSIONS ="motian.permission.extra_permission" // 权限参数 private const val PACKAGE_URL_SCHEME = "package:" // 方案// 启动当前权限页面的公开接口 fun startActivityForResult(activity: Activity, requestCode: Int, vararg permissions: String) { val intent = Intent(activity, PermissionsActivity::class.java) intent.putExtra(EXTRA_PERMISSIONS, permissions) startActivityForResult(activity, intent, requestCode, null) } }}

三:使用方式
class Main2Activity : AppCompatActivity() { // 请求码 private val RESULT_CODE = 0 // 所需的全部权限 var permissionListTmp = arrayOf< String> (Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE) // 权限检测器 private var mPermissionsChecker: PermissionsChecker? = null override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main2) setSupportActionBar(toolbar)mPermissionsChecker = PermissionsChecker(this) // 缺少权限时, 进入权限配置页面 if (mPermissionsChecker!!.lacksPermissions(*permissionListTmp)) { startPermissionsActivity() } }private fun startPermissionsActivity() { PermissionsActivity.startActivityForResult(this, RESULT_CODE, *permissionListTmp) }override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { super.onActivityResult(requestCode, resultCode, data) // 拒绝时, 关闭页面, 缺少主要权限, 无法运行 if (requestCode == RESULT_CODE & & resultCode == PermissionsActivity.PERMISSIONS_DENIED) { finish() } }}

【使用Kotlin写的Android动态权限检查】 

    推荐阅读