Use high API function when targeting lower API - android

in my app minSdkVersion="16" and targetSdkVersion="19". And I wish to use MediaMuxer that introduced in API 18. So when I detetect the API on the phone how can I deside if API>=18 use
MediaMuxer and if API<18 use other function. When targeting minSdkVersion="16" the function MediaMuxer is now shown at all in eclipse.

Test the version number like this
if (Build.VERSION.SDK_INT >= 18) {
// do high api
else {
// do low api
}

You have to check the target version of the Android device using below code
if (android.os.Build.VERSION.SDK_INT >= 16) {
System.out.println("Android Version of device is getter or equal to 16 ::Version::"
+ android.os.Build.VERSION.SDK_INT);
}else{
System.out.println("Android Version of device is getter or equal to 16 ::Version::"
+ android.os.Build.VERSION.SDK_INT);
}

Related

Use setRequiredPasswordComplexity(passwordComplexity : Int) in API less than 31

I want to set password policy on screen lock and now I use setPasswordQuality(ComponentName admin, int quality) in DevicePolicyManager but this method is deprecated on API 31 and they add new method(setRequiredPasswordComplexity(passwordComplexity : Int)).
I can use this method in API < 31?
If I can,how should I use it?
https://developer.android.com/reference/android/app/admin/DevicePolicyManager#setRequiredPasswordComplexity(int)
As the documentation already states the method was added in API 31 and since there is no backward compatibility library for this so this functionality can only be used on devices with api 31 or above
I think your problem might solve here. You can check the build version by
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.S){
// Do something for version 31 and above versions
} else{
// do something for phones running an SDK before 31
}

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

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)

Support higher version of android api

I want to launch NFC settings activity, which is done using action ACTION_WIRELESS_SETTINGS prior to API level 16. But in API level 16 and above it is done using ACTION_NFC_SETTINGS.
I am compiling my source using Android 4.0.3 which is API level 15.
How can I support for higher level so that it can open NFC settings in all versions.
Do i need to compile my source with API level 16 or higher and make min sdk version 15?
You would typically compile your application with the SDK of the highest API version that you use in your code (or the latest available SDK). In your app's manifest you add the minimum and target SDK versions:
<uses-sdk android:minSdkVersion="..." android:targetSdkVersion="..." />
These values define how your app will be treated on a device in terms of (backwards) compatibility features.
Then, in your code, whenever you use API calles that are not available on a certain API level, you would surround those calles with a check for the Build.VERSION.SDK_INT (as JaKoZo showed).
With regard to the NFC settings, you could do even without that check and instead catch the exception that is thrown when no activity is registered for the ACTION_NFC_SETTINGS intent:
try {
this.startActivityForResult(
new Intent(android.provider.Settings.ACTION_NFC_SETTINGS),
1); // magic number to detect when this startActivityForResult returns
} catch (Exception e) {
try {
this.startActivityForResult(
new Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS),
1); // magic number to detect when this startActivityForResult returns
} catch (Exception e1) {
}
}
Instead of the constant ACTION_NFC_SETTINGS, you could also use the hard-coded string "android.settings.NFC_SETTINGS". While I don't really recommend this, you would not need to build against a higher API level (if it's only that constant that causes you to do so).
[...]
this.startActivityForResult(
new Intent("android.settings.NFC_SETTINGS"),
1); // magic number to detect when this startActivityForResult returns
[...]
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
//do stuff
}
u mean this?

Deprecated method, but replacing method requires higher api [duplicate]

This question already has answers here:
setBackground vs setBackgroundDrawable (Android)
(12 answers)
Closed 8 years ago.
I wanted to use view.setBackgroundDrawable(Drawable) but this method is deprecated. It is replaced with .setBackground(Drawable). But my minimum of API 8 can't handle that. It tells me to set the minimum to API 16.
Is there a way to use a different method, based on the API of the device?
Something like
if(API<16)
{
view.setBackgroundDrawable(Drawable)
}
else
{
view.setBackground(Drawable)
}
Or do I really have to change the minimum API to do this?
setBackgroundDrawable is deprecated but it still works so you could just use it. But if you want to be completely correct you should use something like this
int sdk = android.os.Build.VERSION.SDK_INT;
if(sdk < android.os.Build.VERSION_CODES.JELLY_BEAN) {
setBackgroundDrawable()
} else {
setBackground();
}
For this to work you need to set buildTarget api 16 and min build to 7 or something similar.
Something like this:
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN){
view.setBackgroundDrawable(Drawable)
} else {
view.setBackground(Drawable)
}
You can use different methods based on the API versions.
For e.g:
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.FROYO) {
//Methods for version <8 (FROYO)
} else {
// Methods for version >=8
}
Here set your targetSDkversion to any higher versions(for e.g 16 here) and set your minsdkversion to lower versions ( API 7).

Categories

Resources