Explain Codes LogoExplain Codes Logo

Android permission doesn't work even if I have declared it

java
runtime-permission
android-permission
permissions-management
Nikita BarsukovbyNikita Barsukov·Jan 29, 2025
TLDR

Exceptionally crucial on Android 6.0+ (API 23+): Perform ContextCompat.checkSelfPermission to examine and then ActivityCompat.requestPermissions to call for runtime permissions:

if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) { // Calling to the grand magician of Android kingdom for the sacred CAMERA permission ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, MY_PERMISSIONS_REQUEST_CAMERA); }

You can plug and play with CAMERA substitution as per intended permission and undertake grant result handling in onRequestPermissionsResult.

Dangerous? Not on my watch!

Alright, the Android Playground is divided into two areas - normal and dangerous. And guess what, no matter what, if you're hanging out around API level 23 (Android 6.0 or above), dangerous permissions demand runtime approval.

The Three-Step Tango of Runtime Permission

Let's make this party going with the steps of handling permissions:

  1. First, check if your permission slipped from the gate or is not granted by using: ContextCompat.checkSelfPermission.
  2. If the gate isn't open, politely request permission with: ActivityCompat.requestPermissions.
  3. Now, patiently stand by and manage the user's response in your callback named onRequestPermissionsResult.

Remember folks, users may deny permissions like they deny a cold beverage on a freezing day — your challenge is to deal with it as cool as a cucumber.

Unraveling Common Blunders and their Cures

  • Hey, did you misspell permissions? Hustle back and tweak your <uses-permission/> tag for those devilish typos.
  • Avoid playing with targetSdkVersion, don't try to be sneaky by pulling it under 23 as a long-term solution — no one likes a cheater!
  • Got SecurityExceptions trying to wrestle you down? Pin it to the floor with some nifty debug logs to expose its weak spot i.e., the point where the permission check skipped its turn.

Runtime Permissions — The Deep Dive

Clarity Bells are Ringing with Constants

Wishlist of our constant-ly reliable friends: PackageManager.PERMISSION_GRANTED and PackageManager.PERMISSION_DENIED. They bring clarity and durability to your code.

Dancing with Android Version

Build.VERSION.SDK_INT is the Michael Jackson of Android, helping you moonwalk around different Android versions, deciding when to perform the runtime permission check moves.

The Fancy Folks — Handling Multiple Permissions

Got a list of more than one permission? Swing it with a String[] to request them all in a jiffy.

ActivityCompat.requestPermissions(this, new String[] { Manifest.permission.SEND_SMS, Manifest.permission.ACCESS_FINE_LOCATION }, PERMISSIONS_MULTIPLE_REQUEST);

Tip of the Day - Handling SEND_SMS

Working with SEND_SMS permission? Wave your wand Manifest.permission.SEND_SMS during permission requests, secure runtime approval and you will be sending text messages like pigeons in no time.