Android2.3之前的系统可以通过反射机制调用ITelephone的方法来挂断电话,因为Android2.3以后增加了对permission  android.permission.MODIFY_PHONE_STATE 的限制,之前的反射的方法不能用了,我们可以通过发送广播的方式来接听电话。

示例代码

package com.example.android_3gtest;import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.net.Uri;
import android.os.Build;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.view.KeyEvent;public class PhoneUtil {public static String TAG = PhoneUtil.class.getSimpleName();  /*** 挂断电话* @param context*/public static void endCall(Context context) {  try {  Object telephonyObject = getTelephonyObject(context);  if (null != telephonyObject) {  Class telephonyClass = telephonyObject.getClass();  Method endCallMethod = telephonyClass.getMethod("endCall");  endCallMethod.setAccessible(true);  endCallMethod.invoke(telephonyObject);  }  } catch (SecurityException e) {  e.printStackTrace();  } catch (NoSuchMethodException e) {  e.printStackTrace();  } catch (IllegalArgumentException e) {  e.printStackTrace();  } catch (IllegalAccessException e) {  e.printStackTrace();  } catch (InvocationTargetException e) {  e.printStackTrace();  }  }  private static Object getTelephonyObject(Context context) {  Object telephonyObject = null;  try {  // 初始化iTelephony  TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);  // Will be used to invoke hidden methods with reflection  // Get the current object implementing ITelephony interface  Class telManager = telephonyManager.getClass();  Method getITelephony = telManager.getDeclaredMethod("getITelephony");  getITelephony.setAccessible(true);  telephonyObject = getITelephony.invoke(telephonyManager);  } catch (SecurityException e) {  e.printStackTrace();  } catch (NoSuchMethodException e) {  e.printStackTrace();  } catch (IllegalArgumentException e) {  e.printStackTrace();  } catch (IllegalAccessException e) {  e.printStackTrace();  } catch (InvocationTargetException e) {  e.printStackTrace();  }  return telephonyObject;  }  /*** 通过反射调用的方法,接听电话,该方法只在android 2.3之前的系统上有效。* @param context*/  private static void answerRingingCallWithReflect(Context context) {  try {  Object telephonyObject = getTelephonyObject(context);  if (null != telephonyObject) {  Class telephonyClass = telephonyObject.getClass();  Method endCallMethod = telephonyClass.getMethod("answerRingingCall");  endCallMethod.setAccessible(true);  endCallMethod.invoke(telephonyObject);  // ITelephony iTelephony = (ITelephony) telephonyObject;  // iTelephony.answerRingingCall();  }  } catch (SecurityException e) {  e.printStackTrace();  } catch (IllegalArgumentException e) {  e.printStackTrace();  } catch (IllegalAccessException e) {  e.printStackTrace();  } catch (InvocationTargetException e) {  e.printStackTrace();  } catch (NoSuchMethodException e) {  e.printStackTrace();  }  }  /*** 伪造一个有线耳机插入,并按接听键的广播,让系统开始接听电话。* @param context*/  private static void answerRingingCallWithBroadcast(Context context){  AudioManager localAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);  //判断是否插上了耳机  boolean isWiredHeadsetOn = localAudioManager.isWiredHeadsetOn();  if (!isWiredHeadsetOn) {  Intent headsetPluggedIntent = new Intent(Intent.ACTION_HEADSET_PLUG);  headsetPluggedIntent.putExtra("state", 1);  headsetPluggedIntent.putExtra("microphone", 0);  headsetPluggedIntent.putExtra("name", "");  context.sendBroadcast(headsetPluggedIntent);  Intent meidaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON);  KeyEvent keyEvent = new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_HEADSETHOOK);  meidaButtonIntent.putExtra(Intent.EXTRA_KEY_EVENT,keyEvent);  context.sendOrderedBroadcast(meidaButtonIntent, null);  Intent headsetUnpluggedIntent = new Intent(Intent.ACTION_HEADSET_PLUG);  headsetUnpluggedIntent.putExtra("state", 0);  headsetUnpluggedIntent.putExtra("microphone", 0);  headsetUnpluggedIntent.putExtra("name", "");  context.sendBroadcast(headsetUnpluggedIntent);  } else {  Intent meidaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON);  KeyEvent keyEvent = new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_HEADSETHOOK);  meidaButtonIntent.putExtra(Intent.EXTRA_KEY_EVENT,keyEvent);  context.sendOrderedBroadcast(meidaButtonIntent, null);  }  }  /*** 接听电话* @param context*/  public static void answerRingingCall(Context context) {  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {   //2.3或2.3以上系统answerRingingCallWithBroadcast(context);  } else {  answerRingingCallWithReflect(context);  }  }  /*** 打电话* @param context* @param phoneNumber*/  public static void callPhone(Context context, String phoneNumber) {  if(!TextUtils.isEmpty(phoneNumber)){  try {  Intent callIntent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:"+ phoneNumber));  context.startActivity(callIntent);  } catch (Exception e) {  e.printStackTrace();  }  }  }  /*** 拨电话* @param context* @param phoneNumber*/  public static void dialPhone(Context context, String phoneNumber){  if(!TextUtils.isEmpty(phoneNumber)){  try {  Intent callIntent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:"+ phoneNumber));  context.startActivity(callIntent);  } catch (Exception e) {  e.printStackTrace();  }  }  }
}

Android拨打、接听、挂断电话操作相关推荐

  1. android蓝牙耳机来电铃声,Android蓝牙耳机接听挂断电话流程

    一.alps/packages/apps/Bluetooth/src/com/android/bluetooth/hfp/HeadsetStateMachine.java image.png proc ...

  2. Android通过AIDL实现接听电话、挂断电话操作 | 拨打电话

    Android中拨打电话功能是很轻松的只需要几行代码即可,如下: 首先还是得加上权限这个东西(或者动态申请) <!--拨打电话--> <uses-permission android ...

  3. Android 7.0 挂断电话流程分析

    1.图形显示 挂断电话分为本地挂断和远程对方挂断 2.本地挂断 1).点击按钮 先看按键的监听事件 CallCardFragment.java 中有对按钮的监听事件 @Overridepublic v ...

  4. Android之——自动挂断电话的实现

    转载请注明出处:http://blog.csdn.net/l1028386804/article/details/47072451 通过<Android之--AIDL小结>与<And ...

  5. Xamarin.Android实现自动拨打及挂断电话的功能

    目录 1.说明 2.具体实现 2.1 效果 2.2 原理说明 2.3 具体代码实现 2.3.1 layout 2.3.3 广播接收器 2.3.3 拨打电话的Activity 2.3.4 主界面Acti ...

  6. Android开发教程--自定义接听/挂断电话功能

    1.首先在manifest中加入如下的权限 <uses-permission android:name="android.permission.READ_PHONE_STATE&quo ...

  7. Android自动接听和挂断电话实现原理

    转自:http://bbs.51cto.com/thread-1078059-1.html 一  前言 这两天要研究类似白名单黑名单以及手势自动接听的一些功能,所以呢,自然而然的涉及到怎么自动接听/挂 ...

  8. Android挂断电话以及Java Class Loader

    Android中,要自己实现一个挂断电话方法时,很久之前可以endCall().不过现在已经不行了,要应用反射机制,获取到 "android.os.ServiceManager" ...

  9. android6.0 挂断电话流程分析(一)

    下面是android 6.0挂断电话的流程分析图: 后继续更新挂断回调...........................!

最新文章

  1. jQuery 遍历函数
  2. pep8 python 编码规范_Python合集之Python语法特点(三)
  3. java排序-交换排序-快速排序
  4. 有什么用_app用什么软件编写
  5. 【华为云技术分享】大数据容器化成趋势,华为云BigData Pro一马当先
  6. 181023词霸有道扇贝每日一句
  7. 【MapReduce】MapReduce(DataJoin)实现数据连接+Bloom Filter优化
  8. JPG、PNG和GIF图片的基本原理及优化方法
  9. [UWP开发] Win10微博分享
  10. Java实现昵图网摄影图片爬虫
  11. 无人驾驶-控制-阿克曼模型
  12. Linux软RAID换硬盘
  13. Java中的正则表达式 regex
  14. HUST1005 渊子赛马【枚举】
  15. 让技术Leader疯狂点赞的Linux速成手册,到底有多强悍?
  16. python six 用途_python之six用法
  17. 移动web开发—— rem布局
  18. input的onkeyup事件
  19. 苹果系统模拟器_开发者成功在苹果MacBook上通过模拟器正常运行Windows 10X版
  20. python集成环境pydev如何使用_Eclipse+PyDev环境搭建

热门文章

  1. Java工厂企业工艺管理系统源码 springboot2+vue2前后端分离架构 工艺路线 加工工序管理源码
  2. 如何将pdf文档转换成txt格式
  3. 中国互联网站发展状况及其安全报告(2015)发布
  4. Cesium开发-绘制墙体
  5. 微信小程序实现抽奖走马灯+圆点闪烁
  6. neo4j 使用之数据导入初探
  7. freeswitch实现监听、三方通话的两种方法
  8. 8数码无解,拼图问题
  9. 工作日闹钟 android,在Android中设置重复星期几闹钟
  10. 给小朋友讲什么是计算机ppt,计算机是什么给小学生讲计算机知识ppt.ppt