API missing from PackageManager - android

It's weird I found PackageManager has public APIs like installPackage(...)/deletePackage(...) although in the comment it's marked as Hide, here http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/2.2_r1.1/android/content/pm/PackageManager.java#PackageManager.deletePackage%28java.lang.String%2Candroid.content.pm.IPackageDeleteObserver%2Cint%29, but the reference page google provides hide this API.
So my question is how can I access such APIs?
Thank you all very much for help.
Kindest regards,
Nessus.

It was declared as public just to simplify the SDK developers life. But it never was expected to be called by outside developers. See also this post.
But if you really need to call this method, you can use reflection. Thought I suggest to look around for an alternative solution using official API.

In general you should not use "hidden" APIs in your applications. These methods are for internal usage and can be changed or even removed without notice. They are often not available for non-system applications due to permission restriction. If you want to use these APIs in your application then you may build your app within the platform build.
There is also a workaround to build application that uses hidden API in Eclipse: you can collect compiled framework libraries from the platform build and add them to build path as "User library", framework_intermediates/classes-full-debug.jar should be enough for the package manager.

Related

What's the best way to support deprecated methods which are replaced by new ones?

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.

how detect deprecated functions in android apps

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!!!

SDK version required annotation

I'm calling a function through reflection which is available since froyo. How can I mark that this call requires froyo?
There is the annotation #TargetApi(Build.VERSION_CODES.FROYO) but this does just prevent warnings I want to generate a warning if this version does not match.
If there is no way from the SDK site is it possible to write such an annotation? I'm not familiar with that.
Annotations are just metadata and need tools to actually process them, there's no lint check to do the reverse of what you want: indicating that something requires a certain API according to http://tools.android.com/tips/lint-checks.
If you are providing an API, it would be best to document the requirement in the Javadoc of the function so that a caller is aware that that particular call requires Froyo. In the function you should probably throw an exception if this pre condition is not satisfied or handle it in some other way.
Another option, if you are creating a library, is to set the minSdk version of that project to 8 or up.
You also could write your own lint rule that would do a custom check I guess. More details on writing lint rules are here: http://tools.android.com/tips/lint-custom-rules. The downside is that users of your api have to perform some custom setup to activate the rule.
I totally forgot to mention this, but this annotation #RequiresApi was actually introduced in Support Lib v24 back in 2016, after raising a bugreport.

How to acces android.provider.Telephony class?

It seems that this class is not public, and I have to access TELEPHONY_STATUS variables in this class because I see in android source code that the values can change considering what api version you are using.
Read this post from the android-developer mailing list.
android.provider.Telephony is part of the Open Source releases, but never included as part of the Official SDK.
You can view it, to understand how the system works, but you can't actually use it in your apps.
As it says in the post,
No, it does not appear in the SDK, and this means
you should not try to use it from your applications.

What does "This method is deprecated" mean for application developers

I see quite a few good old useful methods or even entire classes being "deprecated and obsolete".
But code that used to call those methods continues to work. So, what does this mean to me, as an Android applications developer?
Continue using this method as long as I want, because newer SDKs
will always remain backward compatible.
It will work as long as I build for older targets (e.g. API 8), but
if I build from API 14 up, the compiler will refuse to complete
the build.
Both (1) and (2)
Other?
This is especially confusing when no alternatives are provided, as in the case of WebView.PictureListener.html#onNewPicture.
It usually means that there's either a better way of doing things or that the deprecated functionality had some irreparable flaw and should be avoided. You can usually keep using deprecated methods, but you are advised to either switch to some new API (in the first case) or find some other way of doing what you want (in the second).
Regarding onNewPicture in particular, the entire PictureListener interface is deprecated. There's no sign of what, if anything, is supposed to replace it. A comment by #CommonsWare in this thread is food for thought:
It is conceivable that upstream changes in WebKit are driving the deprecation and that support for PictureListener might be totally lost in some future release.
I would go with 4:
It will basically tell you that the use of the method or class is discouraged; it is NOT 100% that they will keep backward compatibility (they can decide to not include that method in future releases), so you should try to use the replacement of the method or class. This is sometimes not possible to use the new methods (for instance, if you want to support devices running older versions).
Some other times it is actually possible. For instance, the showDialog method is now deprecated and they recommend to use DialogFragment class. You can achieve that even in older versions of Android by using the compatibility library.
Deprecated methods are not guaranteed to remain backwards compatible. They might remain in there for a few more releases just to give everyone a chance to migrate away from them before the developers remove them. The fact that they're deprecated means that the developers think that there's an easier, faster, neater, or otherwise better way to do whatever that class or method does.
It's probably better to change your code to use a non-deprecated interface now, since if you wait and it does get removed, your users will see crashes and errors.
Even when they are deprecated, they may compile but not work. Google has decided to delete various functionality at the low OS level.
Case in point. Google, at android release 2.3 deprecated many but not all method API's that allowed call recording. They compile OK but do not function since Android 2.3 and forward on any android phone device, or tablet with phone capabilities.
As an example for a deprecated interface that has been removed in a later API level, consider the org.apache.http package: It has been deprecated in API level 22 and removed in API level 23.
Of course on actual Android devices, the classes contained in that package will still be available in the system libraries (otherwise, applications targeting an older Android release would no longer run on that device).
They are however not available in the SDK anymore, so compilation will fail unless you either change the target/build SDK to an older version (or manually include the deprecated classes).
If Google were really determined to discourage use of those libraries, they could modify the implementation so that the affected classes check the target API version of the running application and complain and/or throw a runtime exception.

Categories

Resources