-5

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...??

Floern
  • 33,559
  • 24
  • 104
  • 119
Hamsi
  • 41
  • 1
  • 8
  • this will be help full for u http://www.sathyabaman.com/2016/06/19/permissions-in-android-6-0-api-level-23/ – Sathya Baman Jun 27 '16 at 02:15
  • i have readed something like that before, but i just want to know if i can continue working with api 22 or it's necessary change all my project to api 23? or is there a way to solution my problem... – Hamsi Jun 27 '16 at 17:59
  • There is no issues in working with API 22 or 23. If you don't request the permission as mentioned, it will work with all the devices that is running android 5 and below. but when you run it on Android 6.0 the app will crash. – Sathya Baman Jun 28 '16 at 06:56

1 Answers1

0

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.

Jeffrey Chen
  • 1,777
  • 1
  • 18
  • 29
  • I understand, but when i add the method `requestPermissions(...)` is giving me an error, its shows that "its undefined for the type myClass.java" and calling `Build.VERSION_CODES.M` (M isnt found)...i think i need to add somewhere a marshmallow's library...right? cause im working at api 22 library...or anoter solution for your answer? – Hamsi Jun 27 '16 at 18:01