0
0
Android Kotlinmobile~5 mins

Why runtime permissions protect user privacy in Android Kotlin

Choose your learning style9 modes available
Introduction

Runtime permissions ask users for access only when needed. This helps keep their private data safe.

When your app needs to access the camera to take photos.
When your app wants to read contacts to show friends.
When your app needs location to provide nearby services.
When your app wants to access storage to save or read files.
Syntax
Android Kotlin
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
    ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.CAMERA), REQUEST_CODE)
}
Use checkSelfPermission to see if permission is already granted.
Use requestPermissions to ask the user for permission at runtime.
Examples
Check and request location permission when your app needs GPS data.
Android Kotlin
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
    ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.ACCESS_FINE_LOCATION), LOCATION_REQUEST_CODE)
}
Ask for contacts permission only when you want to access the user's contacts.
Android Kotlin
if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) {
    ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.READ_CONTACTS), CONTACTS_REQUEST_CODE)
}
Sample App

This app checks if it has camera permission when it starts. If not, it asks the user. It shows a message depending on the user's choice.

Android Kotlin
import android.Manifest
import android.content.pm.PackageManager
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import android.widget.Toast

class MainActivity : AppCompatActivity() {
    private val CAMERA_REQUEST_CODE = 100

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(android.R.layout.simple_list_item_1)

        if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.CAMERA), CAMERA_REQUEST_CODE)
        } else {
            Toast.makeText(this, "Camera permission already granted", Toast.LENGTH_SHORT).show()
        }
    }

    override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>, grantResults: IntArray) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        if (requestCode == CAMERA_REQUEST_CODE) {
            if ((grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED)) {
                Toast.makeText(this, "Camera permission granted", Toast.LENGTH_SHORT).show()
            } else {
                Toast.makeText(this, "Camera permission denied", Toast.LENGTH_SHORT).show()
            }
        }
    }
}
OutputSuccess
Important Notes

Always explain to users why your app needs a permission before asking.

Users can deny permissions, so your app should handle that gracefully.

Request only the permissions your app really needs to build trust.

Summary

Runtime permissions protect user privacy by asking only when needed.

Check permission status before requesting to avoid unnecessary prompts.

Handle user responses to permissions carefully to keep app working well.