Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it available to set checkSelfPermission on minimum SDK < 23?

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

like image 673
Ardi Avatar asked Sep 16 '15 17:09

Ardi


People also ask

What is the minimum android SDK version?

Override the minimum value of SDK version. minSdkVersion is the minimum version of the Android operating system required to run your application. The Android app must have a minimum SDK version 19 or higher. If you want to support devices below API level 19, you must override minSDK version.

How do I increase my project SDK version?

Step 1: Open your Android Studio, and go to Menu. File >Project Structure. Step 2: In project Structure window, select app module in the list given on left side. Step 3: Select the Flavors tab and under this you will have an option for setting “Min Sdk Version” and for setting “Target Sdk Version”.


2 Answers

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()).

like image 57
CommonsWare Avatar answered Oct 14 '22 07:10

CommonsWare


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            } 
like image 27
Md. Sajedul Karim Avatar answered Oct 14 '22 08:10

Md. Sajedul Karim