[英]Android - Permission Dialog not showing on button click
In my application, i need to request write and read permission from the storage.在我的应用程序中,我需要从存储中请求写入和读取权限。 Since i want to show the user that the app needs these permissions, i have created and Activity containing a button, which on click, should call the Storage permission Dialog.
因为我想向用户显示应用程序需要这些权限,所以我创建了一个包含一个按钮的 Activity,单击该按钮应该调用存储权限对话框。
However, since the recent Android changes, this doesnt work anymore.但是,由于最近的 Android 发生了变化,这不再起作用。
Is there a new (and clean) way to ask permission?是否有一种新的(和干净的)方式来请求许可? Am i doing something wrong?
难道我做错了什么?
I have added the uses-permission
line inside the AndroidManifest.xml :我在AndroidManifest.xml中添加了
uses-permission
行:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
This is the code inside the Activity:这是 Activity 中的代码:
class ActivityPermission : AppCompatActivity() {
companion object {
var PERMISSION_REQUEST_CODE = 12
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityPermissionBinding.inflate(layoutInflater)
setContentView(R.layout.activity_permission)
binding.btnPermission.setOnClickListener {
ActivityCompat.requestPermissions(this, arrayOf(
android.Manifest.permission.READ_EXTERNAL_STORAGE,
android.Manifest.permission.WRITE_EXTERNAL_STORAGE),
PERMISSION_REQUEST_CODE)
}
}
override fun onRequestPermissionsResult(
requestCode: Int,
permissions: Array<out String>,
grantResults: IntArray
) {
if (requestCode == PERMISSION_REQUEST_CODE) {
if(grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(this, getString(R.string.permissiongranted), Toast.LENGTH_SHORT).show();
finish()
} else {
Toast.makeText(this, getString(R.string.permissiondenied), Toast.LENGTH_SHORT).show();
}
}
}
}
Thanks to everyone for helping me discover more about Android Permissions.感谢大家帮助我发现更多关于 Android 权限的信息。
I decided to change the way i ask for the permission.我决定改变我请求许可的方式。 Instead of an Activity, i decided to use a Fragment instead ( "show an educational UI to the user. In this UI, describe why the feature, which the user wants to enable, needs a particular permission." - source | Thanks to @Michael in the comments for pointing it out).
而不是一个活动,我决定使用一个片段来代替( “向用户显示一个教育用户界面。在这个用户界面中,描述为什么用户想要启用的功能需要一个特定的权限。” - 来源|感谢@迈克尔在评论中指出了这一点)。
Since now im using a Fragment, i have to use requestPermissions
(Thanks to this reply ).由于现在我使用片段,我必须使用
requestPermissions
(感谢这个回复)。 This now works flawlessly without any issues.这现在可以完美运行,没有任何问题。
Turns out you need a combination of checks when trying to request a permission.事实证明,在尝试请求许可时,您需要进行一系列检查。 You have to first check if the permission is actually enabled with
checkSelfPermission
, so you can easily choose where the user should go to start using the app.您必须首先检查是否使用
checkSelfPermission
实际启用了权限,因此您可以轻松选择用户应该从 go 开始使用该应用程序的位置。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.