简体   繁体   中英

Requesting app runtime permissions (Android)

I want to write a simple app which accesses the device's location. Only I will ever use the app. This is my first attempt in about 10 years to write an Android app, so it is the first time I've had to deal with runtime permissions.

My first question is, given that the app is solely for my use, is it possible to by-pass the need for runtime permission code?

Failing that, is there any simple example code that fills in the numerous gaps in the Android documentation ?

To take one example: the doc includes the following:

when {
ContextCompat.checkSelfPermission(
        CONTEXT,
        Manifest.permission.REQUESTED_PERMISSION
        ) == PackageManager.PERMISSION_GRANTED -> {
    // You can use the API that requires the permission.
    performAction(...)

What does this mean? What "API that requires the permission"? What replaces the "..."?

There are several other similar gaps on the page.

You have mentioned that app is solely for your use then you don't have to write the code for the runtime permission you can skip it...

How to do this... ?

Step 1: Just put all the permission you need inside the app manifest file and install the app

Step 2: Go to the app settings or app info in the phone and check for app permissions all the permission that you mentioned will be displayed there just toogle them manually

That's it now write code to access the things which you supposed to write after getting permission

  1. If you want to avoid run time permission request you can build your app with Android SDK version below 23 (Android 6 Marshmellow)

  1. For Android API Level 23 or above (after adding permission in your manifest file):

First, add your permission to AndroidManifest.xml file:

<uses-permission android:name="android.permission.THE_PERMISSION" />

Then in your Activity:

Check Permission:

fun checkPermission(permission: String): Boolean {
        return ContextCompat.checkSelfPermission(this, permission) == PackageManager.PERMISSION_GRANTED
    }

Get Permission:

fun getPermission(permission: String) {
    ActivityCompat.requestPermissions(this, arrayOf(permission), REQ_CODE_PERMISSION)
}

Get Permission Result:

override fun onRequestPermissionsResult(
    requestCode: Int,
    permissions: Array<out String>,
    grantResults: IntArray
) {
    if (requestCode == REQ_CODE_PERMISSION && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
        ...      
    }
    super.onRequestPermissionsResult(requestCode, permissions, grantResults)
    }

If you are using Fragments:

A fragment contains a method for requesting permission and getting the result back to its onRequestPermissionsResult :

fun getPermission(permission: String) {
    requestPermissions(arrayOf(permission), REQ_CODE_PERMISSION)
}

REQ_CODE_PERMISSION : is some random number like 123 you use to identify your request with.

is it possible to by-pass the need for runtime permission code?

You still need the <uses-permission> element. But you could manually grant the permission to your app via the Settings app.

The point of runtime permission code is to request the permission from the user and defend against cases where that permission has not been granted. In your case, if your app crashes because you revoked the permission, you can yell the developer. You, as the developer, in turn, can yell at you, as the user, for failing to manually grant the permission. Since you will be yelling at yourself, it is recommended to do this in a private location, or perhaps have a Bluetooth headset in your ear as cover. :-)

What does this mean?

We request runtime permissions because we want to use some Android API that is defended by such a permission. We usually do not request runtime permissions because we woke up one morning and decided that requesting runtime permissions sounds like a really fun thing to do.

What "API that requires the permission"? What replaces the "..."?

In your case, it would appear to be either methods on LocationManager or stuff using the fused location API from Google Play Services.

is there any simple example code that fills in the numerous gaps in the Android documentation?

The problem is that "example code" is 5% permission-related and 95% whatever it is that you are using that requires the permission. Any sample code that only shows permissions is going to have the same hand-wavy stuff that you don't like from the documentation. In your case, any up-to-date examples of using location APIs should also show the runtime permission elements.

FWIW, this directory contains several sample projects from this book that show getting the location. They are a bit old but do show requesting runtime permissions (mostly contained in an AbstractPermissionActivity ). This sample is newer and in Kotlin, but it is for file-access permissions, not for locations (and is covered in this other book ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM