Monday 31 March 2014

Hidden Android APIs: Settings

In the previous tutorial we talked about the hidden Android APIs, how to discover them and how to use them in our apps. To be more precise, we saw that the easiest way to use the hidden APIs is to copy hidden constants from the Android source code into our Activities (this way we can, for example, set up a BroadcastReceiver to intercept incoming SMS messages).



The same method can be used to start Activities to change specific system settings directly from our app. As far as system settings are concerned, there are some settings that are available in the official and public APIs. An example is the constant Settings.ACTION_AIRPLANE_MODE_SETTINGS, through which we can show settings to allow entering/exiting airplane mode. But there are a lot of other settings which are available only in the hidden APIs. To find them, look for the Settings class with AndroidXRef, as follows:


To find the hidden settings you just have to look for the "@hide" tag. You will find all the constants you can use to create Intents to launch specific parts of the settings UI.
For example, if you want to launch an Activity to check system updates, you can use the hidden constant Settings.ACTION_SYSTEM_UPDATE_SETTINGS:



As you can see from the Android source code, this constant is marked with the "@hide" tag, so it is not visible in the official API.
You can use this constant in the following way:

Intent pref = new Intent("android.settings.SYSTEM_UPDATE_SETTINGS"); startActivity(pref);

When you want to use hidden constants to change system settings you must be aware of two things:

  1. in some cases a matching Activity for a given constant may not exist, so ensure you safeguard against this;
  2. for some settings you have to add special permissions to the AndroidManifest.xml file (you can refer to the error messages in the Logcat to discover the permissions needed).

No comments:

Post a Comment