免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1525 | 回复: 0

[Android] Android 实现自动接听和挂断电话功能 [复制链接]

论坛徽章:
1
操作系统版块每日发帖之星
日期:2015-06-05 22:20:00
发表于 2015-06-04 09:36 |显示全部楼层
这是转载。感谢作者,如果对你有用你学习一下,如果没用请不要喷
[Java]代码
  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>  


  27. PhoneUtils.java是手机功能类,从TelephonyManager中实例化ITelephony并返回,源码如下:
  28. package com.testTelephony;  
  29.    
  30. import java.lang.reflect.Field;  
  31. import java.lang.reflect.Method;  
  32. import com.android.internal.telephony.ITelephony;  
  33. import android.telephony.TelephonyManager;  
  34. import android.util.Log;  
  35.    
  36. public class PhoneUtils {  
  37.     /**
  38.      * 从TelephonyManager中实例化ITelephony,并返回
  39.      */
  40.     static public ITelephony getITelephony(TelephonyManager telMgr) throws Exception {  
  41.         Method getITelephonyMethod = telMgr.getClass().getDeclaredMethod("getITelephony");  
  42.         getITelephonyMethod.setAccessible(true);//私有化函数也能使用  
  43.         return (ITelephony)getITelephonyMethod.invoke(telMgr);  
  44.     }  
  45.       
  46.     static public void printAllInform(Class clsShow) {   
  47.         try {   
  48.             // 取得所有方法   
  49.             Method[] hideMethod = clsShow.getDeclaredMethods();   
  50.             int i = 0;   
  51.             for (; i < hideMethod.length; i++) {   
  52.                 Log.e("method name", hideMethod.getName());   
  53.             }   
  54.             // 取得所有常量   
  55.             Field[] allFields = clsShow.getFields();   
  56.             for (i = 0; i < allFields.length; i++) {   
  57.                 Log.e("Field name", allFields.getName());   
  58.             }   
  59.         } catch (SecurityException e) {   
  60.             // throw new RuntimeException(e.getMessage());   
  61.             e.printStackTrace();   
  62.         } catch (IllegalArgumentException e) {   
  63.             // throw new RuntimeException(e.getMessage());   
  64.             e.printStackTrace();   
  65.         } catch (Exception e) {   
  66.             // TODO Auto-generated catch block   
  67.             e.printStackTrace();   
  68.         }   
  69.     }   
  70. }  
  71. testTelephony.java是主类,使用PhoneStateListener监听通话状态,以及实现上述4种电话控制功能,源码如下:
  72. package com.testTelephony;  
  73.    
  74. import android.app.Activity;  
  75. import android.os.Bundle;  
  76. import android.telephony.PhoneStateListener;  
  77. import android.telephony.TelephonyManager;  
  78. import android.util.Log;  
  79. import android.view.View;  
  80. import android.widget.RadioGroup;  
  81. import android.widget.ToggleButton;  
  82.    
  83. public class testTelephony extends Activity {  
  84.     /** Called when the activity is first created. */
  85.     RadioGroup rg;//来电操作单选框  
  86.     ToggleButton tbtnRadioSwitch;//Radio开关  
  87.     ToggleButton tbtnDataConn;//数据连接的开关  
  88.     TelephonyManager telMgr;  
  89.     CallStateListener stateListner;  
  90.     int checkedId=0;  
  91.     @Override
  92.     public void onCreate(Bundle savedInstanceState) {  
  93.         super.onCreate(savedInstanceState);  
  94.         setContentView(R.layout.main);  
  95.            
  96.         telMgr= (TelephonyManager)getSystemService(TELEPHONY_SERVICE);  
  97.         telMgr.listen(new CallStateListener(), CallStateListener.LISTEN_CALL_STATE);  
  98.            
  99.         PhoneUtils.printAllInform(TelephonyManager.class);  
  100.            
  101.         rg = (RadioGroup)findViewById(R.id.rGrpSelect);  
  102.         rg.setOnCheckedChangeListener(new CheckEvent());  
  103.         tbtnRadioSwitch=(ToggleButton)this.findViewById(R.id.tbtnRadioSwitch);  
  104.         tbtnRadioSwitch.setOnClickListener(new ClickEvent());  
  105.         try {  
  106.             tbtnRadioSwitch.setChecked(PhoneUtils.getITelephony(telMgr).isRadioOn());  
  107.         }  catch (Exception e) {  
  108.             Log.e("error",e.getMessage());  
  109.         }  
  110.         tbtnDataConn=(ToggleButton)this.findViewById(R.id.tbtnDataConn);  
  111.         tbtnDataConn.setOnClickListener(new ClickEvent());  
  112.         try {  
  113.             tbtnDataConn.setChecked(PhoneUtils.getITelephony(telMgr).isDataConnectivityPossible());  
  114.         }  catch (Exception e) {  
  115.             Log.e("error",e.getMessage());  
  116.         }  
  117.     }  
  118.       
  119.     /**
  120.      * 来电时的操作
  121.      * @author GV
  122.      *
  123.      */
  124.     public class CheckEvent implements RadioGroup.OnCheckedChangeListener{  
  125.    
  126.         @Override
  127.         public void onCheckedChanged(RadioGroup group, int checkedId) {  
  128.             testTelephony.this.checkedId=checkedId;  
  129.         }  
  130.     }  
  131.       
  132.     /**
  133.      * Radio和数据连接的开关
  134.      * @author GV
  135.      *
  136.      */
  137.     public class ClickEvent implements View.OnClickListener{  
  138.    
  139.         @Override
  140.         public void onClick(View v) {  
  141.             if (v == tbtnRadioSwitch) {  
  142.                 try {  
  143.                     PhoneUtils.getITelephony(telMgr).setRadio(tbtnRadioSwitch.isChecked());  
  144.                 } catch (Exception e) {  
  145.                     Log.e("error", e.getMessage());  
  146.                 }  
  147.             }  
  148.             else if(v==tbtnDataConn){  
  149.                 try {  
  150.                     if(tbtnDataConn.isChecked())  
  151.                         PhoneUtils.getITelephony(telMgr).enableDataConnectivity();  
  152.                     else if(!tbtnDataConn.isChecked())  
  153.                         PhoneUtils.getITelephony(telMgr).disableDataConnectivity();  
  154.                 } catch (Exception e) {  
  155.                     Log.e("error", e.getMessage());  
  156.                 }     
  157.             }  
  158.         }  
  159.     }  
  160.       
  161.     /**
  162.      * 监视电话状态
  163.      * @author GV
  164.      *
  165.      */
  166.     public class CallStateListener extends PhoneStateListener {  
  167.         @Override
  168.         public void onCallStateChanged(int state, String incomingNumber) {  
  169.             if(state==TelephonyManager.CALL_STATE_IDLE)//挂断  
  170.             {  
  171.                 Log.e("IDLE",incomingNumber);  
  172.             }  
  173.             else if(state==TelephonyManager.CALL_STATE_OFFHOOK)//接听  
  174.             {  
  175.                 Log.e("OFFHOOK",incomingNumber);  
  176.             }  
  177.             else if(state==TelephonyManager.CALL_STATE_RINGING)//来电  
  178.             {  
  179.                 if(testTelephony.this.checkedId==R.id.rbtnAutoAccept)  
  180.                 {  
  181.                     try {  
  182.                         //需要<uses-permission android:name="android.permission.MODIFY_PHONE_STATE" />   
  183.                         PhoneUtils.getITelephony(telMgr).silenceRinger();//静铃  
  184.                         PhoneUtils.getITelephony(telMgr).answerRingingCall();//自动接听  
  185.                            
  186.                     } catch (Exception e) {  
  187.                         Log.e("error",e.getMessage());  
  188.                     }     
  189.                 }  
  190.                 else if(testTelephony.this.checkedId==R.id.rbtnAutoReject)  
  191.                 {  
  192.                     try {  
  193.                         PhoneUtils.getITelephony(telMgr).endCall();//挂断  
  194.                         PhoneUtils.getITelephony(telMgr).cancelMissedCallsNotification();//取消未接显示  
  195.                     } catch (Exception e) {  
  196.                         Log.e("error",e.getMessage());   
  197.                     }  
  198.                 }  
  199.             }  
  200.             super.onCallStateChanged(state, incomingNumber);  
  201.         }  
  202.     }  
  203. }
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP