Android version check - android

I'm not new to Android and I'm well used to the version handling and how to condition it, but when I see this it troubles me...
// Check if we're running on Android 5.0 or higher
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
// Call some material design APIs here
} else {
// Implement this feature without material design
}
On any device pre lollipop this line would crash the app because the Build.VERSION_CODES.LOLLIPOP field does not exists... so why is this in the recommended solution in the documentation?
I'm really wondering what am I missing?

Well in that case use this
// Check if we're running on Android 5.0 or higher
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
// Call some material design APIs here
} else {
// Implement this feature without material design
}
Build.VERSION_CODES.LOLLIPOP = 21

Well, you must compile your project with the latest SDK version. Your constants are replaced with corresponding integer values during compilation. No matter what version of Android you run the application on - integers are the same

Try this one
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP_MR1) {
// Marshmallow+
}else{
//below Marshmallow
}
Note: Build.VERSION_CODES.LOLLIPOP_MR1==22
Build.VERSION_CODES.M==23

Bit a late to answer, but, today I encountered with the same issue on Android Studio 3.4.1
So the workaround is:
Upgrade to the latest Android SDK.
And,
After Marshmallow/Android 6 Build.VERSION_CODES.xxx full names are replaced with initials and some other variations.
So, now for Marshmallow, it will be:
Build.VERSION_CODES.M
And for Nougat:
Build.VERSION_CODES.N
And so on.
Read more about the build version codes here: Android Developer Reference

Related

How to get device Locale on android pre Nougat (< api 24)?

I need to get the device Locale on android studio, but apparently the code below only works on api level 24+; How can i achieve the same result as this line of code in lower level apis?
getResources().getConfiguration().getLocales().get(0);
well according to google docs the command public Locale locale has deprecated in API 24 and you are allowed to use getLocales() and setLocales(android.os.LocaleList) in API levels above 24. so you should yoconfiguration.locale instead in lower APIs.
#Override
public void applyOverrideConfiguration(Configuration overrideConfiguration) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && Build.VERSION.SDK_INT <= Build.VERSION_CODES.N_MR1) {
// update overrideConfiguration with your locale
setLocale(overrideConfiguration) // you need to implement this
}
super.applyOverrideConfiguration(overrideConfiguration);
}
also check this out, it says it all:
Set Locale programmatically
For lower lavel api, you can use
Locale current = getResources().getConfiguration().locale;
Reference: https://stackoverflow.com/a/14389640/12676247

Checking Android version when requesting runtime permission

I have recently updated the code for an Android app to request permissions on Android 6.0+.
However, I am now facing a dilemma on how I want to check for permissions.
I saw people online checking the OS version before checking for permissions, because versions before 23, permissions are unnecessary to check due to them being granted on installation.
Right now my checks look like this,
if(checkPermissions()){
doThings();
} else {
requestPermissions();
}
but should I take the effort to add this?
if (Build.VERSION.SDK_INT >= 23) {
if(checkPermissions()){
doThings();
} else {
requestPermissions();
}
} else {
doThings();
}
I don't see the point of adding the latter to the code as from my understanding, older versions of android could run into the first sample code just fine.
All this brings me to ask, is there a benefit to checking Android version in this case?
but should I take the effort to add this?
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if(checkPermissions()){
doThings();
} else {
requestPermissions();
}
} else {
doThings();
}
You should if checkPermissions(), and requestPermissions() invokes classes or methods strictly API 23 or later. Or else, you may do as you wish.
All this brings me to ask, is there a benefit to checking Android version in this case?
As far as I know, the verification by itself isn't necessary, given what I said above. So this check is just an expendable if clause cluttering your code.
ContextCompact.checkSelfPermission
return always PERMISSION_GRANTED in android versions < 23.
So you can avoid check build version.
More info here

Android WebChromeClient supply different method on all Android version, how to use them?

WebChromeClient supply "onShowFileChooser" on Android 5.0, also supply "openFileChooser" method below Android 4.4 method,How to compatible with them? Has anyone managed to do this?
Maybe you can use "Build.VERSION.SDK_INIT" to check the current sdk version on the phone used your app. And decide which to use.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
// do something with onShowFileChooser()
} else {
//do something with openFileChooser()
}

Eclipse android ID cannot be resolved or is not a field

I have this code:
public void run() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
// This action will have the side-effect of blurring the currently focused element
inAppWebView.loadUrl("javascript:" + finalScriptToInject);
}
But the id KITKAT gets the error: KITKAT cannot be resolved or is not a field
Build.VERSION_CODES.KITKAT
As in docs : October 2013: Android 4.4, KitKat, another tasty treat.
means KITKAT constant added in API level 19.
So change current project target version to API level 19(Android 4.4)

How to only execute code on certain API level

For instance, this code:
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.GINGERBREAD) {
myCalendarView.setOnDateChangeListener(
new OnDateChangeListener() {
#Override
public void onSelectedDayChange(CalendarView view, int year, int month, int dayOfMonth) {
Toast.makeText
(
getApplicationContext(), ""+dayOfMonth, 0
).show();
}
}
);
}
Gives error:
Description Resource Path Location Type Call requires API level 11
(current min is 8):
android.widget.CalendarView#setOnDateChangeListener example.java /example/src/com/example/example line
20 Android Lint Problem
I understand why I get this error compile-time. But is there any way to mark a source Java class to only be used on certain API level-11? Or surround code blocks with a define/similar so the code is late-bound/jitted only on devices above API level-11? What is the best solution to achieve what I want? (Which is to provide an activity with CalendarView on devices capabile of it.)
I'm not sure if this is going to solve your issue,
but what you are using to check version is not working under API 9
(and you are supporting since API 8).
You should use:
if (Build.VERSION.SDK_INT > 9) {
Or as problematic function is API 11, check for "SDK_INT>10"
Then for lint errors on eclipse, do as people comment, disable lint errors or add the #SuppressLint("NewAPi") or the target to that function to 11.
For anyone stumbling upon this much later, conditionally executing code based on the API version at runtime is currently a valid way of supporting different platform versions.
See: https://developer.android.com/training/basics/supporting-devices/platforms#version-codes
Where it says:
Android provides a unique code for each platform version in the Build
constants class. Use these codes within your app to build conditions
that ensure the code that depends on higher API levels is executed
only when those APIs are available on the system.
And gives examples:-
Java:
private void setUpActionBar() {
// Make sure we're running on Honeycomb or higher to use ActionBar APIs
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
ActionBar actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
}
}
Kotlin:
private fun setUpActionBar() {
// Make sure we're running on Honeycomb or higher to use ActionBar APIs
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
actionBar.setDisplayHomeAsUpEnabled(true)
}
}

Categories

Resources