Add Permissions<uses-permission android:name= "Android.permission.CALL_PHONE"/><uses-permission android:name= " Android.permission.MODIFY_PHONE_STATE "/>Main.xml<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"androidrientation= "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= "All incoming calls are answered automatically" ></RadioButton> <radiobutton android:layout_height= "Wrap_content"Android:layout_width= "Fill_parent" android:id= "@+id/rbtnautoreject"Android:text= "All incoming calls automatically hang up" ></RadioButton> </RadioGroup> <togglebutton android:layout_height= "Wrap_content"Android:layout_width= "Fill_parent" android:id= "@+id/tbtnradioswitch"Android:texton= "Radio has started" android:textoff= "Radio has been turned off"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, from Telephonymanager to instantiate Itelephony and return, the source code is as follows: PackageCom.testtelephony; ImportJava.lang.reflect.Field; ImportJava.lang.reflect.Method; ImportCom.android.internal.telephony.ITelephony; ImportAndroid.telephony.TelephonyManager; ImportAndroid.util.Log; Public classPhoneutils {/*** Instantiate itelephony from Telephonymanager and return*/ Static PublicItelephony getitelephony (Telephonymanager telmgr)throwsException {Method Getitelephonymethod= Telmgr.getclass (). Getdeclaredmethod ("Getitelephony"); Getitelephonymethod.setaccessible (true);//The privatization function can also be used return(Itelephony) Getitelephonymethod.invoke (telmgr); } Static Public voidprintallinform (Class clsshow) {Try { //get all the methodsmethod[] Hidemethod =Clsshow.getdeclaredmethods (); inti = 0; for(; i < hidemethod.length; i++) {LOG.E ("Method Name", Hidemethod.getname ()); } //Get all Constantsfield[] AllFields =Clsshow.getfields (); for(i = 0; i < allfields.length; i++) {LOG.E ("Field name", Allfields.getname ()); } } Catch(SecurityException e) {//throw new RuntimeException (E.getmessage ()); E.printstacktrace (); } Catch(IllegalArgumentException e) {//throw new RuntimeException (E.getmessage ()); E.printstacktrace (); } Catch(Exception e) {//TODO auto-generated Catch blockE.printstacktrace (); }}} Testtelephony.java is the main class, the use of Phonestatelistener monitoring call status, as well as the implementation of the above 4 telephone control functions, the source code is as follows: PackageCom.testtelephony; Importandroid.app.Activity; ImportAndroid.os.Bundle; ImportAndroid.telephony.PhoneStateListener; ImportAndroid.telephony.TelephonyManager; ImportAndroid.util.Log; ImportAndroid.view.View; ImportAndroid.widget.RadioGroup; ImportAndroid.widget.ToggleButton; Public classTesttelephonyextendsActivity {/**Called when the activity is first created.*/Radiogroup RG;//Call action Radio boxToggleButton Tbtnradioswitch;//Radio SwitchToggleButton Tbtndataconn;//switches for data connectionsTelephonymanager telmgr; Callstatelistener Statelistner; intCheckedid=0; @Override Public voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.main); Telmgr=(Telephonymanager) Getsystemservice (Telephony_service); Telmgr.listen (NewCallstatelistener (), callstatelistener.listen_call_state); Phoneutils.printallinform (Telephonymanager.class); RG=(Radiogroup) Findviewbyid (r.id.rgrpselect); Rg.setoncheckedchangelistener (Newcheckevent ()); Tbtnradioswitch= (ToggleButton) This. Findviewbyid (R.id.tbtnradioswitch); Tbtnradioswitch.setonclicklistener (Newclickevent ()); Try{tbtnradioswitch.setchecked (Phoneutils.getitelephony (telmgr). Isradioon ()); } Catch(Exception e) {LOG.E ("Error", E.getmessage ()); } tbtndataconn= (ToggleButton) This. Findviewbyid (R.id.tbtndataconn); Tbtndataconn.setonclicklistener (Newclickevent ()); Try{tbtndataconn.setchecked (Phoneutils.getitelephony (telmgr). isdataconnectivitypossible ()); } Catch(Exception e) {LOG.E ("Error", E.getmessage ()); } } /*** operation on incoming call *@authorGV **/ Public classCheckeventImplementsradiogroup.oncheckedchangelistener{@Override Public voidOnCheckedChanged (Radiogroup Group,intCheckedid) {Testtelephony. This. checkedid=Checkedid; } } /*** Radio and data connection switches *@authorGV **/ Public classClickeventImplementsview.onclicklistener{@Override Public voidOnClick (View v) {if(v = =Tbtnradioswitch) { Try{phoneutils.getitelephony (telmgr). Setradio (tbtnradioswitch.ischecked ()); } Catch(Exception e) {LOG.E ("Error", E.getmessage ()); } } Else if(v==tbtndataconn) { Try { if(tbtndataconn.ischecked ()) Phoneutils.getitelephony (telmgr). Enabledataconnectivity (); Else if(!tbtndataconn.ischecked ()) Phoneutils.getitelephony (telmgr). Disabledataconnectivity (); } Catch(Exception e) {LOG.E ("Error", E.getmessage ()); } } } } /*** Monitor phone status *@authorGV **/ Public classCallstatelistenerextendsPhonestatelistener {@Override Public voidOncallstatechanged (intState , String Incomingnumber) { if(State==telephonymanager.call_state_idle)//Hanging off{LOG.E ("IDLE", Incomingnumber); } Else if(State==telephonymanager.call_state_offhook)//Answer{LOG.E ("Offhook", Incomingnumber); } Else if(state==telephonymanager.call_state_ringing)//Call { if(Testtelephony. This. checkedid==r.id.rbtnautoaccept) {Try { //requires <uses-permission android:name= "Android.permission.MODIFY_PHONE_STATE"/>Phoneutils.getitelephony (telmgr). Silenceringer ();//Silent BellPhoneutils.getitelephony (telmgr). Answerringingcall ();//Automatic Answer } Catch(Exception e) {LOG.E ("Error", E.getmessage ()); } } Else if(Testtelephony. This. checkedid==r.id.rbtnautoreject) {Try{phoneutils.getitelephony (telmgr). Endcall ();//Hanging offPhoneutils.getitelephony (telmgr). Cancelmissedcallsnotification ();//Cancel missed display}Catch(Exception e) {LOG.E ("Error", E.getmessage ()); } } } Super. oncallstatechanged (State, Incomingnumber); } } }
Android enables automatic answer and hang-up calls