Last time I introduced how to use the reflection mechanism of JAVA to call the hidden APIs of Bluetooth. This time I continued to practice the reflection mechanism of JAVA and explored several functions that TelephonyManager contains in the Framework but hidden in the SDK. Let's take a look at the program running in this article:
This program demonstrates the following functions:
1. All incoming calls are automatically answered;
2. All incoming calls are automatically hung up;
3. enable/disable Radio;
4. enable/disable data connections (WAP or NET connections ).
To call the hidden API of TelephonyManager, first refer to the aseelephonyjavacomandroidinternalelephonyITelephony of the Framework. aidl, and then implement an ITelephony. aidl: In TelephonyManager, the custom ITelephony is instantiated through the reflection mechanism. After the instantiation, the functions in ITelephony can be called.
The program in this article needs to add the following two lines of code in AndroidManifest. xml to obtain the permission:
View plaincopy to clipboardprint?
<Uses-permission android: name = "android. permission. CALL_PHONE"/>
<Uses-permission android: name = "android. permission. MODIFY_PHONE_STATE"/>
<Uses-permission android: name = "android. permission. CALL_PHONE"/>
<Uses-permission android: name = "android. permission. MODIFY_PHONE_STATE"/>
The source code of main. xml is as follows:
View plaincopy to clipboardprint?
<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: orientation = "vertical" android: layout_width = "fill_parent"
Android: layout_height = "fill_parent">
<RadioGroup android: layout_height = "wrap_content"
Android: layout_width = "fill_parent" android: id = "@ + id/rGrpSelect">
<RadioButton android: layout_height = "wrap_content"
Android: layout_width = "fill_parent" android: id = "@ + id/rbtnAutoAccept"
Android: text = "automatic answers to all incoming calls"> </RadioButton>
<RadioButton android: layout_height = "wrap_content"
Android: layout_width = "fill_parent" android: id = "@ + id/rbtnAutoReject"
Android: text = "automatic suspension of all incoming calls"> </RadioButton>
</RadioGroup>
<ToggleButton android: layout_height = "wrap_content"
Android: layout_width = "fill_parent" android: id = "@ + id/tbtnRadioSwitch"
Android: textOn = "Radio started" android: textOff = "Radio disabled"
Android: textSize = "24dip" android: textStyle = "normal"> </ToggleButton>
<ToggleButton android: layout_height = "wrap_content"
Android: layout_width = "fill_parent" android: id = "@ + id/tbtnDataConn"
Android: textSize = "24dip" android: textStyle = "normal" android: textOn = "allow data connection"
Android: textOff = "Disable data connection"> </ToggleButton>
</LinearLayout>
<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: orientation = "vertical" android: layout_width = "fill_parent"
Android: layout_height = "fill_parent">
<RadioGroup android: layout_height = "wrap_content"
Android: layout_width = "fill_parent" android: id = "@ + id/rGrpSelect">
<RadioButton android: layout_height = "wrap_content"
Android: layout_width = "fill_parent" android: id = "@ + id/rbtnAutoAccept"
Android: text = "automatic answers to all incoming calls"> </RadioButton>
<RadioButton android: layout_height = "wrap_content"
Android: layout_width = "fill_parent" android: id = "@ + id/rbtnAutoReject"
Android: text = "automatic suspension of all incoming calls"> </RadioButton>
</RadioGroup>
<ToggleButton android: layout_height = "wrap_content"
Android: layout_width = "fill_parent" android: id = "@ + id/tbtnRadioSwitch"
Android: textOn = "Radio started" android: textOff = "Radio disabled"
Android: textSize = "24dip" android: textStyle = "normal"> </ToggleButton>
<ToggleButton android: layout_height = "wrap_content"
Android: layout_width = "fill_parent" android: id = "@ + id/tbtnDataConn"
Android: textSize = "24dip" android: textStyle = "normal" android: textOn = "allow data connection"
Android: textOff = "Disable data connection"> </ToggleButton>
</LinearLayout>
PhoneUtils. java is a mobile phone function class. it instantiates ITelephony from TelephonyManager and returns it. The source code is as follows:
View plaincopy to clipboardprint?
Package com. testTelephony;
Import java. lang. reflect. Field;
Import java. lang. reflect. Method;
Import com. android. internal. telephony. ITelephony;
Import android. telephony. TelephonyManager;
Import android. util. Log;
Public class PhoneUtils {
/**
* Instantiate ITelephony from TelephonyManager and return
*/
Static public ITelephony getITelephony (TelephonyManager telMgr) throws Exception {
Method getITelephonyMethod = telMgr. getClass (). getDeclaredMethod ("getITelephony ");
GetITelephonyMethod. setAccessible (true); // Private functions can also be used
Return (ITelephony) getITelephonyMethod. invoke (telMgr );
}
Static public void printAllInform (Class clsShow ){
Try {
// Obtain all methods
Method [] hideMethod = clsShow. getDeclaredMethods ();
Int I = 0;
For (; I Log. e ("method name", hideMethod [I]. getName ());
}
// Obtain all constants
Field [] allFields = clsShow. getFields ();
For (I = 0; I <allFields. length; I ++ ){
Log. e