33

New runtime permissions in Android-M asking for minimum 23 API level, but I still need minimum 16 API level in my project.

enter image description here

So, how to make this code more forward-compatible?

Regards

Ardi
  • 1,811
  • 4
  • 17
  • 29

5 Answers5

60

Use ContextCompat.checkSelfPermission(), ActivityCompat.requestPermissions(), and ActivityCompat.shouldShowPermissionRequestRationale(), from the support-v4 library (v23 or higher). These are backwards-compatible; if you are running on an older version of Android, they will "do the right thing" (e.g., return PackageManager.PERMISSION_GRANTED for ContextCompat.checkSelfPermission()).

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • 5
    I use support v4 too and ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED returns false on HTC Incredible with android 2.3.3. Any idea why ? – Anonymous Mar 07 '16 at 15:35
  • @Anonymous: Your `targetSdkVersion` needs to be 23 or higher. – CommonsWare Mar 07 '16 at 15:45
  • 4
    it is.. minSdkVersion="7" android:targetSdkVersion="23" – Anonymous Mar 07 '16 at 15:58
  • @Anonymous you might need to change your minSdkVersion to 23 also. – Stephen G Tuggy Aug 22 '18 at 18:11
  • In my experience, on older versions (I'm testing with API v.7) the ``ActivityCompat.checkSelfPermission`` does not return permission granted. – Eir Oct 05 '19 at 10:17
5

Just Check your android version before get check permission:

 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                    // TODO: Consider calling
                    //    public void requestPermissions(@NonNull String[] permissions, int requestCode)
                    // here to request the missing permissions, and then overriding
                    //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                    //                                          int[] grantResults)
                    // to handle the case where the user grants the permission. See the documentation
                    // for Activity#requestPermissions for more details.
                    return;
                }
            }else{
              //Do Your Stuff
           }
Md. Sajedul Karim
  • 6,749
  • 3
  • 61
  • 87
4

In case you dont want to use AppCompatActivity the way is described here https://codemammoth.blogspot.gr/2016/06/how-to-invoke-checkselfpermission.html

You have to invoke the methods :)

Armel Larcier
  • 15,747
  • 7
  • 68
  • 89
Nikos
  • 67
  • 5
1

You can check the build version if(Build.Version.SDK_INT >= Build.VERSION_CODES.MARSHMALLOW). And then handle marshmallow permissions in there, and handle the other versions otherwise.

Chris Thoma
  • 499
  • 2
  • 8
1

checkSelfPermission is available above sdk 23.

we can check the permission is available or not using package manager

public static Boolean checkpermissions(Activity activity) {


        PackageManager mPackageManager = activity.getPackageManager();
        int hasPermStorage = mPackageManager.checkPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE, activity.getPackageName());


        if (hasPermStorage != PackageManager.PERMISSION_GRANTED) {
            // do stuff
            //Toast.makeText(getApplicationContext(), "No permission", Toast.LENGTH_LONG).show();

            return false;
        } else if (hasPermStorage == PackageManager.PERMISSION_GRANTED) {

            // do stuff
            //Toast.makeText(getApplicationContext(), "Has permission", Toast.LENGTH_LONG).show();

            return true;
        }else
            return false;
    }
Dino Sunny
  • 921
  • 1
  • 10
  • 18