当前位置: 移动技术网 > 移动技术>移动开发>Android > android 电话状态监听(来电和去电)实现代码

android 电话状态监听(来电和去电)实现代码

2019年07月24日  | 移动技术网移动技术  | 我要评论
实现手机电话状态的监听,主要依靠两个类:telephonemanger和phonestatelistener。
telephonsemanger提供了取得手机基本服务的信息的一种方式。因此应用程序可以使用telephonymanager来探测手机基本服务的情况。应用程序可以注册listener来监听电话状态的改变。我们不能对telephonymanager进行实例化,只能通过获取服务的形式:
context.getsystemservice(context.telephony_service);
注意:对手机的某些信息进行读取是需要一定许可(permission)的。
主要静态成员常量:(它们对应phonestatelistener.listen_call_state所监听到的内容)
int call_state_idle 空闲状态,没有任何活动。
int call_state_offhook 摘机状态,至少有个电话活动。该活动或是拨打(dialing)或是通话,或是 on hold。并且没有电话是ringing or waiting
int call_state_ringing 来电状态,电话铃声响起的那段时间或正在通话又来新电,新来电话不得不等待的那段时间。
手机通话状态在广播中的对应值
extra_state_idle 它在手机通话状态改变的广播中,用于表示call_state_idle状态
extra_state_offhook 它在手机通话状态改变的广播中,用于表示call_state_offhook状态
extra_state_ringing 它在手机通话状态改变的广播中,用于表示call_state_ringing状态
action_phone_state_changed 在广播中用action_phone_state_changed这个action来标示通话状态改变的广播(intent)。
注:需要许可read_phone_state。
string extra_incoming_number
在手机通话状态改变的广播,用于从extra取来电号码。
string extra_state 在通话状态改变的广播,用于从extra取来通话状态。

主要成员函数
public int getcallstate() 取得手机的通话状态。
public celllocation getcelllocation () 返回手机当前所处的位置。如果当前定位服务不可用,则返回null
注:需要许可(permission)access_coarse_location or access_fine_location.
public int getdataactivity () 返回当前数据连接活动状态的情况。
public int getdatastate () 返回当前数据连接状态的情况。
public string getdeviceid ()
返回手机的设备id。比如对于gsm的手机来说是imei码,对于cdma的手机来说meid码或esn码。如果读取失败,则返回null。

如何实现电话状态的监听呢?
android在电话状态改变是会发送action为android.intent.action.phone_state的广播,而拨打电话时会发送action为android.intent.action.new_outgoing_call的广播,但是我看了下开发文档,暂时没发现有来电时的广播。通过自定义广播接收器,接受上述两个广播便可。
java代码:
复制代码 代码如下:

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 {
@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;
}
}
};
}

要在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>
<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>

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网