Android check permission for LocationManager

后端 未结 6 820
我寻月下人不归
我寻月下人不归 2020-11-28 01:35

I\'m trying to get the GPS coordinates to display when I click a button in my activity layout. The following is the method that gets called when I click the button:

6条回答
  •  猫巷女王i
    2020-11-28 01:59

    If you simply want to check for permissions (rather than request for permissions), I wrote a simple extension like so:

    fun BaseActivity.checkPermission(permissionName: String): Boolean {
            return if (Build.VERSION.SDK_INT >= 23) {
                val granted =
                    ContextCompat.checkSelfPermission(this, permissionName)
                granted == PackageManager.PERMISSION_GRANTED
            } else {
                val granted =
                    PermissionChecker.checkSelfPermission(this, permissionName)
                granted == PermissionChecker.PERMISSION_GRANTED
            }
        }
    

    Now, if I want to check for a permission I can simply pass in a permission like so:

    checkPermission(Manifest.permission.READ_CONTACTS)
    

提交回复
热议问题