收藏 分销(赏)

android实现自动接听及挂断电话.doc

上传人:仙人****88 文档编号:7852510 上传时间:2025-01-22 格式:DOC 页数:5 大小:42.50KB 下载积分:10 金币
下载 相关 举报
android实现自动接听及挂断电话.doc_第1页
第1页 / 共5页
android实现自动接听及挂断电话.doc_第2页
第2页 / 共5页


点击查看更多>>
资源描述
Android实现自动接听和挂掉电话源代码 Main.xml  添加权限   <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://   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="所有来电自动接听"></RadioButton>   <RadioButton android:layout_height="wrap_content"   android:layout_width="fill_parent" android:id="@+id/rbtnAutoReject"   android:text="所有来电自动挂断"></RadioButton>   </RadioGroup>   <ToggleButton android:layout_height="wrap_content"   android:layout_width="fill_parent" android:id="@+id/tbtnRadioSwitch"   android:textOn="Radio已经启动" android:textOff="Radio已经关闭"   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="允许数据连接"   android:textOff="禁止数据连接"></ToggleButton>   </LinearLayout> Phoneutils.class   PhoneUtils.java是手机功能类,从TelephonyManager中实例化ITelephony并返回,源码如下:   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 {   /**   * 从TelephonyManager中实例化ITelephony,并返回   */   static public ITelephony getITelephony(TelephonyManager telMgr) throws Exception {   Method getITelephonyMethod = telMgr.getClass().getDeclaredMethod("getITelephony");   getITelephonyMethod.setAccessible(true);//私有化函数也能使用   return (ITelephony)getITelephonyMethod.invoke(telMgr);   }   static public void printAllInform(Class clsShow) {   try {   // 取得所有方法   Method[] hideMethod = clsShow.getDeclaredMethods();   int i = 0;   for (; i < hideMethod.length; i++) {   Log.e("method name", hideMethod.getName());   }   // 取得所有常量   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是主类,使用PhoneStateListener监听通话状态,以及实现上述4种电话控制功能,源码如下:   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;//来电操作单选框   ToggleButton tbtnRadioSwitch;//Radio开关   ToggleButton tbtnDataConn;//数据连接的开关   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);   100   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());   }   }   /**   * 来电时的操作   * @author GV   *   */   public class CheckEvent implements RadioGroup.OnCheckedChangeListener{   @Override   public void onCheckedChanged(RadioGroup group, int checkedId) {   testTelephony.this.checkedId=checkedId;   }   }   /**   * Radio和数据连接的开关   * @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());   }   }   }   }   /**   * 监视电话状态   * @author GV   *   */   public class CallStateListener extends PhoneStateListener {   @Override   public void onCallStateChanged(int state, String incomingNumber) {   if(state==TelephonyManager.CALL_STATE_IDLE)//挂断   {   Log.e("IDLE",incomingNumber);   }   else if(state==TelephonyManager.CALL_STATE_OFFHOOK)//接听   {   Log.e("OFFHOOK",incomingNumber);   }   else if(state==TelephonyManager.CALL_STATE_RINGING)//来电   {   if(testTelephony.this.checkedId==R.id.rbtnAutoAccept)   {   try {   //需要<uses-permission android:name="android.permission.MODIFY_PHONE_STATE" />   PhoneUtils.getITelephony(telMgr).silenceRinger();//静铃   PhoneUtils.getITelephony(telMgr).answerRingingCall();//自动接听   } catch (Exception e) {   Log.e("error",e.getMessage());   }   }   else if(testTelephony.this.checkedId==R.id.rbtnAutoReject)   {   try {   PhoneUtils.getITelephony(telMgr).endCall();//挂断   PhoneUtils.getITelephony(telMgr).cancelMissedCallsNotification();//取消未接显示   } catch (Exception e) {   Log.e("error",e.getMessage());   }   }   }   super.onCallStateChanged(state, incomingNumber);   }   }   }
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传

当前位置:首页 > 教育专区 > 小学其他

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2025 宁波自信网络信息技术有限公司  版权所有

客服电话:4009-655-100  投诉/维权电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服