使用Dexter的Kotlin权限进入设置对话框。

谢谢你的检查。所以我一直在做一个项目,最终会扩展一些功能,目前我正在向Dexter申请权限(https:/github.comKarumiDexter。). 我已经能够让它工作了,当用户点击 “添加图片 “按钮时,它也会询问用户的权限,但我遇到了一个问题,我似乎无法自己解决。所以问题是,如果用户第一次打开应用程序,点击 “添加图片 “按钮,然后选择 “添加图片”。用户点击添加图片按钮,选择第一个选项。

让我们假设用户拒绝所有的权限,如果用户再次点击按钮,应用程序会再次询问权限,但这次是 “拒绝和不要再问 “的选项。而且我还建立了一个小Dialog,弹出解释为什么需要权限,并且可以引导用户进行设置。但是我发现,如果用户在第二次去的时候真的允许了权限,应用还是会弹出那个窗口,我就没能解决这个问题。用户允许权限即使用户给了权限,仍然会弹出提示音。

这是我的代码。

// Creating the variables of Calender Instance and DatePickerDialog listener to use it for date selection
// A variable to get an instance calendar using the default time zone and locale.
private var cal = Calendar.getInstance()

/* A variable for DatePickerDialog OnDateSetListener.
* The listener used to indicate the user has finished selecting a date. It will be initialized later. */
private lateinit var dateSetListener: DatePickerDialog.OnDateSetListener

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_add_happy_place)

    // Adds the back button on the ActionBar
    setSupportActionBar(toolbar_add_place)
    supportActionBar?.setDisplayHomeAsUpEnabled(true)
    toolbar_add_place.setNavigationOnClickListener {
        onBackPressed()
    }

    // Initialize the DatePicker and sets the selected date
    // https://www.tutorialkart.com/kotlin-android/android-datepicker-kotlin-example/
    dateSetListener = DatePickerDialog.OnDateSetListener { _, year, month, dayOfMonth ->
        cal.set(Calendar.YEAR, year)
        cal.set(Calendar.MONTH, month)
        cal.set(Calendar.DAY_OF_MONTH, dayOfMonth)
        updateDateInView()
    }
    // Uses functionality in the onClick function below
    et_date.setOnClickListener(this)
    tv_add_image.setOnClickListener(this)
}

// This is a override method after extending the onclick listener interface (gets created automatically)
override fun onClick(v: View?) {
    when (v!!.id) {
        R.id.et_date -> {
            DatePickerDialog(
                this@AddHappyPlaceActivity, dateSetListener,
                cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH)
            ).show()
        }
        R.id.tv_add_image -> {
            val pictureDialog = AlertDialog.Builder(this)
            pictureDialog.setTitle("Select Action")
            val pictureDialogItems =
                arrayOf("Select photo from gallery", "Capture photo from camera")
            pictureDialog.setItems(pictureDialogItems) { _, which ->
                when (which) {
                    0 -> choosePhotoFromGallery()
                    1 -> Toast.makeText(
                        this,
                        "Camera selection coming soon",
                        Toast.LENGTH_SHORT
                    ).show()
                }
            }
            pictureDialog.show()
        }
    }
}


// Method used for image selection from GALLERY/PHOTOS
private fun choosePhotoFromGallery() {
    // Asking for permissions using DEXTER Library
    Dexter.withContext(this).withPermissions(
        Manifest.permission.READ_EXTERNAL_STORAGE,
        Manifest.permission.WRITE_EXTERNAL_STORAGE,
        Manifest.permission.CAMERA
    ).withListener(object : MultiplePermissionsListener {
        override fun onPermissionsChecked(report: MultiplePermissionsReport?) {
            // Here after all the permission are granted, launch the gallery to select and image.
            if (report!!.areAllPermissionsGranted()) {
                Toast.makeText(
                    this@AddHappyPlaceActivity,
                    "Storage READ/WRITE permission are granted. Now you can select an image from GALLERY or lets says phone storage.",
                    Toast.LENGTH_SHORT
                ).show()
            }
        }

        override fun onPermissionRationaleShouldBeShown(
            permissions: MutableList<PermissionRequest>?,
            token: PermissionToken?
        ) {
            token?.continuePermissionRequest()
            showRationalDialogForPermissions()

        }
    }).onSameThread().check()
}

// Message to be shown if user denies access and possibly send him to the settings
private fun showRationalDialogForPermissions() {
    AlertDialog.Builder(this).setMessage(
        "It looks like you have turned off " +
                "permissions required for this feature"
    ).setPositiveButton("GO TO SETTINGS")
    { _, _ ->
        try {
            val intent = Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS)
            val uri = Uri.fromParts("package", packageName, null)
            intent.data = uri
            startActivity(intent)
        } catch (e: ActivityNotFoundException) {
            e.printStackTrace()
        }
    }.setNegativeButton("Cancel") { dialog, _ ->
        dialog.dismiss()
    }.show()
}

// A function to update the selected date in the UI with selected format.
private fun updateDateInView() {
    val myFormat = "dd.MM.yyyy"
    val sdf = SimpleDateFormat(myFormat, Locale.getDefault())
    et_date.setText(sdf.format(cal.time).toString())
}

}

如你所见,我说的是函数 “showRationalDialogForPermissions()”,它在函数 “onPermissionRationaleShouldBeShown “中被初始化。

如果有人知道如何解决这个问题,或者有什么小窍门可以让我去试试,我将非常感激。

问好。

EDIT: 我还意识到,如果用户点击 “拒绝,不要再问”,并取消我的Dialog,应用程序似乎不会使Dialog出现后。几乎什么都没有发生。

解决方案:

所以我试了一些东西,我得到了一些工作(仍然有缺陷)。

我修改了以下内容。

            R.id.tv_add_image -> {
            val pictureDialog = AlertDialog.Builder(this)
            pictureDialog.setTitle("Select Action")
            val pictureDialogItems = arrayOf("Select photo from gallery", "Capture photo from camera")
            pictureDialog.setItems(pictureDialogItems) {
                    _, which ->
                when(which) {
                    0 -> choosePhotoFromGallery()
                    1 -> Toast.makeText(this, "Camera selection coming soon", Toast.LENGTH_SHORT).show()
                }
            }
            pictureDialog.show()
            addButtonClicked += 1
            if (addButtonClicked > 2) {
                if (ContextCompat.checkSelfPermission(this@AddHappyPlaceActivity,
                        Manifest.permission.CAMERA)
                        != PackageManager.PERMISSION_GRANTED) {
                        showRationalDialogForPermissions()
                    }
                if (ContextCompat.checkSelfPermission(this@AddHappyPlaceActivity,
                        Manifest.permission.READ_EXTERNAL_STORAGE)
                        != PackageManager.PERMISSION_GRANTED) {
                        showRationalDialogForPermissions()
                    }
                if (ContextCompat.checkSelfPermission(this@AddHappyPlaceActivity,
                        Manifest.permission.WRITE_EXTERNAL_STORAGE)
                        != PackageManager.PERMISSION_GRANTED) {
                        showRationalDialogForPermissions()
                    }
            }
        }

这不是一个最佳的解决方案,但我只是把我创建的值递增1,一旦达到3(所以假设用户以某种方式连续拒绝了2次),就会弹出那个窗口。缺点是,你必须在给予权限后点击2次取消按钮。如果有人有什么不同的想法,或者有什么方法可以改进这个 “解决方案”,我将非常感激。

给TA打赏
共{{data.count}}人
人已打赏
未分类

在浏览器中自动执行一些操作(通过Selenium?

2022-9-8 2:01:36

未分类

React, componentWillReceive 生命周期钩子没有被调用(在传递道具时)。

2022-9-8 2:01:38

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索