Android to improve the Telephonymanager function Quest _android

Source: Internet
Author: User
Tags getmessage reflection

The previous article describes how to use the Java reflection mechanism to invoke the Bluetooth hidden API, this article continues to practice the Java reflection Mechanism, explore telephonymanager in the framework included in the SDK hidden in several functions.

Let's take a look at the effect diagram that this program runs as follows:

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:

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

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/ Rgrpselect "> <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= " Wrap_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_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 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 the Telephonymanager and returns the */static public itelephony Getitelephony (Telephon
 Ymanager telmgr) throws Exception {method Getitelephonymethod = Telmgr.getclass (). Getdeclaredmethod ("Getitelephony");
 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 = Clsshow.getdec 
     Laredmethods (); 
     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 action Radio 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 ()); /** * The operation * @author GV */public class Checkevent implements radiogroup.oncheckedchangelistener{@Overri
 De public void oncheckedchanged (radiogroup group, int checkedid) {testtelephony.this.checkedid=checkedid; }/** * Radio and Data connection switch * @author GV * */public class Clickevent implements view.onclicklistener{@Override Pub LIC void OnClick (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). Enabledataco
   Nnectivity ();
  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 Phonestatelistener {@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.getitelephony (telmgr). Answerringingcall ();
   Automatic answer to catch (Exception e) {log.e ("error", E.getmessage ()); } else if (Testtelephony.this.checkedid==r.id.rbtnautoreject) {try {phoneutils.getitelephony (telmgr). EndCa ll ()//Hang Up Phoneutils.getitelephony (telmgr). Cancelmissedcallsnotification ()//Cancel not receiving show} catch (Exception e) {LOG.E 
   ("Error", E.getmessage ());
 }} super.oncallstatechanged (state, Incomingnumber); }
 }
}

Interested readers can test the example code of this article, hoping to help you with the development of Android projects.

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.