I need to call a method in Android SDK v9 while maintaining compatibility with older versions.
The Android developer blog recommends using reflection or wrapper classes, but is that all really necessary? Why can't I just do this?
if (Build.VERSION.SDK_INT >= 9)
callNewMethod();
It seems to me this will work due to Java runtime linkage, since I am building with SDK 9. Is there anything wrong with this approach?
Thanks in advance...
No not at all. Actually it is promoted as best approach while developing applications with wide API level target.
Reflection class is the most solid way, if you have no idea what the class content is and the method exist. But in Android, you know what is supported and what is not supported.
As a result, i didn't like the blog you gave :p
Not necessarily a better answer, but the check you are doing there assumes that the builder of the OS/ROM set that value correctly. If it was not set correctly, then you may try to access a method in SDK 9 that really isn't there. Using reflection is the only way to be 100% sure you do not generate a runtime error by trying to call a non-existent method.
Related
Some methods are marked with a line on it. Is this some kind of error ?? this thing bothers me but the applications work fine.
Deprecated methods. That means that there are newer alternative methods available to be used which are more compatible. Deprecated methods do work, but Google does not guarantee their proper functioning. And support for those methods may be ended in future versions of android.
They are deprecated. Consider using ones who aren't.
I want to build an application where the front camera of the device is used to project the current image to a SurfaceView. All the tutorials I found so far implement this by using a Camera object from the android.hardware package.
This method, however, seems to be deprecated. When trying the 'new' preferred way to implement this feature following the Android documentation, I get the warning it can only be used with API level 21, which is pretty useless.
So I would like to know the currently preferred way to implement camera functionality in an application. Or is there maybe some support library for API levels lower than 21?
Thanks in advance.
Deprecated interface does not mean you should not use it. It means you should know that it will be phasing out in the future.
As a general rule, it is better to use a newer interface if possible, in order to avoid the need to update the software later.
The fact that API level 21 does not yet have a large enough market share means that you are probably better off using the old interface for now, and keep in mind that in a year or two, you may need to update the implementation.
I think you can implement the camera function in both sets of API and check the device`s build version first then decided to call which one implementation.
eg:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
openCameraNewAPI();
}else{
openCameraOldAPI();
}
I like to use the findAll/ findAllAsync method in android.webkit.WebView. findAll is deprecated and Google suggests to use findAllAsync which requires Jelly Bean or higher. However, I like my application to support 2.2+. I tried to the following, but I get warning for findAll (deprecation) and error for findAllAysnc (need to increment minimum SDK version):
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
myWebView.findAll(query);
else
{
myWebView.findAllAsync(query);
}
What's the best way to deal with this? Should I just use findAll and ignore the deprecation warning?
I believe the answer goes in several ways:
What are you setting as min SDK version and target SDK version in the manifest ?
Same question, but in Eclipse (or whatever IDE you're using) for Android build API properties ?
(I'm answering as if your question is "how do I get rid of the android lint warning", rather than "how do I fix the warning correctly" .. )
Use findAll and if the warning is really too annoying add a #SuppressWarning("deprecation") annotation to suppress it explicitly.
One problem with this is that when you use this annotation on your method you might miss other deprecated calls as it will apply to the whole method.
There's a very interesting and powerful way of doing what you want, and it is called reflection. From the Oracle's Java documentation:
Reflection is commonly used by programs which require the ability to
examine or modify the runtime behavior of applications running in the
Java virtual machine. This is a relatively advanced feature and should
be used only by developers who have a strong grasp of the fundamentals
of the language.
In short, reflection allows you to find out if class is defined, you can find out its methods and properties, and invoke a class' functionality... all at runtime!
I have an app that needs to handle the audio focus on Android 2.1 devices but the Audio Focus is not available for those versions, so I use this technique. It is, by the way, a bumpy road. I would suggest reading carefully the documentation and try to follow some examples.
For my new assignment, I wanted to use some library that can provide a "Posterize effect". I found many library like Aviary SDK and jhlabs, Yes, these are easy to use, but these making the code heavier. SO I keep searching for Android's API itself which can do the similar task. And after a lot of RnD, I finally found one my time saver class EffectsFactory which provides the same as I wanted. I applied it in my assignment also. But the bad thing it was added in API level 14, And my app should be compatible with at least API level 8.
So, My question is,
Can I use EffectsFactory class for lower version? If Yes then How?
Or, If No, Then Do we have any API in Android SDK itself which do similar to effectfactory ?
Please avoid referencing any library or NDK's open cv library.
No, there is not an Android API that will posterize an image below API 14. Even above API 14 EffectsFactory may not work, as it says in the Android documentation:
Some effects may not be available on all platforms, so before creating a certain effect, the application should confirm that the effect is supported on this platform by calling isEffectSupported(String).
However, you could easily make a lightweight solution yourself. Posterization is a simple process. For example, the code behind JHlabs' posterize filter is less than 50 lines (and most of them are sugar). In your shoes, if using a 3rd party library was out of the question, I wouldn't hesitate to write my own.
Edit: If you happen to be posterizing images your app takes from the camera, there is also Camera.Parameters.setColorEffect(), but again this is not supported on all devices, as it says in the documentation:
For example, the application should call getSupportedColorEffects() before calling setColorEffect(String).
how detect deprecated method in android application? which resource is needed, for example class files..
My problem is the derecated error is reported in local eclipse but not reported in server for same source. Then what should I check for this problem? please help
Your problem is not really clear.
If Eclipse says the method is deprecated, then it is.
The documentation often tells you what is the new and better way to do what the deprecated method did.
In android doc (http://developer.android.com/reference/packages.html), you can choose the target version on the top of the list on the left, so you should check the doc for the version you are targeting.
It might be acceptable for you to keep a deprecated method if you absolutely need it and want your min version to be an old one. You can also check the support package that provide you compatibility functions of newer versions
Extending #Toaster a bit, deprecated functions are those that are currently supported but may not be supported by future versions. In simpler words, current android versions such KitKat (4.4) will be able to run that function but another update may find that function doesn't exist anymore.
The eclipse gives us warning not show problem that this particular version is working fine now but may be removed in the future releases, so change that function with a function that is not depcrecated and will certainly be present in future versions so that your app may work properly even in case or an OS update.
As far as problem showing the deprecated functions on your server is concerned, i don't know it. Although you might not need it, if you upgrade your functions, but if you do then post a different question and i am sure someone will be able to help you. Happy Coding!!!