在Android1.5版本之前,实现挂断电话是非常容易的事,只需要调用TelephonyManager的endCall()方法就可以了,但在1.5版本之后,Google工程师为了手机的安全期间,把endCall的方法隐藏掉了。所以实现挂断电话可以通过反射的方法,执行endCall方法。具体实现如下:

TelephonyManager在源码里是这样描述的:Context.getSystemService(Context.TELEPHONY_SERVICE)},我们通过TELEPHONY_SERVICE系统服务调用就可以获取。

registerService(TELEPHONY_SERVICE, new ServiceFetcher() {public Object createService(ContextImpl ctx) {return new TelephonyManager(ctx.getOuterContext());}});

而 android.os.ServiceManager  有getService方法

/*** Returns a reference to a service with the given name.** @param name the name of the service to get* @return a reference to the service, or <code>null</code> if the service doesn't exist*/public static IBinder getService(String name) {return null;}

android.telephony.TelephonyManager 类是代理ITelephony接口的,可以看到里面的endCall是被隐藏掉的。

/*** Interface used to interact with the phone.  Mostly this is used by the* TelephonyManager class.  A few places are still using this directly.* Please clean them up if possible and use TelephonyManager insteadl.** {@hide}*/
interface ITelephony {/*** Dial a number. This doesn't place the call. It displays* the Dialer screen.* @param number the number to be dialed. If null, this* would display the Dialer screen with no number pre-filled.*//*** End call or go to the Home screen** @return whether it hung up*/boolean endCall();

我们知道利用绑定服务可以调用里面的方法,会返回一个IBinder对象,利用IBinder可以调用服务里的方法。

TelephonyManager实际上就是系统电话服务的代理对象,通过aidl获取IBinder,然后进一步的进行封装。代理有限的方法。

TelephonyManager    ------------------IBinder-------------------------系统服务

代理对象                                                                   aidl

所以实现需要一下步奏:

1 、 反射加载ServiceManager类。

2、 获取IBinder对象执行getService方法。

3、 在项目中新建com.android.internal.telephony包(包名不能变),然后拷贝ITelephony.aidl。 在新建android.telephony包,拷贝NeighboringCellInfo.aidl 的进程通信接口两个文件。

4、调用endCall方法。

实现方法如下:

private void endCall() {// IBinder iBinder = ServiceManager.getService(TELEPHONY_SERVICE);// ServiceManager 是被系统隐藏掉了 所以只能用反射的方法获取try {// 加载ServiceManager的字节码Class<?> clazz = CallSMSSafeService.class.getClassLoader().loadClass("android.os.ServiceManager");Method method = clazz.getDeclaredMethod("getService",String.class);IBinder iBinder = (IBinder) method.invoke(null,TELEPHONY_SERVICE);ITelephony.Stub.asInterface(iBinder).endCall();} catch (Exception e) {e.printStackTrace();Log.i(TAG, "拦截电话异常");}}

Android开发之来电电话挂断实现相关推荐

  1. android开发之来电自动拒接并自动回复短信_上课模式app

    上课的时候老师说总是错过电话,对方打来没人接还一遍遍的打,觉得可以有个app在上课期间自动拒接电话,并自动回复短信过去. 当然了,需要权限的. 尝试做了个雏形出来. 界面如下: 主要代码如下: pac ...

  2. android 来电拒接_[置顶] android开发之来电自动拒接并自动回复短信_上课模式app...

    上课的时候老师说总是错过电话,对方打来没人接还一遍遍的打,觉得可以有个app在上课期间自动拒接电话,并自动回复短信过去. 当然了,需要权限的. 尝试做了个雏形出来. 界面如下: 主要代码如下: pac ...

  3. android 拨打电话 发送短信 权限,Android开发实现拨打电话与发送信息的方法分析...

    本文实例讲述了Android开发实现拨打电话与发送信息的方法.分享给大家供大家参考,具体如下: xml布局: android:layout_width="fill_parent" ...

  4. android 挂断 电话 反射,Android实现来电自动挂断实现机制

    通过aidl及反射实现挂断电话 具体分三步: (1)ITelephony.aidl ,必须新建com.Android.internal.telephony包并放入ITelephony.aidl文件(构 ...

  5. android 监听电话状态 来电 接听 挂断

    如果想要监听手机的来电状态  需要接收手机的电话广播 首先是静态注册 <receiver android:name=".PhoneReceiver"android:expor ...

  6. Android通过程序接听或者挂断电话

    转载注明出处:简书-十个雨点 这篇文章教你如何帮助用户自动接听或者挂断来电.当然并不是我原创的代码,我只不过是把stackoverflow上的一些代码整合了一下,做个代码的二传手. 源码 Accept ...

  7. Android 来去电监听,电话挂断

    android:enabled="true" android:process=":PhoneListenService"> public class Ph ...

  8. Android Telephony 9.0通话挂断连接处理机制(opt/Telephony)

    前言:今天看了一下通话断开处理流程,所以做一个笔记来记录一下今天的学习成果. 通话断开连接一般有两种应用场景 本地主动挂通话 远端断开通话连接 (这里还包括网络挂断和对方挂断) 先处理本地挂断 本地主 ...

  9. Service-黑名单来电自动挂断

    这只是一个学习例程,很多功能有待完善 允许用户动态加载手机通讯录中的电话号码,勾选某些号码作为黑名单,一旦将某些号码设为黑名单,当该号码自动挂断该号码 Android没有对外公开挂断电话的API,如果 ...

  10. andrid之来电自动挂断电话

    先去官网下载ITelephony.aidl文件 然后在新建一个AIDL文件名字就叫ITelephony 在把下载的ITelephony.aidl替换你新建的ITelephony 重新编译 然后新建一个 ...

最新文章

  1. javascript 执行环境细节分析、原理-12
  2. Oracle - 数据库的实例、表空间、用户、表之间关系
  3. 域名怎么设置非80端口_深信服网关怎么设置端口映射
  4. 1.2 边缘检测示例-深度学习第四课《卷积神经网络》-Stanford吴恩达教授
  5. android屏幕基础知识
  6. linux nvidia驱动安装rpm,Fedora 下安装NVIDIA显卡驱动
  7. 《程序设计技术》第四章例程
  8. 安装一个自己的笔记软件——Wiz开源私有云笔记
  9. 博士德服务器帐套维护密码忘记,T+账套主管的密码忘记了,怎么办
  10. oracle数据库考试题带答案解析,oracle数据库期末考试试题及答案剖析
  11. 联通智能城域网,到底有什么特别?
  12. 神经网络的原理和应用,神经网络理论及应用
  13. CodeForces 1037E Trips(瞎搞)
  14. Oops是什么有什么用
  15. Python之拉盖尔多项式
  16. 终于可以舒服的看电子书了
  17. [转] 串行传输 VS 并行传输
  18. 有道翻译 python 翻译
  19. 深入探索 Android 网络优化(一、网络筑基篇,为什么Flutter能最好地改变移动开发
  20. chrome硬件加速_如何在Chrome中打开和关闭硬件加速

热门文章

  1. 四数之和 leetcode
  2. main函数执行前后发生了什么
  3. python 摄像头采集_Python+OpenCV采集本地摄像头的视频
  4. oracle如何查找谁删除了数据_php如何删除session中数据
  5. 什么是document对象?如何获取文档对象上的元素?_JavaScript DOM操作元素的方法,你还记得多少?...
  6. 分步表单_后台产品设计之表单页设计
  7. 大白话解析模拟退火算法、遗传算法
  8. 动手试试Android Studio插件开发
  9. 【Gym - 100837 F】Controlled Tournament【竞赛树 状态压缩】
  10. 联想微型计算机beta2,联想上网本升级IdeaPad S10-2