[关闭]
@cxm-2016 2017-03-08T12:59:32.000000Z 字数 5062 阅读 4341

Kotlin:一行代码搞定Android6.0的权限申请

Android 0

版本:0
作者:陈小默
声明:禁止转载


在Android API 23以上的操作系统上进行权限申请的步骤虽然简单,但是如果要在每一处使用权限的地方进行申请就很麻烦了。这里就分享一种利用Kotlin的扩展机制实现的一行代码搞定权限申请的实现,这里仅用读取GPS的例子来简要说明实现,对于其他权限申请,其实现过程是相同的。

下面先来看一看实现后的效果:

  1. locationPermission { granted: Boolean ->
  2. enableLocation(granted)
  3. }

精简后可以写成

  1. locationPermission { enableLocation(it) }

权限工具类

下面先放工具类代码

PermissionUtil.kt

  1. private var permissionCode = 0
  2. data class _Permission(val granted: (Boolean) -> Unit)
  3. object PermissionManager {
  4. val permissionList = HashMap<Int, _Permission>()
  5. fun onRequestPermissionsResult(activity: Activity, requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
  6. if (permissionList.containsKey(requestCode)) {
  7. val granted = permissionList.remove(requestCode) ?: return
  8. val isTip = ActivityCompat.shouldShowRequestPermissionRationale(activity, permissions[0])
  9. val isDenied = grantResults.isNotEmpty() && grantResults[0] != PackageManager.PERMISSION_GRANTED
  10. if (isDenied) {
  11. if (isTip) {
  12. val code = ++permissionCode
  13. PermissionManager.permissionList[requestCode] = granted
  14. ActivityCompat.requestPermissions(activity, permissions, code)
  15. } else {
  16. granted.granted.invoke(false)
  17. }
  18. } else
  19. granted.granted.invoke(true)
  20. }
  21. }
  22. }
  23. fun Activity.locationPermission(granted: (Boolean) -> Unit) {
  24. if (ContextCompat.checkSelfPermission(this, Manifest.permission_group.LOCATION) == PackageManager.PERMISSION_GRANTED)
  25. granted(true)
  26. else {
  27. val requestCode = ++permissionCode
  28. PermissionManager.permissionList[requestCode] = _Permission(granted)
  29. ActivityCompat.requestPermissions(this, arrayOf(
  30. Manifest.permission.ACCESS_COARSE_LOCATION,
  31. Manifest.permission.ACCESS_FINE_LOCATION), requestCode)
  32. }
  33. }
  34. fun Activity.storagePermission(granted: (Boolean) -> Unit) {
  35. if (ContextCompat.checkSelfPermission(this, Manifest.permission_group.STORAGE) == PackageManager.PERMISSION_GRANTED)
  36. granted(true)
  37. else {
  38. val requestCode = ++permissionCode
  39. PermissionManager.permissionList[requestCode] = _Permission(granted)
  40. ActivityCompat.requestPermissions(this, arrayOf(
  41. Manifest.permission.READ_EXTERNAL_STORAGE,
  42. Manifest.permission.WRITE_EXTERNAL_STORAGE), requestCode)
  43. }
  44. }
  45. fun Activity.contactsPermission(granted: (Boolean) -> Unit) {
  46. if (ContextCompat.checkSelfPermission(this, Manifest.permission_group.CONTACTS) == PackageManager.PERMISSION_GRANTED)
  47. granted(true)
  48. else {
  49. val requestCode = ++permissionCode
  50. PermissionManager.permissionList[requestCode] = _Permission(granted)
  51. ActivityCompat.requestPermissions(this, arrayOf(
  52. Manifest.permission.READ_CONTACTS,
  53. Manifest.permission.GET_ACCOUNTS,
  54. Manifest.permission.WRITE_CONTACTS), requestCode)
  55. }
  56. }
  57. fun Activity.phonePermission(granted: (Boolean) -> Unit) {
  58. if (ContextCompat.checkSelfPermission(this, Manifest.permission_group.PHONE) == PackageManager.PERMISSION_GRANTED)
  59. granted(true)
  60. else {
  61. val requestCode = ++permissionCode
  62. PermissionManager.permissionList[requestCode] = _Permission(granted)
  63. ActivityCompat.requestPermissions(this, arrayOf(
  64. Manifest.permission.READ_CALL_LOG,
  65. Manifest.permission.READ_PHONE_STATE,
  66. Manifest.permission.CALL_PHONE,
  67. Manifest.permission.WRITE_CALL_LOG,
  68. Manifest.permission.USE_SIP,
  69. Manifest.permission.PROCESS_OUTGOING_CALLS,
  70. Manifest.permission.ADD_VOICEMAIL), requestCode)
  71. }
  72. }
  73. fun Activity.calendarPermission(granted: (Boolean) -> Unit) {
  74. if (ContextCompat.checkSelfPermission(this, Manifest.permission_group.CALENDAR) == PackageManager.PERMISSION_GRANTED)
  75. granted(true)
  76. else {
  77. val requestCode = ++permissionCode
  78. PermissionManager.permissionList[requestCode] = _Permission(granted)
  79. ActivityCompat.requestPermissions(this, arrayOf(
  80. Manifest.permission.READ_CALENDAR,
  81. Manifest.permission.WRITE_CALENDAR), requestCode)
  82. }
  83. }
  84. fun Activity.cameraPermission(granted: (Boolean) -> Unit) {
  85. if (ContextCompat.checkSelfPermission(this, Manifest.permission_group.CAMERA) == PackageManager.PERMISSION_GRANTED)
  86. granted(true)
  87. else {
  88. val requestCode = ++permissionCode
  89. PermissionManager.permissionList[requestCode] = _Permission(granted)
  90. ActivityCompat.requestPermissions(this, arrayOf(
  91. Manifest.permission.CAMERA), requestCode)
  92. }
  93. }
  94. fun Activity.sensorsPermission(granted: (Boolean) -> Unit) {
  95. if (ContextCompat.checkSelfPermission(this, Manifest.permission_group.SENSORS) == PackageManager.PERMISSION_GRANTED)
  96. granted(true)
  97. else {
  98. val requestCode = ++permissionCode
  99. PermissionManager.permissionList[requestCode] = _Permission(granted)
  100. ActivityCompat.requestPermissions(this, arrayOf(
  101. Manifest.permission.BODY_SENSORS), requestCode)
  102. }
  103. }
  104. fun Activity.microphonePermission(granted: (Boolean) -> Unit) {
  105. if (ContextCompat.checkSelfPermission(this, Manifest.permission_group.MICROPHONE) == PackageManager.PERMISSION_GRANTED)
  106. granted(true)
  107. else {
  108. val requestCode = ++permissionCode
  109. PermissionManager.permissionList[requestCode] = _Permission(granted)
  110. ActivityCompat.requestPermissions(this, arrayOf(
  111. Manifest.permission.RECORD_AUDIO), requestCode)
  112. }
  113. }
  114. fun Activity.smsPermission(granted: (Boolean) -> Unit) {
  115. if (ContextCompat.checkSelfPermission(this, Manifest.permission_group.SMS) == PackageManager.PERMISSION_GRANTED)
  116. granted(true)
  117. else {
  118. val requestCode = ++permissionCode
  119. PermissionManager.permissionList[requestCode] = _Permission(granted)
  120. ActivityCompat.requestPermissions(this, arrayOf(
  121. Manifest.permission.READ_SMS,
  122. Manifest.permission.RECEIVE_WAP_PUSH,
  123. Manifest.permission.RECEIVE_MMS,
  124. Manifest.permission.RECEIVE_SMS,
  125. Manifest.permission.SEND_SMS), requestCode)
  126. }
  127. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注