android自动接听和挂断电话

来源:互联网 发布:网络机顶盒看电影软件 编辑:程序博客网 时间:2024/04/29 17:18
实现android自动接听和挂断电话功能。代码如下:
  1.  添加权限
  2.   <uses-permission android:name="android.permission.CALL_PHONE"/>
  3.   <uses-permission android:name="android.permission.MODIFY_PHONE_STATE"/>
  4.   main.xml
  5.   <?xml version="1.0" encoding="utf-8"?>
  6.   <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  7.   androidrientation="vertical" android:layout_width="fill_parent"
  8.   android:layout_height="fill_parent">
  9.   <RadioGroup android:layout_height="wrap_content"
  10.   android:layout_width="fill_parent" android:id="@+id/rGrpSelect">
  11.   <RadioButton android:layout_height="wrap_content"
  12.   android:layout_width="fill_parent" android:id="@+id/rbtnAutoAccept"
  13.   android:text="所有来电自动接听"></RadioButton>
  14.   <RadioButton android:layout_height="wrap_content"
  15.   android:layout_width="fill_parent" android:id="@+id/rbtnAutoReject"
  16.   android:text="所有来电自动挂断"></RadioButton>
  17.   </RadioGroup>
  18.   <ToggleButton android:layout_height="wrap_content"
  19.   android:layout_width="fill_parent" android:id="@+id/tbtnRadioSwitch"
  20.   android:textOn="Radio已经启动" android:textOff="Radio已经关闭"
  21.   android:textSize="24dip" android:textStyle="normal"></ToggleButton>
  22.   <ToggleButton android:layout_height="wrap_content"
  23.   android:layout_width="fill_parent" android:id="@+id/tbtnDataConn"
  24.   android:textSize="24dip" android:textStyle="normal" android:textOn="允许数据连接"
  25.   android:textOff="禁止数据连接"></ToggleButton>
  26.   </LinearLayout>
复制代码

  1.   PhoneUtils.java是手机功能类,从TelephonyManager中实例化ITelephony并返回,源码如下:
  2.   package com.testTelephony;
  3.   import java.lang.reflect.Field;
  4.   import java.lang.reflect.Method;
  5.   import com.android.internal.telephony.ITelephony;
  6.   import android.telephony.TelephonyManager;
  7.   import android.util.Log;
  8.   public class PhoneUtils {
  9.   /**
  10.   * 从TelephonyManager中实例化ITelephony,并返回
  11.   */
  12.   static public ITelephony getITelephony(TelephonyManager telMgr) throws Exception {
  13.   Method getITelephonyMethod = telMgr.getClass().getDeclaredMethod("getITelephony");
  14.   getITelephonyMethod.setAccessible(true);//私有化函数也能使用
  15.   return (ITelephony)getITelephonyMethod.invoke(telMgr);
  16.   }
  17.   static public void printAllInform(Class clsShow) {
  18.   try {
  19.   // 取得所有方法
  20.   Method[] hideMethod = clsShow.getDeclaredMethods();
  21.   int i = 0;
  22.   for (; i < hideMethod.length; i++) {
  23.   Log.e("method name", hideMethod.getName());
  24.   }
  25.   // 取得所有常量
  26.   Field[] allFields = clsShow.getFields();
  27.   for (i = 0; i < allFields.length; i++) {
  28.   Log.e("Field name", allFields.getName());
  29.   }
  30.   } catch (SecurityException e) {
  31.   // throw new RuntimeException(e.getMessage());
  32.   e.printStackTrace();
  33.   } catch (IllegalArgumentException e) {
  34.   // throw new RuntimeException(e.getMessage());
  35.   e.printStackTrace();
  36.   } catch (Exception e) {
  37.   // TODO Auto-generated catch block
  38.   e.printStackTrace();
  39.   }
  40.   }
  41.   }
复制代码
  1.   testTelephony.java是主类,使用PhoneStateListener监听通话状态,以及实现上述4种电话控制功能,源码如下:
  2.   package com.testTelephony;
  3.   import android.app.Activity;
  4.   import android.os.Bundle;
  5.   import android.telephony.PhoneStateListener;
  6.   import android.telephony.TelephonyManager;
  7.   import android.util.Log;
  8.   import android.view.View;
  9.   import android.widget.RadioGroup;
  10.   import android.widget.ToggleButton;
  11.   public class testTelephony extends Activity {
  12.   /** Called when the activity is first created. */
  13.   RadioGroup rg;//来电操作单选框
  14.   ToggleButton tbtnRadioSwitch;//Radio开关
  15.   ToggleButton tbtnDataConn;//数据连接的开关
  16.   TelephonyManager telMgr;
  17.   CallStateListener stateListner;
  18.   int checkedId=0;
  19.   @Override
  20.   public void onCreate(Bundle savedInstanceState) {
  21.   super.onCreate(savedInstanceState);
  22.   setContentView(R.layout.main);
  23.   telMgr= (TelephonyManager)getSystemService(TELEPHONY_SERVICE);
  24.   100
  25.   telMgr.listen(new CallStateListener(), CallStateListener.LISTEN_CALL_STATE);
  26.   PhoneUtils.printAllInform(TelephonyManager.class);
  27.   rg = (RadioGroup)findViewById(R.id.rGrpSelect);
  28.   rg.setOnCheckedChangeListener(new CheckEvent());
  29.   tbtnRadioSwitch=(ToggleButton)this.findViewById(R.id.tbtnRadioSwitch);
  30.   tbtnRadioSwitch.setOnClickListener(new ClickEvent());
  31.   try {
  32.   tbtnRadioSwitch.setChecked(PhoneUtils.getITelephony(telMgr).isRadioOn());
  33.   }  catch (Exception e) {
  34.   Log.e("error",e.getMessage());
  35.   }
  36.   tbtnDataConn=(ToggleButton)this.findViewById(R.id.tbtnDataConn);
  37.   tbtnDataConn.setOnClickListener(new ClickEvent());
  38.   try {
  39.   tbtnDataConn.setChecked(PhoneUtils.getITelephony(telMgr).isDataConnectivityPossible());
  40.   }  catch (Exception e) {
  41.   Log.e("error",e.getMessage());
  42.   }
  43.   }
  44.   /**
  45.   * 来电时的操作
  46.   * @author GV
  47.   *
  48.   */
  49.   public class CheckEvent implements RadioGroup.OnCheckedChangeListener{
  50.   @Override
  51.   public void onCheckedChanged(RadioGroup group, int checkedId) {
  52.   testTelephony.this.checkedId=checkedId;
  53.   }
  54.   }
  55.   /**
  56.   * Radio和数据连接的开关
  57.   * @author GV
  58.   *
  59.   */
  60.   public class ClickEvent implements View.OnClickListener{
  61.   @Override
  62.   public void onClick(View v) {
  63.   if (v == tbtnRadioSwitch) {
  64.   try {
  65.   PhoneUtils.getITelephony(telMgr).setRadio(tbtnRadioSwitch.isChecked());
  66.   } catch (Exception e) {
  67.   Log.e("error", e.getMessage());
  68.   }
  69.   }
  70.   else if(v==tbtnDataConn){
  71.   try {
  72.   if(tbtnDataConn.isChecked())
  73.   PhoneUtils.getITelephony(telMgr).enableDataConnectivity();
  74.   else if(!tbtnDataConn.isChecked())
  75.   PhoneUtils.getITelephony(telMgr).disableDataConnectivity();
  76.   } catch (Exception e) {
  77.   Log.e("error", e.getMessage());
  78.   }
  79.   }
  80.   }
  81.   }
  82.   /**
  83.   * 监视电话状态
  84.   * @author GV
  85.   *
  86.   */
  87.   public class CallStateListener extends PhoneStateListener {
  88.   @Override
  89.   public void onCallStateChanged(int state, String incomingNumber) {
  90.   if(state==TelephonyManager.CALL_STATE_IDLE)//挂断
  91.   {
  92.   Log.e("IDLE",incomingNumber);
  93.   }
  94.   else if(state==TelephonyManager.CALL_STATE_OFFHOOK)//接听
  95.   {
  96.   Log.e("OFFHOOK",incomingNumber);
  97.   }
  98.   else if(state==TelephonyManager.CALL_STATE_RINGING)//来电
  99.   {
  100.   if(testTelephony.this.checkedId==R.id.rbtnAutoAccept)
  101.   {
  102.   try {
  103.   //需要<uses-permission android:name="android.permission.MODIFY_PHONE_STATE" />
  104.   PhoneUtils.getITelephony(telMgr).silenceRinger();//静铃
  105.   PhoneUtils.getITelephony(telMgr).answerRingingCall();//自动接听
  106.   } catch (Exception e) {
  107.   Log.e("error",e.getMessage());
  108.   }
  109.   }
  110.   else if(testTelephony.this.checkedId==R.id.rbtnAutoReject)
  111.   {
  112.   try {
  113.   PhoneUtils.getITelephony(telMgr).endCall();//挂断
  114.   PhoneUtils.getITelephony(telMgr).cancelMissedCallsNotification();//取消未接显示
  115.   } catch (Exception e) {
  116.   Log.e("error",e.getMessage());
  117.   }
  118.   }
  119.   }
  120.   super.onCallStateChanged(state, incomingNumber);
  121.   }
  122.   }
  123.   }
复制代码
原创粉丝点击