Pro Android with Kotlin
Download 5.42 Mb. Pdf ko'rish
|
@de android telegram Pro Android with Kotlin Developing Modern Mobile
109
CHAPTER 7: Permissions Acquiring Permissions The way permissions are handled by the Android OS has changed. Prior to Android 6.0 (API level 23), the permission inquiry asked the user happened during the installation. Starting with API level 23, a paradigm change happened: permission inquiry happens during the runtime of an app. This made the permission system more flexible; users of your app might never use certain parts of it, and thus asking for permission to do so might annoy them. The downside of this approach is that more programming work is needed. The runtime permission inquiry must be included in your code. To do so, at any suitable place before the permission is needed, you add the following: val activity = this val perm = Manifest.permission.CAMERA val cameraPermReturnId = 7239 // any suitable constant val permissionCheck = ContextCompat.checkSelfPermission( activity, perm) if (permissionCheck != PackageManager.PERMISSION_GRANTED) { // Should we show an explanation? if (ActivityCompat. shouldShowRequestPermissionRationale( activity, perm)) { // Show an explanation to the user // *asynchronously* -- don't block // this thread waiting for the user's // response! After the user sees the // explanation, try again to request // the permission. val dialog = AlertDialog.Builder(activity) ... .create() dialog.show() } else { // No explanation needed, we can request // the permission. ActivityCompat.requestPermissions(activity, arrayOf(perm), cameraPermReturnId) // cameraPermReturnId is an app-defined // int constant. The callback method gets // the result of the request. } } This code does the following: First we check whether the permission has already been granted. If the permission was granted before, the user wouldn’t be asked again unless the app got reinstalled or the permission got revoked explicitly. The ActivityCompat.shouldShowRequestPermissionRationale() method checks whether a rationale should be shown to the user. The idea behind that is if the user denied the permission inquiry request a couple of times, they might have done that because the need for the permission was not well understood. In this case, the app gets a chance to tell |
Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling
ma'muriyatiga murojaat qiling