Search code examples
androidkotlinandroid-permissions

Setting up a retry button to request again for permissions in android kotlin


I wrote a permission handling code to handle multiple permission properly.

 //exception handling and cancellation management
private val errorHandler = CoroutineExceptionHandler { _, throwable ->
    Log.e("SplashActivity", "Exception Error: $throwable")
}

private var multiplePermissionsLauncher = registerForActivityResult(
    ActivityResultContracts.RequestMultiplePermissions()
) { results ->
    var areAllPermsGranted = true
    for (isGranted in results.values) {
        areAllPermsGranted = areAllPermsGranted && isGranted
    }
    if (areAllPermsGranted) {
        lifecycleScope.async(Dispatchers.Default + errorHandler) {
            delay(1000)
            startActivity(Intent(this@Splash, ViewDevicesActivity::class.java))
            finish()
        }
    } else {
        findViewById<LinearLayout>(R.id.llPermCheck).visibility = View.VISIBLE
        findViewById<TextView>(R.id.btnPermCheck).setOnClickListener {
//This is the part that does not work as expected.
            permCheck()
        }
    }
}

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_splash)
    uiStuff()
}
 @SuppressLint("InlinedApi")
private fun uiStuff() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        findViewById<TextView>(R.id.tvPermMessage).justificationMode =
            LineBreaker.JUSTIFICATION_MODE_INTER_WORD
    }
    val motionLayout = findViewById<MotionLayout>(R.id.clMovingParts)
    motionLayout.addTransitionListener(object : MotionLayout.TransitionListener {
        override fun onTransitionStarted(motionLayout: MotionLayout, startId: Int, endId: Int) {
        }

        override fun onTransitionChange(
            motionLayout: MotionLayout,
            startId: Int,
            endId: Int,
            progress: Float
        ) {
        }

        override fun onTransitionCompleted(motionLayout: MotionLayout, currentId: Int) {
//checking for permission after app runs
            permCheck()
        }

        override fun onTransitionTrigger(
            motionLayout: MotionLayout,
            triggerId: Int,
            positive: Boolean,
            progress: Float
        ) {
        }
    })
}
    private fun permCheck() {
    val permissions = arrayOf(
        Manifest.permission.SEND_SMS,
        Manifest.permission.RECEIVE_SMS,
        Manifest.permission.READ_PHONE_STATE
    )
    multiplePermissionsLauncher.launch(permissions)
}

The problem is I created a layout and button which by default are hidden and if one permission or more are not grated the layout will be visible to explain that why the permissions are required and tells the user to click a button(i used a textview instead of button) so app asks again for permissions. The problem is by pressing the button the android permission dialog wont appear.


Solution

  • The problem is by pressing the button the android permission dialog wont appear.

    Once the user had denied the permission multiple times (currently twice), the permission request is a no-op.

    https://developer.android.com/training/permissions/requesting#handle-denial

    You can direct them to your app settings to enable the permissions manually instead.

    https://stackoverflow.com/a/32983128/1197251