android 电话状态的监听(来电和去电)
来源:互联网 发布:安装mysql步骤 编辑:程序博客网 时间:2024/04/30 09:57
http://www.pocketdigi.com/20110725/417.html
PS: 注意代码中直接通过broadcast注册监听器,没有像“小米锁屏代码”通过service注册监听器。
今天的程序可以实现电话状态改变时启动(来电、接听、挂断、拨打电话),但是暂时没法实现拨打电话时判断对方是否接听、转语音信箱等。Android在电话状态改变是会发送action为android.intent.action.PHONE_STATE的广播,而拨打电话时会发送action为android.intent.action.NEW_OUTGOING_CALL的广播,但是我看了下开发文档,暂时没发现有来电时的广播。知道这个就好办了,我们写个BroadcastReceiver用于接收这两个广播就可以了。
package com.pocketdigi.phonelistener; import android.app.Service;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.telephony.PhoneStateListener;import android.telephony.TelephonyManager; public class PhoneReceiver extends BroadcastReceiver { @Overridepublic void onReceive(Context context, Intent intent) {// TODO Auto-generated method stubSystem.out.println("action"+intent.getAction());if(intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL)){//如果是去电(拨出)System.out.println("拨出");}else{//查了下android文档,貌似没有专门用于接收来电的action,所以,非去电即来电System.out.println("来电");TelephonyManager tm = (TelephonyManager)context.getSystemService(Service.TELEPHONY_SERVICE); tm.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);//设置一个监听器}}PhoneStateListener listener=new PhoneStateListener(){ @Overridepublic void onCallStateChanged(int state, String incomingNumber) {// TODO Auto-generated method stub//state 当前状态 incomingNumber,貌似没有去电的APIsuper.onCallStateChanged(state, incomingNumber);switch(state){case TelephonyManager.CALL_STATE_IDLE:System.out.println("挂断");break;case TelephonyManager.CALL_STATE_OFFHOOK:System.out.println("接听");break;case TelephonyManager.CALL_STATE_RINGING:System.out.println("响铃:来电号码"+incomingNumber);//输出来电号码break;}} };}
要在AndroidManifest.xml注册广播接收器:
<receiver android:name=".PhoneReceiver"> <intent-filter> <action android:name="android.intent.action.PHONE_STATE"/><action android:name="android.intent.action.NEW_OUTGOING_CALL" /> </intent-filter> </receiver>
还要添加权限:
<uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission> <uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"></uses-permission>
- android 电话状态的监听(来电和去电)
- android 电话状态的监听(来电和去电)
- android 电话状态的监听(来电和去电)
- android 电话状态的监听(来电和去电)
- android 电话状态的监听(来电和去电)
- android 电话状态的监听(来电和去电)
- android 电话状态的监听(来电和去电)
- android 电话状态的监听(来电和去电)
- android 电话状态的监听(来电和去电)
- android 电话状态的监听(来电和去电) PhoneStateListener和TelephonyManager
- android 电话状态的监听(来电和去电) PhoneStateListener和TelephonyManager
- android 电话状态的监听(来电和去电) PhoneStateListener和TelephonyManager
- Android之电话状态的监听(来电和去电)
- 【android】 监听去电和来电电话状态
- android 电话状态监听(来电和去电)实现代码
- android 电话状态监听(来电和去电)实现代码
- Android来电监听和去电监听
- Android来电监听和去电监听
- .net的组件
- HTML to Image in C#(C#实现Html转换为Image,即网页截图)
- 通过使用resetlog恢复控制文件恢复数据库
- 查询Oracle使用的特性
- Perl I/O Read & Write
- android 电话状态的监听(来电和去电)
- 简介Spring中常用的hql查询方法
- 注册JDBC驱动的三种方式
- 最简单的菜单导航--不兼容IE6
- 虚拟文件路径,轻松搞定某些软件对特殊文件路径不能识别问题
- 黑马程序员——泛型的总结
- sql server例子处理
- android代码格式化方法小结
- sed for <AAA>VALUE</AAA>