#javascriptinterface on Android lower then 4.2 version - android

I use D3SView library in my app in payment process. It uses #javascriptinterface in one of functions. Can I use it on Android >= 2.3 ? link to required class.
code:
addJavascriptInterface(new D3SJSInterface(), JavaScriptNS);
...
class D3SJSInterface {
D3SJSInterface(){}
#android.webkit.JavascriptInterface
public void processHTML(final String paramString) {
completeAuthorization(paramString);
}
}
How to make this code allowable for >= 2.3 android version?

Can I use it on Android >= 2.3 ?
Yes, though your compileSdkVersion (a.k.a., "build target" in Eclipse) will need to be API Level 17 or higher. On the older devices, the annotation is ignored.

Related

Using android.os.Build.VERSION to use estimote library

I create android app, which is connected with estimote beacons. My project has to use estimote library and this library minimum sdk is set to 18, so minimum sdk of my whole project has to be 18. But I want my application works on minimum sdk 14. Is there any possibility to set my project min sdk to 14 and when sdk is older than 18 to turn off using this library?
SOLUTION
I have to use multiple apk to determine which devices can download version with estimote library and which have to download lower api version.
More info:
http://developer.android.com/google/play/publishing/multiple-apks.html
You can do it by using this check:
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR2){
// turn off that library here
}
If you cannot stop the library for some reasons then do this.
#SuppressLint("NewApi")
#Override
protected void onCreate(Bundle instantanState) {
.
.
.
if(Build.VERSION.SDK_INT >= 18){
// start your library here.
}
}
Make sure to add #SuppressLint("NewApi") before the function in which it is called or you can turn off android lint error checking for new Api features.
When you want use your library into your app check the OS version with
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2){
// use your library here
}
but don't forget to manage previous api:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2){
// use your library here
}
else {
//do something else
}

Tool to Check if code contains higher API calls than minimum level

I am working with a large project, which has a minimum API level:16. however, I came across API usages that are above API level 16.
Is there any tool in Android studio or elsewhere, other than testing with a device, to check if the code doesn't violate the minimum required API level or better point it out like an error etc.?
Thank you.
The IDE will use the minimum android SDK, thus you will not get compile errors. If you there are classes in SDK 14 which are moved in sdk 16, yet you are using the imports from SDK 14, it will give a standard compile error.
So no, not that I am aware of.
You can use something like this:
public static boolean supports(final int version) {
return Build.VERSION.SDK_INT >= version;
}
Like this,
if (supports(Build.VERSION_CODES.HONEYCOMB)) {
// do something HONEYCOMB+ compatible here
}
More codes here,
http://developer.android.com/reference/android/os/Build.VERSION_CODES.html

How to support different Android versions?

I found that from API 19+ their is a small change in code and it doesn't appear in API 19-.
How can I support both android versions in the same app?
EDIT: The are feature that exists only in android 4.1.2 and in versions below 4.1.2 they doesn't exists.
How can I make an app that support both versions with the exception that in lower version the "new" features won't exist? (its hard for me to explain)
You can wrap the API 19-specific code inside a conditional that queries the device version with the enumerated values present in Build.VERSION_CODES.
for instance :
if ( android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT )
{
// Kitkat (API level 19) code in here...
}
else {
// code for all versions lower than Kitkat in here...
}

How to use android apis higher than actual build target

When building android apps for different behavior on different targets we can do:
if (Build.VERSION.SDK_INT < 10) {
Toast.makeText(this.context, "Not Supported", Toast.LENGTH_LONG ).show();
} else {
this.doComplicatedOperation();
}
But in the method doComplicatedOperation() it would be logical to use higher api classes than the current build target (eg. api 5), but lint keeps complaining that ClassIntroducedInApiLevel11 can not be resolved as a type
How could I change the code of doComplicatedOperation() that my project compiles?
#TargetApi(11)
private void doComplicatedOperation() {
ClassIntroducedInApiLevel11 = new ClassIntroducedInApiLevel11();
}
The purpose of minSdkVersion is to exclude platforms where you do not provide backward compatibility support.
In order to provide the proper libraries for your build you need to set a higher targetSdkVersion so the IDE or whatever knows what libraries to include when creating your APK.
It sounds like you don't want to target a higher SDK because some methods or objects may be deprecated or even unsupported. That's when you use support libraries, if necessary, for backward compatibility.
You need to change your SDK target to at least the SDK that the method is in so in. You should always be targeting the highest API there is available (currently 19) so your manifest should look like this
<uses-sdk
android:minSdkVersion="5"
android:targetSdkVersion="19" />
if you are still targeting SDK 5 you are in the dark ages

Android - How to use different code lines depending of Android version

I have a project which is compatible with Android versions from 10(GINGERBREAD_MR1) to 17(JELLY_BEAN_MR1).
So, I would like to use setBackgroundDrawable for versions lower to 16 and setBackground from version 16 or upper.
I've tried this:
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
subMessageFromToLinearLayout.setBackgroundDrawable(null);
} else {
subMessageFromToLinearLayout.setBackground(null);
}
But, Eclipse gives me:
A warning for subMessageFromToLinearLayout.setBackgroundDrawable(null);:
"The method setBackgroundDrawable(Drawable) from the type View is deprecated"
And an error for subMessageFromToLinearLayout.setBackground(null);:
"Call requires API level 16 (current min is 10): android.widget.LinearLayout#setBackground"
How can I fix this errors in order I can use both lines depending of the running Android version?
Thanks in advance.
In general the most robust way makes use of class lazy loading:
static boolean isSDK17()
{
return android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1;
}
if (isSDK17())
xyMode_SDK17.setXyMode(context, mode);
else
xyMode_SDK8.setXyMode(context, mode);
#TargetApi(17)
public class xyMode_SDK17
{
static void setXyMode(Context context, boolean mode)
{...}
}
public class xyMode_SDK8
{
#SuppressWarnings("deprecation")
static void setXyMode(Context context, boolean mode)
{...}
}
Have you seen
ActionBarSherlock gives tons of "Call requires API level 11 (current min is 7)" errors
Android - set layout background programmatically
You can mark it with #TargetApi(16) and #SuppressWarnings("deprecated").
If the error still there, try cleaning the project or restart eclipse.
"ah I know of the .setBackgroundDrawable(Drawable) method but to me the IDE had the same error with api 16 requirement. I am using Eclipse and it seemed to be a bug after reopening the ide and cleaning the code a bit it worked. Than you very much and sorry for trouble".

Categories

Resources