Android来电监听和去电监听实现代码
我觉得写文章就得写得有用一些的,必须要有自己的思想,关于来电去电监听将按照下面三个问题展开
1、监听来电去电有什么用?
2、怎么监听,来电去电监听方式一样吗?
3、实战,有什么需要特别注意地方?
监听来电去电能干什么
1、能够对监听到的电话做个标识,告诉用户这个电话是诈骗、推销、广告什么的
2、能够针对那些特殊的电话进行自动挂断,避免打扰到用户
来电去电的监听方式(不一样的方式)
1、来电监听(phonestatelistener)
来电监听是使用phonestatelistener类,使用方式是,将phonestatelistener对象(一般是自己继承phonestatelistener类完成一些封装)注册到系统电话管理服务中去(telephonymanager)
然后通过phonestatelistener的回调方法oncallstatechanged(int state, string incomingnumber) 实现来电的监听 (详细实现可以参考后面给出的拓展阅读部分)
注册监听
// phoneservicename是服务名,一般是 "phone" --> context.telephony_service telephonymanager telephonymanager = (telephonymanager) mcontext.getsystemservice(phoneservicename); if(telephonymanager != null) { try { // 注册来电监听 telephonymanager.listen(mtelephonylistener, phonestatelistener.listen_call_state); } catch(exception e) { // 异常捕捉 } }
phonestatelistener的oncallstatechanged方法监听来电状态
@override public void oncallstatechanged(int state, string incomingnumber) { switch (state) { case telephonymanager.call_state_idle: // 电话挂断 break; case telephonymanager.call_state_offhook: // 来电响铃 break; case telephonymanager.call_state_ringing: // 来电接通 break; default: break; } }
三种状态源码解释
/** device call state: no activity. */ public static final int call_state_idle = 0; // 电话挂断 /** device call state: ringing. a new call arrived and is * ringing or waiting. in the latter case, another call is * already active. */ public static final int call_state_ringing = 1; // 来电响铃 /** device call state: off-hook. at least one call exists * that is dialing, active, or on hold, and no calls are ringing * or waiting. */ public static final int call_state_offhook = 2; // 来电接通
2、去电监听(通过广播来实现)
// outgoingcalllistener继承一个broadcastreceiver <receiver android:name="com.test.outgoingcalllistener" > <intent-filter> <action android:name="android.intent.action.phone_state"/> <action android:name="android.intent.action.new_outgoing_call" /> </intent-filter> </receiver>
实战,有什么需要特别注意地方
1、双卡双待的手机怎么获取
对于双卡手机,每张卡都对应一个service和一个phonestatelistener,需要给每个服务注册自己的phonestatelistener,服务的名称还会有点变化,厂商可能会修改
public arraylist<string> getmultsimcardinfo() { // 获取双卡的信息,这个也是经验尝试出来的,不知道其他厂商有什么坑 arraylist<string> phoneserverlist = new arraylist<string>(); for(int i = 1; i < 3; i++) { try { string phoneservicename; if (miuiutils.ismiuiv6()) { phoneservicename = "phone." + string.valueof(i-1); } else { phoneservicename = "phone" + string.valueof(i); } // 尝试获取服务看是否能获取到 ibinder ibinder = servicemanager.getservice(phoneservicename); if(ibinder == null) continue; itelephony itelephony = itelephony.stub.asinterface(ibinder); if(itelephony == null) continue; phoneserverlist.add(phoneservicename); } catch(exception e) { e.printstacktrace(); } } // 这个是默认的 phoneserverlist.add(context.telephony_service); return phoneserverlist; }
2、挂断电话
挂断电话使用系统服务提供的接口去挂断,但是挂断电话是个并不能保证成功的方法,所以会有多种方式挂断同时使用,下面提供
public boolean endcall() { boolean callsuccess = false; itelephony telephonyservice = gettelephonyservice(); try { if (telephonyservice != null) { callsuccess = telephonyservice.endcall(); } } catch (remoteexception e) { e.printstacktrace(); } catch (exception e){ e.printstacktrace(); } if (callsuccess == false) { executor es = executors.newsinglethreadexecutor(); es.execute(new runnable() { @override public void run() { disconnectcall(); } }); callsuccess = true; } return callsuccess; } private boolean disconnectcall() { runtime runtime = runtime.getruntime(); try { runtime.exec("service call phone 5 \n"); } catch (exception exc) { exc.printstacktrace(); return false; } return true; } // 使用endcall挂断不了,再使用killcall反射调用再挂一次 public static boolean killcall(context context) { try { // get the boring old telephonymanager telephonymanager telephonymanager = (telephonymanager) context.getsystemservice(context.telephony_service); // get the getitelephony() method class classtelephony = class.forname(telephonymanager.getclass().getname()); method methodgetitelephony = classtelephony.getdeclaredmethod("getitelephony"); // ignore that the method is supposed to be private methodgetitelephony.setaccessible(true); // invoke getitelephony() to get the itelephony interface object telephonyinterface = methodgetitelephony.invoke(telephonymanager); // get the endcall method from itelephony class telephonyinterfaceclass = class.forname(telephonyinterface.getclass().getname()); method methodendcall = telephonyinterfaceclass.getdeclaredmethod("endcall"); // invoke endcall() methodendcall.invoke(telephonyinterface); } catch (exception ex) { // many things can go wrong with reflection calls return false; } return true; }
3、挂断电话需要权限
<uses-permission android:name="android.permission.call_phone" />
以上所述是小编给大家介绍的android来电监听和去电监听,希望对大家有所帮助
上一篇: vs2010怎么插入断点和取消断点?