android开发怎么监控来电并监听

 我来答
厦门侯
高粉答主

推荐于2018-04-18 · 信息管理员
厦门侯
采纳数:30618 获赞数:90296

向TA提问 私信TA
展开全部
实现如下广播,并且在AndroidManifest中进行注册

public class PhoneReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
System.out.println("action"+intent.getAction());
//如果是去电
if(intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL)){
String phoneNumber = intent
.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
Log.d(TAG, "call OUT:" + phoneNumber);
}else{
//查了下android文档,貌似没有专门用于接收来电的action,所以,非去电即来电.
//如果我们想要监听电话的拨打状况,需要这么几步 :
* 第一:获取电话服务管理器TelephonyManager manager = this.getSystemService(TELEPHONY_SERVICE);
* 第二:通过TelephonyManager注册我们要监听的电话状态改变事件。manager.listen(new MyPhoneStateListener(),
* PhoneStateListener.LISTEN_CALL_STATE);这里的PhoneStateListener.LISTEN_CALL_STATE就是我们想要
* 监听的状态改变事件,初次之外,还有很多其他事件哦。
* 第三步:通过extends PhoneStateListener来定制自己的规则。将其对象传递给第二步作为参数。
* 第四步:这一步很重要,那就是给应用添加权限。android.permission.READ_PHONE_STATE

TelephonyManager tm = (TelephonyManager)context.getSystemService(Service.TELEPHONY_SERVICE);
tm.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);
//设置一个监听器
}
}
PhoneStateListener listener=new PhoneStateListener(){

@Override
public void onCallStateChanged(int state, String incomingNumber) {
//注意,方法必须写在super方法后面,否则incomingNumber无法获取到值。
super.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;
}
}
深圳市容大彩晶科技有限公司
2024-11-15 广告
广告机代理优选容大彩晶,深圳市容大彩晶科技有限公司自2007年成立伊始,一直专注于液晶商用显示产品的技术研发及产品服务。公司依托具有自主研发能力、创新高效的技术团队,打造出行业优质商用液晶显示设备。容大彩晶产品涵盖商用液晶显示、多媒体广告机... 点击进入详情页
本回答由深圳市容大彩晶科技有限公司提供
徐小小天
推荐于2016-11-04 · TA获得超过1453个赞
知道小有建树答主
回答量:839
采纳率:83%
帮助的人:768万
展开全部
权限
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
方式一:通过广播接收来电

定义来电广播接收类

Java代码
package com.zhouzijing.android.demo;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.TelephonyManager;
import android.util.Log;

public class BroadcastReceiverMgr extends BroadcastReceiver {

private final String TAG = MyBroadcastReceiver.TAG;

@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
Log.i(TAG, "[Broadcast]"+action);

//呼入电话
if(action.equals(MyBroadcastReceiver.B_PHONE_STATE)){
Log.i(TAG, "[Broadcast]PHONE_STATE");
doReceivePhone(context,intent);
}
}

/**
* 处理电话广播.
* @param context
* @param intent
*/
public void doReceivePhone(Context context, Intent intent) {
String phoneNumber = intent.getStringExtra(
TelephonyManager.EXTRA_INCOMING_NUMBER);
TelephonyManager telephony =
(TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
int state = telephony.getCallState();
switch(state){
case TelephonyManager.CALL_STATE_RINGING:
Log.i(TAG, "[Broadcast]等待接电话="+phoneNumber);
break;
case TelephonyManager.CALL_STATE_IDLE:
Log.i(TAG, "[Broadcast]电话挂断="+phoneNumber);
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
Log.i(TAG, "[Broadcast]通话中="+phoneNumber);
break;
}
}
}

定义Actitvity类
Java代码
package com.zhouzijing.android.demo;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.View;

public class MyBroadcastReceiver extends Activity {
public final static String TAG = "MyBroadcastReceiver";

public final static String B_PHONE_STATE =
TelephonyManager.ACTION_PHONE_STATE_CHANGED;

private BroadcastReceiverMgr mBroadcastReceiver;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_broadcast_receiver);
}

//按钮1-注册广播
public void registerIt(View v) {
Log.i(TAG, "registerIt");
mBroadcastReceiver = new BroadcastReceiverMgr();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(B_PHONE_STATE);
intentFilter.setPriority(Integer.MAX_VALUE);
registerReceiver(mBroadcastReceiver, intentFilter);
}

//按钮2-撤销广播
public void unregisterIt(View v) {
Log.i(TAG, "unregisterIt");
unregisterReceiver(mBroadcastReceiver);
}

}

方式二:通过监听器来实现

Java代码
package com.zhouzijing.android.demo;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.View;

public class MyBroadcastReceiver extends Activity {
public final static String TAG = "MyBroadcastReceiver";

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_broadcast_receiver);
}

/**
* 按钮-监听电话
* @param v
*/
public void createPhoneListener(View v) {
TelephonyManager telephony = (TelephonyManager)getSystemService(
Context.TELEPHONY_SERVICE);
telephony.listen(new OnePhoneStateListener(),
PhoneStateListener.LISTEN_CALL_STATE);
}

/**
* 电话状态监听.
* @author stephen
*
*/
class OnePhoneStateListener extends PhoneStateListener{
@Override
public void onCallStateChanged(int state, String incomingNumber) {
Log.i(TAG, "[Listener]电话号码:"+incomingNumber);
switch(state){
case TelephonyManager.CALL_STATE_RINGING:
Log.i(TAG, "[Listener]等待接电话:"+incomingNumber);
break;
case TelephonyManager.CALL_STATE_IDLE:
Log.i(TAG, "[Listener]电话挂断:"+incomingNumber);
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
Log.i(TAG, "[Listener]通话中:"+incomingNumber);
break;
}
super.onCallStateChanged(state, incomingNumber);
}
}

}
本回答被提问者和网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式