use android 6.0 api 23 permissions to low devices(

2019-03-07 06:19发布

问题:

it's my first question here and maybe i'll explain a little bad, but okay look: I have a project with its using api 22 library, but compiles with android 6.0(api 23), and i wanna continue with api 22 library, but when i run my project with android 6.0 device i have problems with the permissions...

My question: is there a way that you can work in, for example, Android 5.1.1 and compile with Android 6.0 with the necessary permission's methods? i mean, can i add some library, for use callback like onRequestPermissionsResult(...) ? i tried adding api 23 library to my project but isnt works his methods, any suggestion...??

回答1:

1. Checkout if the permission you need is a dangerous permission at this page.

2. List all dangerous permissions you need in a String array like below.

private static final String[] REQUIRED_PERMISSIONS = new String[]{your_permissions_here};

3. Add the following code to your onCreate().

if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
    LinkedList<String> missingPermissions = new LinkedList<>();
    for(String p : REQUIRED_PERMISSIONS){
        if(checkSelfPermission(p) != PackageManager.PERMISSION_GRANTED){
            missingPermissions.add(p);
        }
    }
    if(!missingPermissions.isEmpty()){
        String[] mpArray = new String[missingPermissions.size()];
        missingPermissions.toArray(mpArray);
        requestPermissions(mpArray, REQUEST_PERMISSIONS);
    }
}

REQUEST_PERMISSIONS is a constant integer to identify this requesting.