How does Android implement automatic answer and hang-up phone functions

Source: Internet
Author: User

Original address: Http://zhidao.baidu.com/link?url= Lvq9ui0shuwtkoroolc21aaawoujdznql4txfwf5bqouqculml3ayzg5bby29cuqqvcyvay6dlrypmuhyoingkojhn1t37szcmdrsnyiyby

Android Automatic answer and hang-up phone implementation principle : http://blog.csdn.net/chenda_lin/article/details/41346493

--telephonymanager (Phone manager) of system services provided by Android: http://www.2cto.com/kf/201410/347844.html

Android for automatic call answering and auto-hang the method:
The first step: Prepare the System packages and Aidl files required by the application environment.
(1) Create a package in the app: Android.telephony
\framework\telephony\java\android\ the Android system under the frame The Neighboringcellinfo.aidl file in the telephony directory is copied to the package created above (android.telephony);
(2) Create a package in the app: Com.android.internal.telephony
\framework\telephony\java\com\android\internal\ the Android system under the frame The Itelephony.aidl file in the telephony directory is copied to the package created above (com.android.internal.telephony).
Step Two: Create a method to get Itelephony
Phoneutils.java
Package Com.zhouzijing.android.demo;

Import Java.lang.reflect.Method;
Import Com.android.internal.telephony.ITelephony;
Import Android.telephony.TelephonyManager;

public class Phoneutils {
/**
* Itelephony instances of the system are obtained according to the incoming Telephonymanager.
* @param telephony
* Itelephony Example of @return system
* @throws Exception
*/
public static Itelephony Getitelephony (Telephonymanager telephony) throws Exception {
Method Getitelephonymethod = Telephony.getclass (). Getdeclaredmethod ("Getitelephony");
Getitelephonymethod.setaccessible (TRUE);//The privatization function can also be used
Return (Itelephony) Getitelephonymethod.invoke (telephony);
}
}

Step three: Create a phone broadcast interceptor
Myphonebroadcastreceiver.java
Package Com.zhouzijing.android.demo;

Import Com.android.internal.telephony.ITelephony;
Import Android.content.BroadcastReceiver;
Import Android.content.Context;
Import android.content.Intent;
Import Android.telephony.TelephonyManager;
Import Android.util.Log;

public class Myphonebroadcastreceiver extends Broadcastreceiver {

Private final static String TAG = Myphone.tag;

@Override
public void OnReceive (context context, Intent Intent) {
String action = Intent.getaction ();
LOG.I (TAG, "[Broadcast]" +action);

Incoming Calls
if (Action.equals (myphone.b_phone_state)) {
LOG.I (TAG, "[broadcast]phone_state");
Doreceivephone (context,intent);
}
}

/**
* Processing of telephone broadcasts.
* @param context
* @param Intent
*/
public void Doreceivephone (context context, Intent Intent) {
String PhoneNumber = Intent.getstringextra (
Telephonymanager.extra_incoming_number);
Telephonymanager telephony = (Telephonymanager) context.getsystemservice (
Context.telephony_service);
int state = Telephony.getcallstate ();

Switch (state) {
Case telephonymanager.call_state_ringing:
LOG.I (TAG, "[broadcast] waiting to pick up the phone =" +phonenumber);
try {
Itelephony itelephony = phoneutils.getitelephony (telephony);
Itelephony.answerringingcall ();//auto-connect phone
Itelephony.endcall ();//auto-hang up the phone
} catch (Exception e) {
LOG.E (TAG, "[broadcast]exception=" +e.getmessage (), E);
}
Break
Case Telephonymanager.call_state_idle:
LOG.I (TAG, "[broadcast] phone hangs up =" +phonenumber);
Break
Case Telephonymanager.call_state_offhook:
LOG.I (TAG, "[broadcast] in call =" +phonenumber);
Break
}
}

}

Fourth: Registration of telephone broadcast interceptors
Myphone.java
Package Com.zhouzijing.android.demo;

Import android.app.Activity;
Import Android.content.IntentFilter;
Import Android.os.Bundle;
Import Android.telephony.TelephonyManager;
Import Android.util.Log;
Import Android.view.View;

public class Myphone extends Activity {
Public final static String TAG = "Myphone";

Public final static String b_phone_state = telephonymanager.action_phone_state_changed;

Private Myphonebroadcastreceiver Mbroadcastreceiver;

@Override
public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.my_phone);
}

Button 1-Registering the broadcast
public void Registerthis (View v) {
LOG.I (TAG, "registerthis");
Mbroadcastreceiver = new Myphonebroadcastreceiver ();
Intentfilter intentfilter = new Intentfilter ();
Intentfilter.addaction (b_phone_state);
Intentfilter.setpriority (Integer.max_value);
Registerreceiver (Mbroadcastreceiver, Intentfilter);
}

Button 2-Cancel broadcast
public void Unregisterthis (View v) {
LOG.I (TAG, "unregisterthis");
Unregisterreceiver (Mbroadcastreceiver);
}

}

5th Step: Configure Permissions in Androidmanifest.xml
<uses-permission android:name= "Android.permission.READ_PHONE_STATE"/>
<uses-permission android:name= "Android.permission.MODIFY_PHONE_STATE"/>
<uses-permission android:name= "Android.permission.CALL_PHONE"/>

which
Itelephony.answerringingcall ();//auto-connect phone
Must have permission Android.permission.MODIFY_PHONE_STATE
Itelephony.endcall ();//auto-hang up the phone
Must have permission to Android.permission.CALL_PHONE.

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 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:
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);//The privatization function can also be used
Return (Itelephony) Getitelephonymethod.invoke (telmgr);
}

static public void Printallinform (Class clsshow) {
try {
Get all the methods
method[] Hidemethod = Clsshow.getdeclaredmethods ();
int i = 0;
for (; i < hidemethod.length; i++) {
LOG.E ("Method name", Hidemethod.getname ());
}
Get all constants
field[] 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 block
E.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:
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;

public class Testtelephony extends Activity {
/** called when the activity is first created. */
Radiogroup rg;//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 ());
}
}

/**
* Operation at the time of call
* @author GV
*
*/
public class Checkevent implements radiogroup.oncheckedchangelistener{

@Override
public void OnCheckedChanged (radiogroup group, int checkedid) {
Testtelephony.this.checkedid=checkedid;
}
}

/**
* Switches for radio and data connections
* @author GV
*
*/
public class Clickevent implements view.onclicklistener{

@Override
public 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). 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 Phonestatelistener {
@Override
public void oncallstatechanged (int state, String incomingnumber) {
if (state==telephonymanager.call_state_idle)//Hang 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 ();//Static Bell
Phoneutils.getitelephony (telmgr). Answerringingcall ();//Auto Answer

} catch (Exception e) {
LOG.E ("Error", E.getmessage ());
}
}
else if (testtelephony.this.checkedid==r.id.rbtnautoreject)
{
try {
Phoneutils.getitelephony (telmgr). Endcall ();//Hang Up
Phoneutils.getitelephony (telmgr). Cancelmissedcallsnotification ()//Cancel missed display
} catch (Exception e) {
LOG.E ("Error", E.getmessage ());
}
}
}
Super.oncallstatechanged (state, Incomingnumber);
}
}
}

How does Android implement automatic answer and hang-up phone functions

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.