Is this considered reflection and to what degree?

I have an Android application (java) that was working fine when compiled with the Android 1.6 SDK using the following code from the android.provider.Contacts class:

Uri baseUri = Contacts.Phones.CONTENT_FILTER_URL;

When the 2.0 SDK came out, the android.provider.Contacts class was depreciated and replaced with android.provider.ContactsContract. In order to get one program to work on both 1.6 and 2.0, I compiled under 1.6 with the following change:

Uri baseUri = Contacts.Phones.CONTENT_FILTER_URL;
…
try {
    Class<?> c = Class.forName("android.provider.ContactsContract$PhoneLookup");
    baseUri = (Uri) c.getField("CONTENT_FILTER_URI").get(baseUri);
} 
    catch (Exception e) {           
}

Since I was compiling under 1.6, I could not import android.provider.ContactsContract since it is a class known only to 2.0. Is this considered reflection and to what degree?

Added Comment: After reading the "Reflection" chapter of "The Java Programming Language" (which I should have done first), I mostly now understand what you can do with reflection but a concise definition of reflection is not easy to come by. Your answers have helped to clarify what prompted my question - that once a class has been reflected on, and an instance of the class created using reflection, you can interact with the instance as if the class was new'ed.

Here is my meager attempt at a concise definition that is far from perfect and I am sure I will need to revise as I learn more:

Reflection is the indirect, dynamic inquiry, manipulation or invocation of class objects using class objects contained in java.lang.reflect or the Class or Package classes that requires initially accessing the class using a fully qualified string name.


Asked by: Ryan217 | Posted: 25-01-2022






Answer 1

I believe that is the very definition of Java reflection (more on Android reflection for multiple-version compatibility). I'm not sure what you mean by "to what degree"; it just is.

Answered by: Chloe462 | Posted: 26-02-2022



Answer 2

Dynamically asking for the availability of a method is a form of reflection, yes.

Answered by: Max568 | Posted: 26-02-2022



Answer 3

It's reflection.

If CONTENT_FILTER_URI is a final static field, then you should use get(null) instead of get(baseUri) because you are not invoking an object.

Edit

I was a bit confused by your code. As I understand your snippet, first you assign Contacts.Phones.CONTENT_FILTER_URL to URL baseUri, then you reflect the CONTENT_FILTER_URI field on the PhoneLookup class and read that fields value from the URL instance stored in baseUri - just to assign the value to baseUri again !? Typo or room for improvement?

Answered by: Emma159 | Posted: 26-02-2022



Answer 4

This is a very good article on strategies, reflection and other more sophisticated things, for using new APIs while remaining compatible with older platforms:

http://android-developers.blogspot.com/2009/04/backward-compatibility-for-android.html

Answered by: Michelle282 | Posted: 26-02-2022



Similar questions

android - Is a dialogBox considered a View?

Is a DialogBox considered a View in Android? I want to add one to a FrameLayout, but I'm not sure if I can? Is it possible?


android - Why is screen drawing on mobile devices considered "expensive"?

I always see people talking at GoogleIO or WWDC about how drawing the screen(bits/pixels) on mobile devices is so expensive. Can anyone explain why this is?


java - Is declaring an outer class variable final to access it in inner class considered good practice?

In Java when you need to access to an outer class member or variable from an inner class, you have to declare it final. See this question. My question is: is this a good practice? Particularly when I write code for android I often use solutions similar to this one: ...


html - <div>, <p> tags are not considered as block elements in Android browser

I have a strange issue with Android browser. the div, p tags aren't considered as blocks in my phone's Android browser. I've used the Eric Myer's reset in the beginning of the style sheet. And here is my HTML code - &lt;body&gt; &lt;div id="mainwrap"&gt; &lt;p&gt;Some paragraph&lt;/p&gt; &lt;/div&gt; &lt;/body&gt; In that Android browser, p tag is just 50% of the mainwrap. I tried ...


what is considered `click event` in android?

in iOS world, there's a concept of touchUpInside, touch started within a view and user lifted a finger within a view bound. I see touch vs click (event listeners) in android, and I'd like to know the difference between them.


java - if(...) - two equal sides are considered as not equal

This question already has answers here:


android - What size xml file should be considered too large for a DOM parser?

I have been developing a fairly simple app for iOS and Android that parses local xml in order to form lists of items and show information about them. When looking into my parsing options, I settled on the native SAX parser for android, as well as the NSXMLparser class that is built in to objective-C. When looking at my options, I repeatedly saw people saying that DOM parsers are not suitable for large xml files. However, n...


android - Is header of ListView considered item?


process - android: when exactly is a task considered background?

I'm trying to kill a specific application with a specific packageName. So this is what I'm doing: I use a service, the service listens for the foreground processes and once found the specific package, it fires off a killer activity, the killer activity is now a foreground and has just a textview, showing the correctly delivered packagename. In the the killer activity I'm trying all of these killer commands and nothing happ...


android - "&" Symbol in String of my class is not considered as a part of it

I have a String s="&amp;0_o10&amp;"; I am using this string in a webservice call as url. This string is a part of the url.I have dubugged the code , but its taking the string as "0_o10" instead of "&amp;0_o10&amp;". Please help me .






Still can't find your answer? Check out these communities...



Android Google Support | Android Community | Android Community (Facebook) | Dev.io Android



top