Pro Android with Kotlin
Download 5.42 Mb. Pdf ko'rish
|
@de android telegram Pro Android with Kotlin Developing Modern Mobile
110
CHAPTER 7: Permissions the user more about the permission need. The frequency of how often shouldShowRequestPermissionRationale() returns true is up to the Android OS. The example here shows a dialogue; you can of course do whatever you want here to inform the user. The ActivityCompat.requestPermissions(...) method finally performs the permission inquiry. This happens asynchronously, so the call returns immediately. Once the call to ActivityCompat.requestPermissions(...) happens, the user gets asked by the Android OS, outside your app, whether they want to grant the permission. The result of that will show up in an asynchronous callback method as follows: override fun onRequestPermissionsResult( requestCode: Int, permissions: Array grantResults: IntArray) { when (requestCode) { cameraPermReturnId -> { // If request is canceled, the result // arrays are empty. Here we know it just // can be one entry if ((grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED)) { // permission was granted // act accordingly... } else { // permission denied // act accordingly... } return } // Add other 'when' lines to check for other // permissions this App might request. else -> { // Ignore all other requests. // Or whatever makes sense to you. } } } This method needs to be implemented inside an android.content.Activity class. In other contexts, this is not possible. Acquiring Special Permissions Using ActivityCompat.requestPermissions() in certain circumstances is not enough to acquire permissions SYSTEM_ALERT_WINDOW and WRITE_SETTINGS. For those two permissions, you need to follow a different approach. The permission WRITE_SETTINGS for API levels 23 and higher must be acquired using a special intent as follows: |
Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling
ma'muriyatiga murojaat qiling