由于毕业设计题目涉及到电话拦截这一块。所以鼓捣了一下。遇到了一些问题,总结一下,以免忘记,也希望能帮助其他新人们。

本篇博客目的:实现电话的拦截

会遇到的问题:android studio下AIDL的使用,TelephonyManager.Listen()的监听取消。

首先,电话状态监听需要涉及到系统服务TelephonyManager,我们需要获取到他的实例

 mTelephonyManager = (TelephonyManager)getSystemService(TELEPHONY_SERVICE);
     

之后我们就可以向他添加状态改变的监听,需要重写监听器PhoneStateListener

他的state值有如下几种

TelephonyManager.CALL_STATE_IDLE:  空闲状态

TelephonyManager.CALL_STATE_RINGING:  响铃状态

TelephonyManager.CALL_STATE_OFFHOOK:  挂掉电话

    class MyPhoneListener extends PhoneStateListener{

            @Override
public void onCallStateChanged(int state, String incomingNumber) {
if(state==TelephonyManager.CALL_STATE_RINGING){
endCall();
super.onCallStateChanged(state,incomingNumber);
}
}
}

这里我给他加了个模式来设置监听与取消。

我们需要用一个FLAG : PhoneStateListener.LISTEN_NONE来取消监听。如果新加监听器,会同时监听

   private void setTelephonyListener(int mode){
if(mode==INTERRUPTED){
mTelephonyManager.listen(mMyPhoneListener,PhoneStateListener.LISTEN_CALL_STATE);
}else{
mTelephonyManager.listen(mMyPhoneListener,PhoneStateListener.LISTEN_NONE);
}
}

好了,以上就是状态监听的步骤。

那么如何实现电话的拦截呢?由于安卓隐藏了PHONE类的API,所以我们需要用AIDL将它的方法反射出来。

1,新建AIDL文件,ITelephony.aidl。注意包名为com.android.internal.telephony,不可更改,此时需要rebuild project才能正常使用。

内容如下:

package com.android.internal.telephony;  

 /**  

  * 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 instead.  

  * {@hide}  

  */ 

interface ITelephony {      

 /**      

  * End call or go to the Home screen

  * @return whether it hung up     

  */    

 boolean endCall();      

   /**      

    * Answer the currently-ringing call.      

    *      

    * If there's already a current active call, that call will be      

    * automatically put on hold.  If both lines are currently in use, the     

    * current active call will be ended.      

    *    

    * TODO: provide a flag to let the caller specify what policy to use     

    * if both lines are in use.  (The current behavior is hardwired to     

    * "answer incoming, end ongoing", which is how the CALL button      

    * is specced to behave.)      

    *      

    * TODO: this should be a oneway call (especially since it's called     

    * directly from the key queue thread).      

    */     

    void answerRingingCall(); 

    /**

     * Allow mobile data connections.

     */

    boolean enableDataConnectivity();

    /**

     * Disallow mobile data connections.

     */

    boolean disableDataConnectivity();

    /**

     * Report whether data connectivity is possible.

     */

    boolean isDataConnectivityPossible();

} 

之后来反射出来endcall方法,当来电的时候 即可对电话进行拦截

 private void endCall()
{
Class<TelephonyManager> c = TelephonyManager.class;
try
{
Method getITelephonyMethod = c.getDeclaredMethod("getITelephony", (Class[]) null);
getITelephonyMethod.setAccessible(true);
ITelephony iTelephony = null;
iTelephony = (ITelephony) getITelephonyMethod.invoke(mTelephonyManager, (Object[]) null);
iTelephony.endCall(); Log.i("wing", "iTelePhony endcall");
}
catch (Exception e)
{ Log.i("wing", "iTelePhony endcall failed"+e.getMessage() );
}
}

以上就是我的经验和总结,欢迎大家讨论。

Android电话拦截实现以及TelephonyManager监听的取消的更多相关文章

  1. Android中Button的五种监听事件

    简单聊一下Android中Button的五种监听事件: 1.在布局文件中为button添加onClick属性,Activity实现其方法2.匿名内部类作为事件监听器类3.内部类作为监听器4.Activ ...

  2. DownEditTextView【自定义Edittext对Android 软键盘向下的监听】

    版权声明:本文为HaiyuKing原创文章,转载请注明出处! 前言 记录自定义EditText控件实现监听软键盘隐藏事件的功能.基本上和参考资料相同. 效果图    代码分析 自定义EditText子 ...

  3. 【Android】[转] Android屏幕旋转使用OrientationEventListener的监听

    说明 遇到一个奇葩的问题,我在使用onConfigChanged拦截屏幕的横竖屏旋转时,发现直接进行180度的横屏/竖屏转换居然没有反应!查找原因发现仅对landscape或者portrait状态有用 ...

  4. Android 多个include标签的监听事件处理

    include标签的作用是为了xml文件代码的模块化,详细不再多提.主要是说说include标签的监听. 网上也有很多例子,不过大多是只写了一个include标签的监听,如果需要实现多个include ...

  5. Android应用中返回键的监听及处理

    MainActivity: package com.testnbackpressed;  import android.os.Bundle;  import android.view.KeyEvent ...

  6. Android中Preference的使用以及监听事件分析

    在Android系统源码中,绝大多数应用程序的UI布局采用了Preference的布局结构,而不是我们平时在模拟器中构建应用程序时使用的View布局结构,例如,Setting模块中布局.当然,凡事都有 ...

  7. Android之EditText文本变化的监听

    监听EditText的文本变化需要给EditText控件加一个addTextChangeListener监听器 editText.addTextChangeListener(textWatcher); ...

  8. Android 用Activity的onTouchEvent来监听滑动手势

    package com.example.activityOnTouchEvent; import android.app.Activity; import android.os.Bundle; imp ...

  9. Android 开发中的View事件监听机制

    在开发过程中,我们常常根据实际的需要绘制自己的应用组件,那么定制自己的监听事件,及相应的处理方法是必要的.我们都知道Android中,事件的监听是基于回调机制的,比如常用的OnClick事件,你了解它 ...

随机推荐

  1. 高仿腾讯QQ最终版

    之前写过一篇关于高仿腾讯QQ的博客,不知道的看这:http://blog.csdn.net/htq__/article/details/51840273 ,主要是从界面上高仿了腾讯QQ,在UI上基本上 ...

  2. Android设置竖屏

    Android设置竖屏(禁止旋转屏) 清单文件,Activity下添加属性 android:screenOrientation="portrait" 如下 <?xml ver ...

  3. 开源框架Volley的使用《二》[NetWorkImageView&&LruCache&ImageLoader]

    转载本专栏每一篇博客请注明转载出处地址,尊重原创.此博客转载链接地址:小杨的博客    http://blog.csdn.net/qq_32059827/article/details/5278849 ...

  4. activiti uuid主键

    1.1.1.  activiti默认主键生成方式 ; 下面我们看一下主键的生成策略:主键的生成策略定义在IdGenerator接口中,接口定义如下所示: public interface IdGene ...

  5. JBOSS EAP 6 系列五 Managed domains 管理域最主要的功能是“统一部署,统一配置”

    摘要 本文首先介绍Managed Domain的概念,管理域最主要的功能是"统一部署,统一配置".接下来通过一个实例在"统一配置"部分实现一个双机配置起来的域, ...

  6. 5.1、Android Studio用Logcat编写和查看日志

    Android Studio在Android Monitor中包含了一个logcat的tab,可以打印系统事件,比如垃圾回收发生时,实时打印应用消息. 为了显示需要的信息,你可以创建过滤器,更改需要显 ...

  7. JS和Jquery操作label标签

    获取label值:  label标签在JS和Jquery中使用不能像其他标签一样用value获取它的值: 代码如下: var label=document.getElementById("s ...

  8. 剑指Offer——二叉树

    剑指Offer--二叉树 前言 数据结构通常是编程面试中考察的重点.在参加面试之前,应聘者需要熟练掌握链表.树.栈.队列和哈希表等数据结构,以及它们的操作.本片博文主要讲解二叉树操作的相关知识,主要包 ...

  9. spark概念、编程模型和模块概述

    http://blog.csdn.net/pipisorry/article/details/50931274 spark基本概念 Spark一种与 Hadoop 相似的通用的集群计算框架,通过将大量 ...

  10. Swift基础之闭包Closure学习

    首先Swift语言中没有了Block内容,但是你可以通过调用OC文件使用,也可以使用Closure(闭包),实现Block或者Delegae同样反向传值或回调函数的效果,也可以解决函数指针的问题,两者 ...