How i can request permission at runtime in Android?

心已入冬 提交于 2019-12-11 13:56:29

问题


I'm developing an Android application, I have to allow users to use camera to scan a QRCode.

In each Android version (except > 6.0) i haven't problem, but in marshmallow I must enable manually the permission from "Settings -> App ->Permission" (it's strange because I have declared the camera permission in the manifest).

I read the documentation dev-android website but i don't understand some things:

if (ContextCompat.checkSelfPermission(thisActivity,
                Manifest.permission.READ_CONTACTS)
        != PackageManager.PERMISSION_GRANTED) {

    if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
            Manifest.permission.READ_CONTACTS)) {

        } else {    
        ActivityCompat.requestPermissions(thisActivity,
                new String[]{Manifest.permission.READ_CONTACTS},
                MY_PERMISSIONS_REQUEST_READ_CONTACTS);

    }
}

Second part of the code:

@Override
public void onRequestPermissionsResult(int requestCode,
        String permissions[], int[] grantResults) {
    switch (requestCode) {
        case MY_PERMISSIONS_REQUEST_READ_CONTACTS: {
            // If request is cancelled, the result arrays are empty.
            if (grantResults.length > 0
                && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                // permission was granted, yay! Do the
                // contacts-related task you need to do.

            } else {

                // permission denied, boo! Disable the
                // functionality that depends on this permission.
            }
            return;
        }

        // other 'case' lines to check for other
        // permissions this app might request
    }
}

How i can adapt this code to my problem ?

What is "MY_PERMISSIONS_REQUEST_READ_CONTACTS" ?


回答1:


MY_PERMISSIONS_REQUEST_READ_CONTACTS is a static int variable that you need to set in your Activity. It is the request code that is used in onRequestPermissionsResult. It is required so that you know which permission was acted upon (whether it be accepted or rejected) in onRequestPermissionsResult.

At the top of your Activity just put private static final int MY_PERMISSIONS_REQUEST_READ_CONTACTS = 1; (the number can be whatever you want)




回答2:


RxPermission is widely used library now. It makes complex if write all code for permission.

RxPermissions rxPermissions = new RxPermissions(this); // where this is an Activity instance // Must be done during an initialization phase like onCreate
rxPermissions
    .request(Manifest.permission.CAMERA)
    .subscribe(granted -> {
        if (granted) { // Always true pre-M
           // I can control the camera now
        } else {
           // Oups permission denied
        }
    });

your build.gradle

allprojects {
    repositories {
        ...
        maven { url 'https://jitpack.io' }
    }
}

dependencies {
    implementation 'com.github.tbruyelle:rxpermissions:0.10.1'
    implementation 'com.jakewharton.rxbinding2:rxbinding:2.1.1'
}

Isn't this easy?




回答3:


Try this simple code for asking user permissions at Runtime.

Step 1: Declare this variable

private int STORAGE_PERMISSION_CODE=23

Step 2: Paste this code in the onCreate() function,this will generate a dialog box once the activity is called asking you to grant access for "Read External Storage" in this case.

ActivityCompat.requestPermissions(this,new String[{Manifest.permission.READ_EXTERNAL_STORAGE},STORAGE_PERMISSION_CODE);

You can change the "READ_EXTERNAL_STORAGE" parameter as per your requirement.

For complete demonstration on "Android Marshmallow Permissions" check out this link : https://www.simplifiedcoding.net/android-marshmallow-permissions-example/



来源:https://stackoverflow.com/questions/37441133/how-i-can-request-permission-at-runtime-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!