This article from http://blog.csdn.net/hellogv/, reference must indicate the source!
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/base/telephony/Java/COM/Android/Internal/telephony/itelephony 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:
<Uses-Permission Android: Name = "android. Permission. call_phone"/> <br/> <uses-Permission Android: Name = "android. Permission. modify_phone_state"/>
The source code of Main. XML is as follows:
<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> Android: Orientation = "vertical" Android: layout_width = "fill_parent" <br/> Android: layout_height = "fill_parent"> <br/> <radiogroup Android: layout_height = "wrap_content" <br/> Android: layout_width = "fill_parent" Android: id = "@ + ID/rgrpselect"> <br/> <radiobutton Android: layout_height = "wrap_content" <br/> Android: layout_width = "fill_parent" Android: id = "@ + ID/rbtnautoaccept" <br/> Android: text = "all incoming calls are automatically answered"> </radiobutton> <br/> <radiobutton Android: layout_height = "wrap_content" <br/> Android: layout_width = "fill_parent" Android: Id = "@ + ID/rbtnautoreject" <br/> Android: TEXT = "all incoming calls are automatically hung up"> </radiobutton> <br/> </radiogroup> <br/> <togglebutton Android: layout_height = "wrap_content" <br/> Android: layout_width = "fill_parent" Android: Id = "@ + ID/tbtnradioswitch" <br/> Android: texton = "radio started" Android: textoff = "Radio disabled" <br/> Android: textsize = "24dip" Android: textstyle = "normal"> </togglebutton> <br/> <togglebutton Android: layout_height = "wrap_content" <br/> Android: layout_width = "fill_parent" Android: Id = "@ + ID/tbtndataconn" <br/> Android: textsize = "24dip" Android: textstyle = "normal" Android: texton = "allow data connection" <br/> Android: textoff = "Disable data connection"> </togglebutton> <br/> </linearlayout> <br/>
Phoneutils. Java is a mobile phone function class. it instantiates itelephony from telephonymanager and returns it. The source code is as follows:
Package COM. testtelephony; </P> <p> Import Java. lang. reflect. field; <br/> Import Java. lang. reflect. method; <br/> Import COM. android. internal. telephony. itelephony; <br/> Import android. telephony. telephonymanager; <br/> Import android. util. log; </P> <p> public class phoneutils {<br/>/** <br/> * instantiate itelephony from telephonymanager, returns <br/> */<br/> static public itelephony getitelephony (telephonymanager telmgr) throws exception {<br/> method getitelephonymethod = telmgr. getclass (). getdeclaredmethod ("getitelephony"); <br/> getitelephonymethod. setaccessible (true); // The Private function can also use <br/> return (itelephony) getitelephonymethod. invoke (telmgr); <br/>}</P> <p> static public void printallinform (class clsshow) {<br/> try {<br/> // obtain all methods <br/> method [] hidemethod = clsshow. getdeclaredmethods (); <br/> int I = 0; <br/> for (; I <pidemethod. length; I ++) {<br/> log. E ("method name", hidemethod [I]. getname (); <br/>}< br/> // obtain all constants <br/> Field [] allfields = clsshow. getfields (); <br/> for (I = 0; I <allfields. length; I ++) {<br/> log. E ("field name", allfields [I]. getname (); <br/>}< br/>} catch (securityexception e) {<br/> // throw new runtimeexception (E. getmessage (); <br/> E. printstacktrace (); <br/>} catch (illegalargumentexception e) {<br/> // throw new runtimeexception (E. getmessage (); <br/> E. printstacktrace (); <br/>} catch (exception e) {<br/> // todo auto-generated Catch Block <br/> E. printstacktrace (); <br/>}< br/>
Testtelephony. Java is the main class. It uses phonestatelistener to listen on the call status and implement the above four telephone control functions. The source code is as follows:
Package COM. testtelephony; </P> <p> Import android. app. activity; <br/> Import android. OS. bundle; <br/> Import android. telephony. phonestatelistener; <br/> Import android. telephony. telephonymanager; <br/> Import android. util. log; <br/> Import android. view. view; <br/> Import android. widget. radiogroup; <br/> Import android. widget. togglebutton; </P> <p> public class testtelephony extends activity {<br/>/** called When the activity is first created. */<br/> radiogroup RG; // call operation ticket quota <br/> togglebutton tbtnradioswitch; // radio switch <br/> togglebutton tbtndataconn; // data connection switch <br/> telephonymanager telmgr; <br/> callstatelistener statelistner; <br/> int checkedid = 0; <br/> @ override <br/> Public void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); <br/> setcontentview (R. layout. main); </P> <p> telmg R = (telephonymanager) getsystemservice (telephony_service); <br/> telmgr. listen (New callstatelistener (), callstatelistener. listen_call_state); </P> <p> phoneutils. printallinform (telephonymanager. class); </P> <p> Rg = (radiogroup) findviewbyid (R. id. rgrpselect); <br/> RG. setoncheckedchangelistener (New checkevent (); <br/> tbtnradioswitch = (togglebutton) This. findviewbyid (R. id. tbtnradioswitch); <br/> tbtnradioswit Ch. setonclicklistener (New clickevent (); <br/> try {<br/> tbtnradioswitch. setchecked (phoneutils. getitelephony (telmgr ). isradioon (); <br/>}catch (exception e) {<br/> log. E ("error", E. getmessage (); <br/>}< br/> tbtndataconn = (togglebutton) This. findviewbyid (R. id. tbtndataconn); <br/> tbtndataconn. setonclicklistener (New clickevent (); <br/> try {<br/> tbtndataconn. setchecked (phoneutils. getitelephony (telmgr ). Isdataconnectivitypossible (); <br/>} catch (exception e) {<br/> log. E ("error", E. getmessage ()); <br/>}</P> <p>/** <br/> * Action on incoming call <br/> * @ author GV <br/> * <br/> */<br/> public class checkevent implements radiogroup. oncheckedchangelistener {</P> <p> @ override <br/> Public void oncheckedchanged (radiogroup group, int checkedid) {<br/> testtelephony. this. checkedid = checkedid; <br/>}</P> <p>/ ** <Br/> * radio and data connection switch <br/> * @ author GV <br/> */<br/> public class clickevent implements view. onclicklistener {</P> <p> @ override <br/> Public void onclick (view v) {<br/> If (V = tbtnradioswitch) {<br/> try {<br/> phoneutils. getitelephony (telmgr ). setradio (tbtnradioswitch. ischecked (); <br/>}catch (exception e) {<br/> log. E ("error", E. getmessage (); <br/>}< br/> else if (V = tbtndatac ONN) {<br/> try {<br/> If (tbtndataconn. ischecked () <br/> phoneutils. getitelephony (telmgr ). enabledataconnectivity (); <br/> else if (! Tbtndataconn. ischecked () <br/> phoneutils. getitelephony (telmgr ). disabledataconnectivity (); <br/>} catch (exception e) {<br/> log. E ("error", E. getmessage ()); <br/>}</P> <p>/** <br/> * monitor the phone status <br /> * @ author GV <br/> */<br/> public class callstatelistener extends phonestatelistener {<br/> @ override <br/> Public void oncallstatechanged (INT state, string incomingnumber) {<br/> If (State = telephonymanager. call_state_idle) // hang up <br/>{< br/> log. E ("idle", incomingnumber); <br/>}< br/> else if (State = telephonymanager. call_state_offhook) // Answer <br/>{< br/> log. E ("offhook", incomingnumber); <br/>}< br/> else if (State = telephonymanager. call_state_ringing) // call <br/>{< br/> If (testtelephony. this. checkedid = R. id. rbtnautoaccept) <br/>{< br/> try {<br/> // required <uses-Permission Android: Name = "android. permission. modify_phone_state "/> <br/> phoneutils. getitelephony (telmgr ). silenceringer (); // jingling <br/> phoneutils. getitelephony (telmgr ). answerringingcall (); // automatically receives </P> <p >}catch (exception e) {<br/> log. E ("error", E. getmessage (); <br/>}< br/> else if (testtelephony. this. checkedid = R. id. rbtnautoreject) <br/>{< br/> try {<br/> phoneutils. getitelephony (telmgr ). endcall (); // hang up <br/> phoneutils. getitelephony (telmgr ). cancelmissedcallsnotification (); // cancel not displayed <br/>}catch (exception e) {<br/> log. E ("error", E. getmessage (); <br/>}< br/> super. oncallstatechanged (State, incomingnumber); <br/>}< br/>}