【发布时间】:2020-08-06 19:35:04
【问题描述】:
感谢您的检查。所以我一直在做一个项目,该项目最终将扩展/添加一些功能,目前我正在努力向 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()”。
如果有人知道如何解决这个问题或有任何我可以尝试的提示,我将不胜感激。
亲切的问候,
编辑:我也意识到如果用户点击“拒绝并且不再询问”并取消我的对话框,应用程序似乎不会在此之后出现对话框。几乎什么都没有发生。
【问题讨论】: