New runtime permissions in Android-M asking for minimum 23 API level, but I still need minimum 16 API level in my project.
So, how to make this code more forward-compatible?
Regards
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.
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”.
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()
).
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 }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With