Android Telephonymanager Detailed and implementation code _android

Source: Internet
Author: User
Tags getmessage reflection

The reflection mechanism of Java, explore the Telephonymanager in the framework contains but in the SDK hidden several functions. Let's take a look at the effect of this program:

This program demonstrates the following features:

1. All incoming calls are automatically answered;

2. All incoming calls are automatically hung up;

3. Open/Close radio;

4. Turn on/Off data connections (WAP or net connections).

Calling Telephonymanager's Hidden API is the first reference to the framework's/base/telephony/java/com/android/internal/telephony/ Itelephony.aidl, and then realize a itelephony.aidl by itself, and finally instantiate the custom itelephony through the reflection mechanism in Telephonymanager, then the function in Itelephony can be invoked after instantiation.

This program needs to add the following two lines of code in Androidmanifest.xml to get permission:

Xml/html Code:

<uses-permission android:name= "Android.permission.CALL_PHONE"/> 

Main.xml source code is as follows:

<?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/rGrpS Elect "> <radiobutton android:layout_height=" wrap_content "android:layout_width=" Fill_parent "android:id=" "@+id/rbtnautoaccept" android:text= "all incoming calls automatically answered" ></RadioButton> <radiobutton android:layout_height= "W Rap_content "android:layout_width=" fill_parent "android:id=" @+id/rbtnautoreject "android:text=" all calls automatically hang up > </RadioButton> </RadioGroup> <togglebutton android:layout_height= "Wrap_content" Android:layout_w Idth= "Fill_parent" android:id= "@+id/tbtnradioswitch android:texton=" Radio has started "android:textoff=" Radio has been closed "Andr Oid:textsize= "24dip" Android:textstyle= "normal" ></ToggleButton> <togglebutton android:layout_height= "wrap_content" android:layout_width= "fi Ll_parent "android:id=" @+id/tbtndataconn "android:textsize=" 24dip "android:textstyle=" normal "android:texton="  Allow data Connections "android:textoff=" Prohibit data connections ></ToggleButton> </LinearLayout>

 phoneutils.java is a mobile phone function class, from Telephonymanager to instantiate Itelephony and return, the source code is as follows:

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; The public class Phoneutils {/** * instantiates itelephony from Telephonymanager and returns the */static public itelephony Getitelephon Y (Telephonymanager telmgr) throws Exception {method Getitelephonymethod = Telmgr.getclass (). Getdeclaredmethod ("GetIT 
    Elephony "); 
  Getitelephonymethod.setaccessible (TRUE);//privatisation function can also use return (Itelephony) Getitelephonymethod.invoke (telmgr); static public void Printallinform (Class clsshow) {try {//Get all methods method[] Hidemethod = CL  
      Sshow.getdeclaredmethods ();  
      int i = 0;  
      for (; i < hidemethod.length; i++) {log.e (' method name ', Hidemethod[i].getname ());  
      }//Get all constants field[] AllFields = Clsshow.getfields (); for (i = 0; i < allfields.length; i++) { 
        LOG.E ("Field name", Allfields[i].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 block E.printstacktrace ();  }  
  }  
}

Testtelephony.java is the main class, using Phonestatelistener to monitor the status of the call, and to achieve the above 4 kinds of phone control functions, the source code is as follows:

Package com.testtelephony; 
Import android.app.Activity; 
Import Android.os.Bundle; 
Import Android.telephony.PhoneStateListener; 
Import Android.telephony.TelephonyManager; 
Import Android.util.Log; 
Import Android.view.View; 
Import Android.widget.RadioGroup; 
 
Import Android.widget.ToggleButton; The public class Testtelephony extends activity {/** called the "when the" is the "the" is a-created-radiogroup call operation 
  Single-selection box ToggleButton tbtnradioswitch;//radio switch ToggleButton tbtndataconn;//data connection switch Telephonymanager telMgr; 
  Callstatelistener Statelistner; 
  int checkedid=0; 
    @Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); 
     
    Setcontentview (R.layout.main); 
    Telmgr= (Telephonymanager) Getsystemservice (Telephony_service); 
     
    Telmgr.listen (New Callstatelistener (), callstatelistener.listen_call_state); 
     
    Phoneutils.printallinform (Telephonymanager.class); RG = (Radiogroup) Findviewbyid (r.iD.rgrpselect); 
    Rg.setoncheckedchangelistener (New Checkevent ()); 
    Tbtnradioswitch= (ToggleButton) This.findviewbyid (R.id.tbtnradioswitch); 
    Tbtnradioswitch.setonclicklistener (New Clickevent ()); 
    try {tbtnradioswitch.setchecked (Phoneutils.getitelephony (telmgr). Isradioon ()); 
    catch (Exception e) {log.e ("error", E.getmessage ()); 
    } tbtndataconn= (ToggleButton) This.findviewbyid (r.id.tbtndataconn); 
    Tbtndataconn.setonclicklistener (New Clickevent ()); 
    try {tbtndataconn.setchecked (Phoneutils.getitelephony (telmgr). isdataconnectivitypossible ()); 
    catch (Exception e) {log.e ("error", E.getmessage ()); /** * when calling * @author GV */public class Checkevent implements Radiogroup.oncheckedcha ngelistener{@Override public void oncheckedchanged (radiogroup group, int checkedid) {testtelephony.th 
    Is.checkedid=checkedid; }/** * Radio and Data connection switch * @author GV * */public class Clickevent implements view.onclicklistener{@Override public void OnClick View V) {if (v = = Tbtnradioswitch) {try {phoneutils.getitelephony (telmgr). Setradio (TBTNRADIOSWITC 
        H.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 * @author GV * */public class Callstatelistener extends P Honestatelistener {@Override public void oncallstatechanged (int state, String incomingnumber) {if (state ==telephonymanager.call_state_idle)//Hang Up {log.e ("IDLE", Incomingnumber); 
      else if (State==telephonymanager.call_state_offhook)//To answer the {LOG.E ("Offhook", Incomingnumber); else if (state==telephonymanager.call_state_ringing)//Call {if (testtelephony.this.checkedid==r.id . rbtnautoaccept) {try {//need <uses-permission android:name= "android.permission.MODIFY_ Phone_state "/> Phoneutils.getitelephony (telmgr). Silenceringer (); Silent Bell Phoneutils.getitelepho NY (TELMGR). Answerringingcall ();//Auto answer to catch (Exception e) {log.e ("error", E.getmes 
          Sage ()); 
            } else if (Testtelephony.this.checkedid==r.id.rbtnautoreject) {try { Phoneutils.getitelephony (telmgr). Endcall ()//Hang Up Phoneutils.getitelephony (telmgr). Cancelmissedcallsnotificat Ion ()//Cancel not receiving show} catch (Exception e) {LOG.E("Error", E.getmessage ()); 
    }} super.oncallstatechanged (state, Incomingnumber);  } 
  } 
}

Above is the Android Telephonymanager introduction and simple example, I hope to help the students in need, thank you for your support for this site!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.