android 电话状态的监听(来电和去电) PhoneStateListener和TelephonyManager
来源:互联网 发布:win7网络修复大师 编辑:程序博客网 时间:2024/06/10 07:44
今天的程序可以实现电话状态改变时启动(来电、接听、挂断、拨打电话),但是暂时没法实现拨打电话时判断对方是否接听、转语音信箱等。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;
-
- publicclassPhoneReceiverextendsBroadcastReceiver{
-
- @Override
- publicvoid onReceive(Context context,Intent intent){
- // TODO Auto-generated method stub
- System.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=newPhoneStateListener(){
-
- @Override
- publicvoid onCallStateChanged(int state,String incomingNumber){
- // TODO Auto-generated method stub
- //state 当前状态 incomingNumber,貌似没有去电的API
- super.onCallStateChanged(state, incomingNumber);
- switch(state){
- caseTelephonyManager.CALL_STATE_IDLE:
- System.out.println("挂断");
- break;
- caseTelephonyManager.CALL_STATE_OFFHOOK:
- System.out.println("接听");
- break;
- caseTelephonyManager.CALL_STATE_RINGING:
- System.out.println("响铃:来电号码"+incomingNumber);
- //输出来电号码
- break;
- }
- }
- };
- }
要在AndroidManifest.xml注册广播接收器:- <receiverandroid:name=".PhoneReceiver">
- <intent-filter>
- <actionandroid:name="android.intent.action.PHONE_STATE"/>
- <actionandroid:name="android.intent.action.NEW_OUTGOING_CALL"/>
- </intent-filter>
- </receiver>
还要添加权限:- <uses-permissionandroid:name="android.permission.READ_PHONE_STATE"></uses-permission>
- <uses-permissionandroid:name="android.permission.PROCESS_OUTGOING_CALLS"></uses-permission>
- 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;
- publicclassPhoneReceiverextendsBroadcastReceiver{
- @Override
- publicvoid onReceive(Context context,Intent intent){
- // TODO Auto-generated method stub
- System.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=newPhoneStateListener(){
- @Override
- publicvoid onCallStateChanged(int state,String incomingNumber){
- // TODO Auto-generated method stub
- //state 当前状态 incomingNumber,貌似没有去电的API
- super.onCallStateChanged(state, incomingNumber);
- switch(state){
- caseTelephonyManager.CALL_STATE_IDLE:
- System.out.println("挂断");
- break;
- caseTelephonyManager.CALL_STATE_OFFHOOK:
- System.out.println("接听");
- break;
- caseTelephonyManager.CALL_STATE_RINGING:
- System.out.println("响铃:来电号码"+incomingNumber);
- //输出来电号码
- break;
- }
- }
- };
- }
- <receiverandroid:name=".PhoneReceiver">
- <intent-filter>
- <actionandroid:name="android.intent.action.PHONE_STATE"/>
- <actionandroid:name="android.intent.action.NEW_OUTGOING_CALL"/>
- </intent-filter>
- </receiver>
- <uses-permissionandroid:name="android.permission.READ_PHONE_STATE"></uses-permission>
- <uses-permissionandroid:name="android.permission.PROCESS_OUTGOING_CALLS"></uses-permission>
0 0
- android 电话状态的监听(来电和去电) PhoneStateListener和TelephonyManager
- android 电话状态的监听(来电和去电) PhoneStateListener和TelephonyManager
- android 电话状态的监听(来电和去电) PhoneStateListener和TelephonyManager
- android 电话状态的监听(来电和去电)
- android 电话状态的监听(来电和去电)
- android 电话状态的监听(来电和去电)
- android 电话状态的监听(来电和去电)
- android 电话状态的监听(来电和去电)
- android 电话状态的监听(来电和去电)
- android 电话状态的监听(来电和去电)
- android 电话状态的监听(来电和去电)
- android 电话状态的监听(来电和去电)
- 【android】 监听去电和来电电话状态
- android 电话状态监听(来电和去电)实现代码
- android 电话状态监听(来电和去电)实现代码
- Android之电话状态的监听(来电和去电)
- Android来电监听和去电监听
- Android来电监听和去电监听
- ubuntu 12.04 密码输入正确但无法进入图形化界面
- 打开myeclipse 里面有些类报错 再删个空格再保存就ok
- asp.net 最简单的MD5加密方式
- Java中Runnable和Thread的区别
- eclipse开发jsp环境的插件
- android 电话状态的监听(来电和去电) PhoneStateListener和TelephonyManager
- 背景音乐
- 解决考试系统高并发数据加载不正确问题
- 关于C#操作PPT时遇到的“对COM组件的调用返回了错误HRESULT E_FAIL”错误的解决方法
- Navicat for mysql连接数据库 出现1130错误
- LeetCode | Longest Substring Without Repeating Characters
- vc 线程的使用
- 从ARM到CORTEX----4412开发板的超强性能,无可挑剔
- tomcat设置https请求步骤(单向验证)