Home > other >  Permission request problem in kotlin (Storage Permission)
Permission request problem in kotlin (Storage Permission)

Time:12-16

I am trying to request permission on android using kotlin , when i request permission then permission was show but i have created a function that not works. Function like user deind permission 1 time then permission show second time but after denied second time then dialog box show , but my function not.

Please check my codes

 package com.`fun`.rapidplayer

import android.app.Dialog
import android.content.Intent
import android.content.pm.PackageManager
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.provider.Settings
import android.view.Window
import android.widget.Button
import android.widget.LinearLayout
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat


class PermissionActivity : AppCompatActivity() {

    private lateinit var requestPermissionLauncher: ActivityResultLauncher<String>
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_permission)

        requestPermissionLauncher =
            registerForActivityResult(ActivityResultContracts.RequestPermission()) { granted ->
                if (granted) {
                    //Storage Permission Granted
                    intent.setClass(applicationContext, MainActivity::class.java)
                    startActivity(intent)
                    finish()
                } else {
                    //Storage Permission NOT Granted
                    
                    showDialog()
                }
            }


        val allow = findViewById<Button>(R.id.Allow_Btn)

        allow.setOnClickListener {
            requestStoragePermission()
        }
    }

    private fun requestStoragePermission(){
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            when {
                ContextCompat.checkSelfPermission(
                    this,
                    android.Manifest.permission.WRITE_EXTERNAL_STORAGE
                ) == PackageManager.PERMISSION_GRANTED -> {
                    intent.setClass(applicationContext, MainActivity::class.java)
                    startActivity(intent)
                    finish()
                }
                shouldShowRequestPermissionRationale(android.Manifest.permission.WRITE_EXTERNAL_STORAGE) -> {
                    // This case means user previously denied the permission
                    showDialog()
                }
                else -> {
                    // request the permission
                    requestPermissionLauncher.launch(android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
                }
            }
        }
    }

    override fun onResume() {
        super.onResume()
        if (ContextCompat.checkSelfPermission(this,android.Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
            intent.setClass(applicationContext, MainActivity::class.java)
            startActivity(intent)
            finish()
        }
    }

    private fun showDialog() {
        val dialog = Dialog(this)
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE)
        dialog.setCancelable(false)
        dialog.window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
        dialog.setContentView(R.layout.permission_denied)
        dialog.findViewById(R.id.root) as LinearLayout
        val opensettigs = dialog.findViewById(R.id.open_settings) as Button
        opensettigs.setOnClickListener {
            val intent = Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS)
            val uri: Uri = Uri.fromParts("package", packageName, null)
            intent.data = uri
            startActivity(intent)
        }
        dialog.show()
    }
}

show please check out my code and give the best solution please

CodePudding user response:

I believe this section here in thw when clause is not making a comparison to anything:

shouldShowRequestPermissionRationale(android.Manifest.permission.WRITE_EXTERNAL_STORAGE) -> {
                // This case means user previously denied the permission
                showDialog()
            }

In the previous clause you're comparing against PackageManager.PERMISSION_GRANTED

Hope it helps!

CodePudding user response:

Ted Permission library handles it for you, implementation is simple https://github.com/ParkSangGwon/TedPermission

  • Related