Complete the code to request a runtime permission for accessing the camera.
ActivityCompat.requestPermissions(this, arrayOf([1]), 100)
The Manifest.permission.CAMERA is the correct permission to request camera access at runtime.
Complete the code to check if the app already has permission to read contacts.
if (ContextCompat.checkSelfPermission(this, [1]) == PackageManager.PERMISSION_GRANTED) { // Permission granted }
To check if the app has permission to read contacts, use Manifest.permission.READ_CONTACTS.
Fix the error in the code to handle the user's response to a permission request.
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>, [1]: IntArray) { super.onRequestPermissionsResult(requestCode, permissions, grantResults) if (requestCode == 100 && grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) { // Permission granted } }
The parameter name must be grantResults to match the method signature and be used correctly inside the method.
Fill both blanks to create a dictionary comprehension that maps permissions to their grant status.
val permissionStatus = permissions.associateWith { [1][permissions.indexOf(it)] == [2] }This code maps each permission to whether it was granted by comparing grantResults to PackageManager.PERMISSION_GRANTED.
Fill all three blanks to request multiple permissions and handle the result.
ActivityCompat.requestPermissions(this, arrayOf([1], [2]), [3]) override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>, grantResults: IntArray) { if (requestCode == [3]) { for (i in permissions.indices) { if (grantResults[i] == PackageManager.PERMISSION_GRANTED) { // Permission [1] or [2] granted } } } }
This code requests camera and location permissions with request code 101 and checks the results accordingly.