片段中未调用Android onRequestPermissionsResult

时间:2019-06-18 21:08:00

标签: android kotlin callback permissions android-permissions

我正在使用Google地图,所以我需要问一些permissions
我在onStart()的{​​{1}}期间请求用户
我之所以打电话给Fragment是因为我处在一个片段中,并且我也按照此answer中的建议以及您在下面的代码中看到的方式也呼叫了requestPermissions
我已经放置了一些super.onRequestPermissionsResultbreakpoint以确保100%不会调用Log

onRequestPermissionsResult
class FindLessonFragment : BaseFragment(), OnMapReadyCallback, OnClusterItemClickListener<ClusterLesson>, OnClusterClickListener<ClusterLesson> {

    override fun onStart() {
        super.onStart()
    checkPermissions()
    }

    private fun checkPermissions() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (activity!!.checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED
                    || activity!!.checkSelfPermission(permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
                    || activity!!.checkSelfPermission(permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
                val builder = AlertDialog.Builder(activity!!)
                builder.setTitle("This app needs location access")
                builder.setMessage("Please grant location access so this app can work normally.")
                builder.setPositiveButton(android.R.string.ok, null)
                builder.setOnDismissListener {
                    requestPermissions(
                            arrayOf(permission.ACCESS_COARSE_LOCATION, permission.READ_EXTERNAL_STORAGE, permission.ACCESS_FINE_LOCATION),
                            PERMISSION_REQUEST_LOCATION)
                }
                builder.show()
            }
        }
    }

    override fun onRequestPermissionsResult(requestCode: Int, @NonNull permissions: Array<String>, @NonNull grantResults: IntArray) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        Log.e("PERMISSIONRESULT", "PASS PERMISSION RESULT")
        when (requestCode) {
            BaseActivity.PERMISSION_REQUEST_LOCATION -> {
                if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    Log.d("BaseActivity", "coarse location permission granted")
                    onMapReady(mMap)
                } else {
                    val builder = AlertDialog.Builder(context!!)
                    builder.setTitle("Functionality limited")
                    builder.setMessage(
                            "Since location access has not been granted, this app will not be able to work correctly.")
                    builder.setPositiveButton(android.R.string.ok, null)
                    builder.setOnDismissListener { dialog -> dialog.dismiss() }
                    builder.show()
                }
            }
        }
    }

    companion object {

        fun create() = FindLessonFragment()

        internal const val PERMISSION_REQUEST_LOCATION = 1
    }

}

欢迎任何帮助

1 个答案:

答案 0 :(得分:0)

onRequestPermissionsResult必须抓住activty