In addiction to being somewhat messy, reflection can be used in dangerous ways. Remember you are using code in which the programmer never intended. Sometimes this can result in humorous things (see Crashing an in flight entertainment system). Sometimes your great plans can go down in flames.
Looking through the source code of the Android platform (which can be downloaded very easily from here if you have a mac or linux computer), you can find out how to do things that are not in the standard source. In this case I was looking for how to enable/disable GPS for my ToggleGPS app.
You can see the code for the Security and Location settings screen here.
It's not for the faint of heart. I wouldn't suggest doing this except for experienced developers because a lot of times you encounter code and terminology that you haven't seen before.
All the magic of enabling GPS happens with this line
// Inform the location manager about the changes
However, if you check out the Android SDK reference for LocationManager you find out that there isn't an updateProviders() method.
It's private, it's hidden. How google is calling it here I'm not really sure. We can get the LocationManager in our code by
LocationManager locationManager =
And as I showed in my last post you can access "hidden" methods using reflection.
Method updateProvidersMethod =
Under the hood things are not as they seem. If you try to run this in code in the emulator you get a very strange exception. Buried in the stack trace you find:
W/System.err( 415): Caused by: java.lang.SecurityException: Permission Denial:
getIntentSender() from pid=415, uid=10023 is not allowed to send as package andr
Now I didn't bother looking through the Android code to figure out what is going on under the hood, but the message "not allowed to send as package android" stopped me in my tracks. You can use reflection to trick java into doing what you want it to do but this you can't trick. I'm guessing, but the error message probably means that I was trying to access operating system protected code.
But there ARE apps in the Android Market that can do this. But how?
The answer to this is surprisingly simple. But that is for another post.