Basically I want to run a code if the OS is Honeycomb or latter, and a different code if not. Should I compare using > or >= ?
if(Build.VERSION.SDK_INT > Build.VERSION_CODES.HONEYCOMB) {
} else {
}
To use HoneyComb use >=
i.e. code as
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
} else {
}
For more info see at Android Check for HoneyComb
As per your requirement used >= means with HoneyComb
Related
How to get the device is supported for Dual Sim for Below Android R.
I am using the following code. But I am getting a deprecated warning for telephony.getPhoneCount().
How to get for below Android R. Or do we need to add #SuppressWarnings( "deprecation" )?
public boolean isDuelSim(Context pContext) {
TelephonyManager telephony = (TelephonyManager) pContext.getSystemService(Context.TELEPHONY_SERVICE);
if (telephony != null) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
return telephony.getActiveModemCount() == 2; //Dual SIM functionality
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
return telephony.getPhoneCount() == 2;
}
}
return false;
}
The getPhoneCount() method seems to be the right way of checking if the device supports dual SIM on api levels from M to Q. If you are compiling your project for R or greater, you will get the deprecation lint warning.
IMO, your implementation is correct and if you need to suppress that warning then either add the #SuppressWarnings("deprecation") annotation to your isDualSim() method signature or the //noinspect comment above the telephony.getPhoneCount() call.
BTW, I'd also suggest considering the case when getActiveModemCount() or getPhoneCount() returns a 3. Whether a tri SIM phone should pass your dualSIM test or not depends on your use case, of course.
I am trying to use #ChecksSdkIntAtLeast annotation to convert our java utility functions into kotlin properties that would not require #SuppressLint("NewApi").
But anyway I write the property, it stops red underlining code for both branches of if.
Demo:
#get:ChecksSdkIntAtLeast(api = Build.VERSION_CODES.O)
val isAtLeastOreo: Boolean
get() = Build.VERSION.SDK_INT >= Build.VERSION_CODES.O
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
manager.getNotificationChannel("general")
} else {
manager.getNotificationChannel("general") // CORRECTLY UNDERLINED
}
if (isAtLeastOreo) {
manager.getNotificationChannel("general")
} else {
manager.getNotificationChannel("general") // NOT UNDERLINED - WHY??
}
Same thing happens if I make it into function instead of getter. The only working one would be using lambda, but then I am losing if-else construct and would require massive amount of places to refactor.
Issue has been reported on Aug 23. Waiting for response
https://issuetracker.google.com/issues/197428342
I am working on a project and I noticed that this check if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { only takes care of API level 30 and above for API level 29 and below it does not. I however don't know the correct flags to use to like handle this case. For instance else {... showStatuBar and ... show navigationBars} any ideas on how to show status bar and navigation bar on API level 29 and below.
private fun showUI() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
activity?.window?.setDecorFitsSystemWindows(true)// ensures the layout fits system window
activity?.window?.insetsController?.show(
WindowInsets.Type.statusBars()
or WindowInsets.Type.navigationBars()
)
} else {
// What to add
}
}
The Android oficial documentation's all version codes documented take a look at the link bellow:
Version Codes
Follows a small example bellow, just try to adapt at will:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
//Your code go here
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOOLIPOP) {
//Your code go here
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
//Your code go here
}
Or like the code versions are just a Int value you can use ranges and when instead if:
when (Build.VERSION.SDK_INT) {
>= 30 -> {
//Your code go here
}
in 26 .. 29 -> {
//Your code go here
}
in 21 .. 25 -> {
//Your code go here
}
}
References:
https://www.programiz.com/kotlin-programming/if-expression
https://kotlinlang.org/docs/ranges.html
https://www.programiz.com/kotlin-programming/when-expression
https://typealias.com/start/kotlin-conditionals/
Is there any way I can support both deprecated and new API in the same method call for Android? I'm using the camera API which seems to be deprecated for the Lollipop version, so I tried to handle it like this:
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.LOLLIPOP)
{
//Before Lollipop, use the Camera API since it still supported.
}
else
{
//Use the CameraManager
try
{
for (int i= 0; i < _camera.getCameraIdList().length; i++)
{
System.out.println("Camera= " + _camera.getCameraIdList()[i]);
}
}
catch (CameraAccessException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
But this does however just give me the error Call requires API level 21 (current min is 15): android.hardware.camera2.CameraManager#getCameraIdList
I tried SupressLint and TargetApi but that only made the device running an earlier (before Lollipop) Android version crash when creating an class instance of this type.
Thanks for any help!
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
//handler lollipop and higher
} else {
//earlier api calls
}
Simply having code in a class does not crash any Android 2.0+ device - code actually needs to run for it to crash. Check to make sure all of your Lollipop specific code is wrapped in version checks.
So the code is following:
public static ActionBarHelper createInstance(Activity activity) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
return new ActionBarHelperICS(activity);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
return new ActionBarHelperHoneycomb(activity);
} else {
return new ActionBarHelperBase(activity);
}
}
If my device is 2.3 (api 9) it shouldn't support Build.VERSION_CODES.ICE_CREAM_SANDWICH but yet it runs, why is that? If it had been a method I called it would have crashed. Is it because it is a public static final int and therefor added from to my code? Please give some general detail and not just a yes/no answer :)
As Stefan pointed out, the api level used to compile the code determines the constants visible at compile time. For my case this doesn't give any problems as I just compare simple values. This only give problems if you use a set method that uses different constants and you happent to use a constant that wasn't possible to handle for your api version.