Android电话拦截实现以及TelephonyManager监听的取消
由于毕业设计题目涉及到电话拦截这一块。所以鼓捣了一下。遇到了一些问题,总结一下,以免忘记,也希望能帮助其他新人们。
本篇博客目的:实现电话的拦截
会遇到的问题: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监听的取消的更多相关文章
- Android中Button的五种监听事件
简单聊一下Android中Button的五种监听事件: 1.在布局文件中为button添加onClick属性,Activity实现其方法2.匿名内部类作为事件监听器类3.内部类作为监听器4.Activ ...
- DownEditTextView【自定义Edittext对Android 软键盘向下的监听】
版权声明:本文为HaiyuKing原创文章,转载请注明出处! 前言 记录自定义EditText控件实现监听软键盘隐藏事件的功能.基本上和参考资料相同. 效果图 代码分析 自定义EditText子 ...
- 【Android】[转] Android屏幕旋转使用OrientationEventListener的监听
说明 遇到一个奇葩的问题,我在使用onConfigChanged拦截屏幕的横竖屏旋转时,发现直接进行180度的横屏/竖屏转换居然没有反应!查找原因发现仅对landscape或者portrait状态有用 ...
- Android 多个include标签的监听事件处理
include标签的作用是为了xml文件代码的模块化,详细不再多提.主要是说说include标签的监听. 网上也有很多例子,不过大多是只写了一个include标签的监听,如果需要实现多个include ...
- Android应用中返回键的监听及处理
MainActivity: package com.testnbackpressed; import android.os.Bundle; import android.view.KeyEvent ...
- Android中Preference的使用以及监听事件分析
在Android系统源码中,绝大多数应用程序的UI布局采用了Preference的布局结构,而不是我们平时在模拟器中构建应用程序时使用的View布局结构,例如,Setting模块中布局.当然,凡事都有 ...
- Android之EditText文本变化的监听
监听EditText的文本变化需要给EditText控件加一个addTextChangeListener监听器 editText.addTextChangeListener(textWatcher); ...
- Android 用Activity的onTouchEvent来监听滑动手势
package com.example.activityOnTouchEvent; import android.app.Activity; import android.os.Bundle; imp ...
- Android 开发中的View事件监听机制
在开发过程中,我们常常根据实际的需要绘制自己的应用组件,那么定制自己的监听事件,及相应的处理方法是必要的.我们都知道Android中,事件的监听是基于回调机制的,比如常用的OnClick事件,你了解它 ...
随机推荐
- DrawerLayout案例
布局文件: <?xml version="1.0" encoding="utf-8"?> <android.support.v4.widget ...
- 基于AOP的iOS用户操作引导框架设计
背景 有一种现象,App设计者觉得理所当然的操作方式,却常常被用户所忽视,为了防止这种现象发生,就要为App设计一个帮助,一种低成本的方案是将帮助文档写成HTML然后展示给用户,这样的方式常常不能带来 ...
- LAB颜色空间各通道的取值范围
简介 LAB颜色空间在计算机视觉中经常被使用,知道L,A,B三个通道的取值范围有一定的意义. OpenCV获取LAB取值范围 下面是一段实验代码,用于获取LAB的取值范围. 基本思路是,排列组合所有R ...
- FFmpeg的HEVC解码器源代码简单分析:CTU解码(CTU Decode)部分-PU
===================================================== HEVC源代码分析文章列表: [解码 -libavcodec HEVC 解码器] FFmpe ...
- JAVA面向对象-----extends关键字
继承使用extends关键字实现 1:发现学生是人,工人是人.显然属于is a 的关系,is a就是继承. 2:谁继承谁? 学生继承人,发现学生里的成员变量,姓名和年龄,人里边也都进行了定义.有重 复 ...
- Maven插件详解
插件与插件目标 Maven定义了三套相互独立的生命周期,每套生命周期都有多个生命周期阶段,而这些阶段都是抽象的,不做任何工作.真正完成工作的是绑定在生命周期阶段的插件目标.插件以独立的构件形式存在,一 ...
- 基于CAS实现单点登录(SSO):工作原理
工作中使用到了SSO,网上看到了这个博客的一系列文章感觉不错,转载收藏 源地址http://blog.csdn.net/tch918/article/details/19930037 系列文章的第一篇 ...
- RDD:基于内存的集群计算容错抽象
转载自:http://shiyanjun.cn/archives/744.html 摘要 本文提出了分布式内存抽象的概念--弹性分布式数据集(RDD,Resilient Distributed Dat ...
- Shell命令:echo 命令详解
http://blog.chinaunix.net/uid-27124799-id-3383327.html # echo命令介绍 功能说明:显示文字. 语 法:echo [-ne][字符串] / e ...
- win7下创建逻辑分区
许多用户在安装Win7时都遇,安装程序创建的都是主分区,并没有创建逻辑分区的任何选项,这样的情况导致创建4个主分区后剩余的空间无法继续分配的情况,这使得许多用户情何以堪.很多用户都向小编反映该问题,有 ...