转自:http://stephen830.iteye.com/blog/1181786


//-----------------------------------------------------------------------------------------------------------------------------------//


android 来电自动接听和自动挂断

注意:android2.3版本不支持下面的自动接听方法。(会抛异常:java.lang.SecurityException: Neither user xxxxx nor current process has android.permission.MODIFY_PHONE_STATE.)

第一步:准备应用环境需要的系统包和aidl文件。

(1)在应用中创建包:android.telephony

将android系统框架下的\framework\telephony\java\android\telephony目录中的NeighboringCellInfo.aidl文件复制到上面创建的包(android.telephony )中;

(2)在应用中创建包:com.android.internal.telephony

将android系统框架下的\framework\telephony\java\com\android\internal\telephony目录中的ITelephony.aidl文件复制到上面创建的包(com.android.internal.telephony )中;

第二步:创建一个获取ITelephony的方法

PhoneUtils.java

Java代码 收藏代码
  1. packagecom.zhouzijing.android.demo;
  2. importjava.lang.reflect.Method;
  3. importcom.android.internal.telephony.ITelephony;
  4. importandroid.telephony.TelephonyManager;
  5. publicclassPhoneUtils{
  6. /**
  7. *根据传入的TelephonyManager来取得系统的ITelephony实例.
  8. *@paramtelephony
  9. *@return系统的ITelephony实例
  10. *@throwsException
  11. */
  12. publicstaticITelephonygetITelephony(TelephonyManagertelephony)throwsException{
  13. MethodgetITelephonyMethod=telephony.getClass().getDeclaredMethod("getITelephony");
  14. getITelephonyMethod.setAccessible(true);//私有化函数也能使用
  15. return(ITelephony)getITelephonyMethod.invoke(telephony);
  16. }
  17. }

第三步:创建电话广播拦截器

MyPhoneBroadcastReceiver.java

Java代码 收藏代码
  1. packagecom.zhouzijing.android.demo;
  2. importcom.android.internal.telephony.ITelephony;
  3. importandroid.content.BroadcastReceiver;
  4. importandroid.content.Context;
  5. importandroid.content.Intent;
  6. importandroid.telephony.TelephonyManager;
  7. importandroid.util.Log;
  8. publicclassMyPhoneBroadcastReceiverextendsBroadcastReceiver{
  9. privatefinalstaticStringTAG=MyPhone.TAG;
  10. @Override
  11. publicvoidonReceive(Contextcontext,Intentintent){
  12. Stringaction=intent.getAction();
  13. Log.i(TAG,"[Broadcast]"+action);
  14. //呼入电话
  15. if(action.equals(MyPhone.B_PHONE_STATE)){
  16. Log.i(TAG,"[Broadcast]PHONE_STATE");
  17. doReceivePhone(context,intent);
  18. }
  19. }
  20. /**
  21. *处理电话广播.
  22. *@paramcontext
  23. *@paramintent
  24. */
  25. publicvoiddoReceivePhone(Contextcontext,Intentintent){
  26. StringphoneNumber=intent.getStringExtra(
  27. TelephonyManager.EXTRA_INCOMING_NUMBER);
  28. TelephonyManagertelephony=(TelephonyManager)context.getSystemService(
  29. Context.TELEPHONY_SERVICE);
  30. intstate=telephony.getCallState();
  31. switch(state){
  32. caseTelephonyManager.CALL_STATE_RINGING:
  33. Log.i(TAG,"[Broadcast]等待接电话="+phoneNumber);
  34. try{
  35. ITelephonyiTelephony=PhoneUtils.getITelephony(telephony);
  36. iTelephony.answerRingingCall();//自动接通电话
  37. //iTelephony.endCall();//自动挂断电话
  38. }catch(Exceptione){
  39. Log.e(TAG,"[Broadcast]Exception="+e.getMessage(),e);
  40. }
  41. break;
  42. caseTelephonyManager.CALL_STATE_IDLE:
  43. Log.i(TAG,"[Broadcast]电话挂断="+phoneNumber);
  44. break;
  45. caseTelephonyManager.CALL_STATE_OFFHOOK:
  46. Log.i(TAG,"[Broadcast]通话中="+phoneNumber);
  47. break;
  48. }
  49. }
  50. }

第四部:注册电话广播拦截器

MyPhone.java

Java代码 收藏代码
  1. packagecom.zhouzijing.android.demo;
  2. importandroid.app.Activity;
  3. importandroid.content.IntentFilter;
  4. importandroid.os.Bundle;
  5. importandroid.telephony.TelephonyManager;
  6. importandroid.util.Log;
  7. importandroid.view.View;
  8. publicclassMyPhoneextendsActivity{
  9. publicfinalstaticStringTAG="MyPhone";
  10. publicfinalstaticStringB_PHONE_STATE=TelephonyManager.ACTION_PHONE_STATE_CHANGED;
  11. privateMyPhoneBroadcastReceivermBroadcastReceiver;
  12. @Override
  13. publicvoidonCreate(BundlesavedInstanceState){
  14. super.onCreate(savedInstanceState);
  15. setContentView(R.layout.my_phone);
  16. }
  17. //按钮1-注册广播
  18. publicvoidregisterThis(Viewv){
  19. Log.i(TAG,"registerThis");
  20. mBroadcastReceiver=newMyPhoneBroadcastReceiver();
  21. IntentFilterintentFilter=newIntentFilter();
  22. intentFilter.addAction(B_PHONE_STATE);
  23. intentFilter.setPriority(Integer.MAX_VALUE);
  24. registerReceiver(mBroadcastReceiver,intentFilter);
  25. }
  26. //按钮2-撤销广播
  27. publicvoidunregisterThis(Viewv){
  28. Log.i(TAG,"unregisterThis");
  29. unregisterReceiver(mBroadcastReceiver);
  30. }
  31. }

第5步:在AndroidManifest.xml配置权限

Xml代码 收藏代码
  1. <uses-permissionandroid:name="android.permission.READ_PHONE_STATE"/>
  2. <uses-permissionandroid:name="android.permission.MODIFY_PHONE_STATE"/>
  3. <uses-permissionandroid:name="android.permission.CALL_PHONE"/>

其中:

Java代码 收藏代码
  1. iTelephony.answerRingingCall();//自动接通电话

必须有权限 android.permission.MODIFY_PHONE_STATE

Java代码 收藏代码
  1. iTelephony.endCall();//自动挂断电话

必须有权限 android.permission.CALL_PHONE

更多相关文章

  1. Android电话秀(二)
  2. 第一行代码Android第三课
  3. Android 开源源代码收集(不断更新中...)
  4. android打电话,接电话,挂电话过程
  5. 我的Android进阶之旅------>Android电话实例
  6. Android顶部工具栏和底部工具栏的简单实现代码
  7. 收藏Android学习相关资料

随机推荐

  1. Android基础总结八:ContentProvider
  2. 从无到有,支付路由系统升级打怪之路|原创
  3. CTF之Five86-1靶机***实战
  4. Android(安卓)S5PV210 camera驱动(tw9912)
  5. Vue 第一次 commit 原来是这个样子
  6. 人生新篇章
  7. 使用 apiDoc 为你的Node.js API 自动生成
  8. 一步步从头搭建 Vue 开发环境
  9. 8 个你不知道的 DOM 功能[每日前端夜话0x
  10. 从输入 URL 到展现涉及哪些缓存环节(非常