gpt4 book ai didi

java - 使用 Dexter 的 Kotlin 权限转到设置对话框

转载 作者:行者123 更新时间:2023-12-02 13:43:23 25 4
gpt4 key购买 nike

感谢您检查。所以我一直在做一个项目,该项目最终将扩展/添加一些功能,目前我正在努力向 Dexter (https://github.com/Karumi/Dexter) 请求权限。我已经能够让它工作,当用户单击“添加图片”按钮时,它也会向用户询问权限,但我遇到了一个我自己似乎无法解决的问题。所以问题是如果用户第一次打开应用程序并单击“添加图像”按钮,然后选择 User clicks on add picture button and chooses first option

并假设用户拒绝所有权限,如果用户再次单击按钮,应用程序会再次请求权限,但这次使用“拒绝并且不再询问”选项。我已经构建了一个弹出的小对话框,解释了为什么需要权限并可以引导用户进行设置。但我发现,如果用户实际上在第二次允许权限时,应用程序仍然会弹出该窗口,而我只是无法解决它。
User allows permissions
Msg still pops even though user gave permissions

这是我的代码:

// 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())
}

}

正如您所看到的,我正在谈论在函数“onPermissionRationaleShouldBeShown”中初始化的函数“showRationalDialogForPermissions()”。

如果有人知道如何解决这个问题或有任何我可以尝试的提示,我将不胜感激。

亲切的问候,

编辑:我也意识到如果用户点击“拒绝并且不再询问”并取消我的对话框,应用程序似乎不会在此之后出现对话框。几乎什么都没有发生。

最佳答案

所以我尝试了一些东西并且我得到了它以某种方式工作(仍然存在缺陷)

我更改了以下内容:

            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 次。但它以某种方式有效并且可以满足我的要求;如果有人有任何不同的想法或改进此“解决方案”的方法,将不胜感激

关于java - 使用 Dexter 的 Kotlin 权限转到设置对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61387862/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com