Android 实现电话来去自动录音的功能
我们在使用Android手机打电话时,有时可能会需要对来去电通话自动录音,本文就详细讲解实现Android来去电通话自动录音的方法,大家按照文中的方法编写程序就可以完成此功能。
来去电自动录音的关键在于如何监听手机电话状态的转变:
1)来电的状态的转换如下(红色标记是我们要用到的状态)
空闲(IDEL)——>响铃(RINGING)——>接听(ACTIVE)——>挂断(经历DISCONNECTING——DISCONNECTED)——>空闲(IDEL)
或者 空闲(IDEL)——>响铃(RINGING)——>拒接——>空闲(IDEL)
2)去电状态的转换如下
空闲(IDEL)——>拨号(DIALING)——>(对方)响铃(ALERTING)——>建立连接(ACTIVE)——挂断(经历DISCONNECTING——DISCONNECTED)——>空闲(IDEL)
或者空闲(IDEL)——>拨号(DIALING)——>(对方)响铃(ALERTING)——>挂断/对方拒接——>空闲(IDEL)
下面就分别就来电和去电这两种状态分析并实现。
1、先进行来电的分析和实现。
相对去电来说,来电状态的转换检测要简单些。androidapi中的PhoneStateListener类提供了相应的方法,但我们需要覆盖其中的onCallStateChanged(intstate,StringincomingNumber)方法即可实现来电状态的检测,并在此基础上添加录音功能即可。其中state参数就是各种电话状态,到时我们将它跟下面我们要用到的状态进行比较,若是电话处在我们想要的状态上,则进行一系列操作,否则就不管他。想要获取这些状态,还需要另一个电话相关类,那就是TelephonyManager,该类提供了一些电话状态,其中我们要用到的是:TelephonyManager.CALL_STATE_IDLE(空闲)、TelephonyManager.CALL_STATE_OFFHOOK(摘机)和TelephonyManager.CALL_STATE_RINGING(来电响铃)这三个状态。判别这三种状态,可以继承android.telephony.PhoneStateListener类,实现上面提到的onCallStateChanged(intstate,StringincomingNumber)方法,请看如下代码:
publicclassTelListenerextendsPhoneStateListener{
@Override
publicvoidonCallStateChanged(intstate,StringincomingNumber){
super.onCallStateChanged(state,incomingNumber);
switch(state){
caseTelephonyManager.CALL_STATE_IDLE://空闲状态,即无来电也无去电
Log.i("TelephoneState","IDLE");
//此处添加一系列功能代码
break;
caseTelephonyManager.CALL_STATE_RINGING://来电响铃
Log.i("TelephoneState","RINGING");
//此处添加一系列功能代码
break;
caseTelephonyManager.CALL_STATE_OFFHOOK://摘机,即接通
Log.i("TelephoneState","OFFHOOK");
//此处添加一系列功能代码
break;
}
Log.i("TelephoneState",String.valueOf(incomingNumber));
}
}
有了以上来电状态监听代码还不足以实现监听功能,还需要在我们的一个Activity或者Service中实现监听,方法很简单,代码如下:
/** *在activity或者service中加入如下代码,以实现来电状态监听 */ TelephonyManagertelMgr=(TelephonyManager)context.getSystemService( Context.TELEPHONY_SERVICE); telMgr.listen(newTelListener(),PhoneStateListener.LISTEN_CALL_STATE);
这样就实现了来电状态监听功能,但要能够在设备中跑起来,这还不够,它还需要两个获取手机电话状态的权限:
<uses-permissionandroid:name="android.permission.READ_PHONE_STATE"/> <uses-permissionandroid:name="android.permission.PROCESS_OUTGOING_CALLS"/>
这样的话就可以跑起来了。
说到这,我想如果你可以实现录音功能的话,在此基础上实现来电自动录音就应该没什么问题了,不过请容我简单罗嗦几句。既然是来电,那么要想录音的话,那么应该就是在监听到TelephonyManager.CALL_STATE_OFFHOOK的状态时开启录音机开始录音,在监听到TelephonyManager.CALL_STATE_IDLE的状态时关闭录音机停止录音。这样,来电录音功能就完成了,不要忘记录音功能同样需要权限:
<uses-permissionandroid:name="android.permission.RECORD_AUDIO"/> <!--要存储文件或者创建文件夹的话还需要以下两个权限--> <uses-permissionandroid:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/> <uses-permissionandroid:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
2、介绍完了来电自动录音,下面就来介绍去电自动录音的实现方法。
上面说过,相比来电状态的监听,去电的要麻烦些,甚至这种方法不是通用的,这个主要是因为androidapi中没有提供去电状态监听的相应类和方法(也许我刚接触,没有找到)。刚开始网上搜索了一通也没有找到对应的解决方法,大多是来电监听的,也就是上面的方法。不过中途发现一篇博文(后来就搜不到了),记得是查询系统日志的方式,从中找到去电过程中的各个状态的关键词。无奈之中,最终妥协了此方法。
我的(联想A65上的)去电日志内容如下:
过滤关键词为mforeground
01-0616:29:54.225:D/InCallScreen(251):onPhoneStateChanged:mForegroundCall.getState():DIALING 01-0616:29:54.245:V/LogInfoOutGoingCall(2492):D/InCallScreen(251):onPhoneStateChanged:mForegroundCall.getState():DIALING 01-0616:29:54.631:D/InCallScreen(251):onPhoneStateChanged:mForegroundCall.getState():DIALING 01-0616:29:54.645:V/LogInfoOutGoingCall(2492):D/InCallScreen(251):onPhoneStateChanged:mForegroundCall.getState():DIALING 01-0616:29:54.742:D/InCallScreen(251):onPhoneStateChanged:mForegroundCall.getState():DIALING 01-0616:29:54.766:V/LogInfoOutGoingCall(2492):D/InCallScreen(251):onPhoneStateChanged:mForegroundCall.getState():DIALING 01-0616:29:54.873:D/InCallScreen(251):onPhoneStateChanged:mForegroundCall.getState():DIALING 01-0616:29:54.877:V/LogInfoOutGoingCall(2492):D/InCallScreen(251):onPhoneStateChanged:mForegroundCall.getState():DIALING 01-0616:29:55.108:D/InCallScreen(251):onPhoneStateChanged:mForegroundCall.getState():DIALING 01-0616:29:55.125:V/LogInfoOutGoingCall(2492):D/InCallScreen(251):onPhoneStateChanged:mForegroundCall.getState():DIALING 01-0616:29:57.030:D/InCallScreen(251):onPhoneStateChanged:mForegroundCall.getState():ACTIVE 01-0616:29:57.155:V/LogInfoOutGoingCall(2492):D/InCallScreen(251):onPhoneStateChanged:mForegroundCall.getState():ACTIVE 01-0616:29:57.480:D/InCallScreen(251):onPhoneStateChanged:mForegroundCall.getState():ACTIVE 01-0616:29:57.598:V/LogInfoOutGoingCall(2492):D/InCallScreen(251):onPhoneStateChanged:mForegroundCall.getState():ACTIVE 01-0616:29:59.319:D/InCallScreen(251):onPhoneStateChanged:mForegroundCall.getState():DISCONNECTING 01-0616:29:59.373:V/LogInfoOutGoingCall(2492):D/InCallScreen(251):onPhoneStateChanged:mForegroundCall.getState():DISCONNECTING 01-0616:30:00.392:D/InCallScreen(251):-onDisconnect:currentlyIdle:true;mForegroundCall.getState():DISCONNECTED 01-0616:30:00.399:V/LogInfoOutGoingCall(2492):D/InCallScreen(251):-onDisconnect:currentlyIdle:true;mForegroundCall.getState():DISCONNECTED 01-0616:30:01.042:D/InCallScreen(251):onPhoneStateChanged:mForegroundCall.getState():IDLE 01-0616:30:01.070:V/LogInfoOutGoingCall(2492):D/InCallScreen(251):onPhoneStateChanged:mForegroundCall.getState():IDLE 01-0616:30:01.558:D/InCallScreen(251):onPhoneStateChanged:mForegroundCall.getState():IDLE 01-0616:30:01.572:V/LogInfoOutGoingCall(2492):D/InCallScreen(251):onPhoneStateChanged:mForegroundCall.getState():IDLE
过滤关键词 mbackground
01-0616:29:54.226:D/InCallScreen(251):onPhoneStateChanged:mBackgroundCall.getState():IDLE 01-0616:29:54.256:V/LogInfoOutGoingCall(2492):D/InCallScreen(251):onPhoneStateChanged:mBackgroundCall.getState():IDLE 01-0616:29:54.638:D/InCallScreen(251):onPhoneStateChanged:mBackgroundCall.getState():IDLE 01-0616:29:54.652:V/LogInfoOutGoingCall(2492):D/InCallScreen(251):onPhoneStateChanged:mBackgroundCall.getState():IDLE 01-0616:29:54.743:D/InCallScreen(251):onPhoneStateChanged:mBackgroundCall.getState():IDLE 01-0616:29:54.770:V/LogInfoOutGoingCall(2492):D/InCallScreen(251):onPhoneStateChanged:mBackgroundCall.getState():IDLE 01-0616:29:54.875:D/InCallScreen(251):onPhoneStateChanged:mBackgroundCall.getState():IDLE 01-0616:29:54.882:V/LogInfoOutGoingCall(2492):D/InCallScreen(251):onPhoneStateChanged:mBackgroundCall.getState():IDLE 01-0616:29:55.109:D/InCallScreen(251):onPhoneStateChanged:mBackgroundCall.getState():IDLE 01-0616:29:55.142:V/LogInfoOutGoingCall(2492):D/InCallScreen(251):onPhoneStateChanged:mBackgroundCall.getState():IDLE 01-0616:29:57.031:D/InCallScreen(251):onPhoneStateChanged:mBackgroundCall.getState():IDLE 01-0616:29:57.160:V/LogInfoOutGoingCall(2492):D/InCallScreen(251):onPhoneStateChanged:mBackgroundCall.getState():IDLE 01-0616:29:57.481:D/InCallScreen(251):onPhoneStateChanged:mBackgroundCall.getState():IDLE 01-0616:29:57.622:V/LogInfoOutGoingCall(2492):D/InCallScreen(251):onPhoneStateChanged:mBackgroundCall.getState():IDLE 01-0616:29:59.319:D/InCallScreen(251):onPhoneStateChanged:mBackgroundCall.getState():IDLE 01-0616:29:59.373:V/LogInfoOutGoingCall(2492):D/InCallScreen(251):onPhoneStateChanged:mBackgroundCall.getState():IDLE 01-0616:30:01.042:D/InCallScreen(251):onPhoneStateChanged:mBackgroundCall.getState():IDLE 01-0616:30:01.070:V/LogInfoOutGoingCall(2492):D/InCallScreen(251):onPhoneStateChanged:mBackgroundCall.getState():IDLE 01-0616:30:01.559:D/InCallScreen(251):onPhoneStateChanged:mBackgroundCall.getState():IDLE 01-0616:30:01.573:V/LogInfoOutGoingCall(2492):D/InCallScreen(251):onPhoneStateChanged:mBackgroundCall.getState():IDLE
从上面的日志可以看到,每一行的末尾的大写英文词就是去电的状态,状态说明如下:
- DIALING拨号,对方还未响铃
- ACTIVE 对方接通,通话建立
- DISCONNECTING通话断开时
- DISCONNECTED 通话已断开,可以认为是挂机了
由于我拨打的是10010,没有响铃过程(电脑自动接通的够快),还少了一个状态,状态是ALERTING,这个就是对方正在响铃的状态。
有了这几个去电状态就好办了,现在我们要做的就是读取系统日志,然后找到这些状态,提取的关键词就是上面提到的mforeground(前台通话状态)和mbackground(后台通话状态)(可能不一样的设备生成的不一样,根据自己具体设备设置,这里只提取前台的),如果读取的这一行日志中包含mforground,再看看是否包含上面的状态的单词。既然说的如此,那么看看读取系统日志的代码吧。
packagecom.sdvdxl.phonerecorder;
importjava.io.BufferedReader;
importjava.io.IOException;
importjava.io.InputStream;
importjava.io.InputStreamReader;
importcom.sdvdxl.outgoingcall.OutgoingCallState;
importandroid.content.Context;
importandroid.content.Intent;
importandroid.util.Log;
/**
*
*@authorsdvdxl
*找到日志中的
*onPhoneStateChanged:mForegroundCall.getState()这个是前台呼叫状态
*mBackgroundCall.getState()后台电话
*若是DIALING则是正在拨号,等待建立连接,但对方还没有响铃,
*ALERTING呼叫成功,即对方正在响铃,
*若是ACTIVE则已经接通
*若是DISCONNECTED则本号码呼叫已经挂断
*若是IDLE则是处于空闲状态
*
*/
publicclassReadLogextendsThread{
privateContextctx;
privateintlogCount;
privatestaticfinalStringTAG="LogInfoOutGoingCall";
/**
*前后台电话
*@authorsdvdxl
*
*/
privatestaticclassCallViewState{
publicstaticfinalStringFORE_GROUND_CALL_STATE="mForeground";
}
/**
*呼叫状态
*@authorsdvdxl
*
*/
privatestaticclassCallState{
publicstaticfinalStringDIALING="DIALING";
publicstaticfinalStringALERTING="ALERTING";
publicstaticfinalStringACTIVE="ACTIVE";
publicstaticfinalStringIDLE="IDLE";
publicstaticfinalStringDISCONNECTED="DISCONNECTED";
}
publicReadLog(Contextctx){
this.ctx=ctx;
}
/**
*读取Log流
*取得呼出状态的log
*从而得到转换状态
*/
@Override
publicvoidrun(){
Log.d(TAG,"开始读取日志记录");
String[]catchParams={"logcat","InCallScreen*:s"};
String[]clearParams={"logcat","-c"};
try{
Processprocess=Runtime.getRuntime().exec(catchParams);
InputStreamis=process.getInputStream();
BufferedReaderreader=newBufferedReader(newInputStreamReader(is));
Stringline=null;
while((line=reader.readLine())!=null){
logCount++;
//输出所有
Log.v(TAG,line);
//日志超过512条就清理
if(logCount>512){
//清理日志
Runtime.getRuntime().exec(clearParams)
.destroy();//销毁进程,释放资源
logCount=0;
Log.v(TAG,"-----------清理日志---------------");
}
/*---------------------------------前台呼叫-----------------------*/
//空闲
if(line.contains(ReadLog.CallViewState.FORE_GROUND_CALL_STATE)
&&line.contains(ReadLog.CallState.IDLE)){
Log.d(TAG,ReadLog.CallState.IDLE);
}
//正在拨号,等待建立连接,即已拨号,但对方还没有响铃,
if(line.contains(ReadLog.CallViewState.FORE_GROUND_CALL_STATE)
&&line.contains(ReadLog.CallState.DIALING)){
Log.d(TAG,ReadLog.CallState.DIALING);
}
//呼叫对方正在响铃
if(line.contains(ReadLog.CallViewState.FORE_GROUND_CALL_STATE)
&&line.contains(ReadLog.CallState.ALERTING)){
Log.d(TAG,ReadLog.CallState.ALERTING);
}
//已接通,通话建立
if(line.contains(ReadLog.CallViewState.FORE_GROUND_CALL_STATE)
&&line.contains(ReadLog.CallState.ACTIVE)){
Log.d(TAG,ReadLog.CallState.ACTIVE);
}
//断开连接,即挂机
if(line.contains(ReadLog.CallViewState.FORE_GROUND_CALL_STATE)
&&line.contains(ReadLog.CallState.DISCONNECTED)){
Log.d(TAG,ReadLog.CallState.DISCONNECTED);
}
}//ENDwhile
}catch(IOExceptione){
e.printStackTrace();
}//ENDtry-catch
}//ENDrun
}//ENDclassReadLog
以上代码中,之所以用线程,是为了防止读取日志过程中阻滞主方法的其他方法的执行,影响到程序捕捉对应的电话状态。
好了,捕捉到了去电过程中各个状态的转变,那么,如何通知给程序呢,我采用的方法是捕获后立马给系统发送广播,然后程序进行广播接收,接收后再处理录音事件。要发送广播,就要发送一个唯一的广播,为此,建立如下类:
packagecom.sdvdxl.outgoingcall;
importcom.sdvdxl.phonerecorder.ReadLog;
importandroid.content.Context;
importandroid.util.Log;
publicclassOutgoingCallState{
Contextctx;
publicOutgoingCallState(Contextctx){
this.ctx=ctx;
}
/**
*前台呼叫状态
*@authorsdvdxl
*
*/
publicstaticfinalclassForeGroundCallState{
publicstaticfinalStringDIALING=
"com.sdvdxl.phonerecorder.FORE_GROUND_DIALING";
publicstaticfinalStringALERTING=
"com.sdvdxl.phonerecorder.FORE_GROUND_ALERTING";
publicstaticfinalStringACTIVE=
"com.sdvdxl.phonerecorder.FORE_GROUND_ACTIVE";
publicstaticfinalStringIDLE=
"com.sdvdxl.phonerecorder.FORE_GROUND_IDLE";
publicstaticfinalStringDISCONNECTED=
"com.sdvdxl.phonerecorder.FORE_GROUND_DISCONNECTED";
}
/**
*开始监听呼出状态的转变,
*并在对应状态发送广播
*/
publicvoidstartListen(){
newReadLog(ctx).start();
Log.d("Recorder","开始监听呼出状态的转变,并在对应状态发送广播");
}
}
程序需要读取系统日志权限:
XML/HTML代码
<uses-permissionandroid:name="android.permission.READ_LOGS"/>
然后,在读取日志的类中检测到去电各个状态的地方发送一个广播,那么,读取日志的完整代码如下:
packagecom.sdvdxl.phonerecorder;
importjava.io.BufferedReader;
importjava.io.IOException;
importjava.io.InputStream;
importjava.io.InputStreamReader;
importcom.sdvdxl.outgoingcall.OutgoingCallState;
importandroid.content.Context;
importandroid.content.Intent;
importandroid.util.Log;
/**
*
*@authormrloong
*找到日志中的
*onPhoneStateChanged:mForegroundCall.getState()这个是前台呼叫状态
*mBackgroundCall.getState()后台电话
*若是DIALING则是正在拨号,等待建立连接,但对方还没有响铃,
*ALERTING呼叫成功,即对方正在响铃,
*若是ACTIVE则已经接通
*若是DISCONNECTED则本号码呼叫已经挂断
*若是IDLE则是处于空闲状态
*
*/
publicclassReadLogextendsThread{
privateContextctx;
privateintlogCount;
privatestaticfinalStringTAG="LogInfoOutGoingCall";
/**
*前后台电话
*@authorsdvdxl
*
*/
privatestaticclassCallViewState{
publicstaticfinalStringFORE_GROUND_CALL_STATE="mForeground";
}
/**
*呼叫状态
*@authorsdvdxl
*
*/
privatestaticclassCallState{
publicstaticfinalStringDIALING="DIALING";
publicstaticfinalStringALERTING="ALERTING";
publicstaticfinalStringACTIVE="ACTIVE";
publicstaticfinalStringIDLE="IDLE";
publicstaticfinalStringDISCONNECTED="DISCONNECTED";
}
publicReadLog(Contextctx){
this.ctx=ctx;
}
/**
*读取Log流
*取得呼出状态的log
*从而得到转换状态
*/
@Override
publicvoidrun(){
Log.d(TAG,"开始读取日志记录");
String[]catchParams={"logcat","InCallScreen*:s"};
String[]clearParams={"logcat","-c"};
try{
Processprocess=Runtime.getRuntime().exec(catchParams);
InputStreamis=process.getInputStream();
BufferedReaderreader=newBufferedReader(newInputStreamReader(is));
Stringline=null;
while((line=reader.readLine())!=null){
logCount++;
//输出所有
Log.v(TAG,line);
//日志超过512条就清理
if(logCount>512){
//清理日志
Runtime.getRuntime().exec(clearParams)
.destroy();//销毁进程,释放资源
logCount=0;
Log.v(TAG,"-----------清理日志---------------");
}
/*---------------------------------前台呼叫-----------------------*/
//空闲
if(line.contains(ReadLog.CallViewState.FORE_GROUND_CALL_STATE)
&&line.contains(ReadLog.CallState.IDLE)){
Log.d(TAG,ReadLog.CallState.IDLE);
}
//正在拨号,等待建立连接,即已拨号,但对方还没有响铃,
if(line.contains(ReadLog.CallViewState.FORE_GROUND_CALL_STATE)
&&line.contains(ReadLog.CallState.DIALING)){
//发送广播
IntentdialingIntent=newIntent();
dialingIntent.setAction(OutgoingCallState.ForeGroundCallState.DIALING);
ctx.sendBroadcast(dialingIntent);
Log.d(TAG,ReadLog.CallState.DIALING);
}
//呼叫对方正在响铃
if(line.contains(ReadLog.CallViewState.FORE_GROUND_CALL_STATE)
&&line.contains(ReadLog.CallState.ALERTING)){
//发送广播
IntentdialingIntent=newIntent();
dialingIntent.setAction(OutgoingCallState.ForeGroundCallState.ALERTING);
ctx.sendBroadcast(dialingIntent);
Log.d(TAG,ReadLog.CallState.ALERTING);
}
//已接通,通话建立
if(line.contains(ReadLog.CallViewState.FORE_GROUND_CALL_STATE)
&&line.contains(ReadLog.CallState.ACTIVE)){
//发送广播
IntentdialingIntent=newIntent();
dialingIntent.setAction(OutgoingCallState.ForeGroundCallState.ACTIVE);
ctx.sendBroadcast(dialingIntent);
Log.d(TAG,ReadLog.CallState.ACTIVE);
}
//断开连接,即挂机
if(line.contains(ReadLog.CallViewState.FORE_GROUND_CALL_STATE)
&&line.contains(ReadLog.CallState.DISCONNECTED)){
//发送广播
IntentdialingIntent=newIntent();
dialingIntent.setAction(OutgoingCallState.ForeGroundCallState.DISCONNECTED);
ctx.sendBroadcast(dialingIntent);
Log.d(TAG,ReadLog.CallState.DISCONNECTED);
}
}//ENDwhile
}catch(IOExceptione){
e.printStackTrace();
}//ENDtry-catch
}//ENDrun
}//ENDclassReadLog
发送了广播,那么就要有接收者,定义接收者如下(关于录音机的代码可以先忽略):
packagecom.sdvdxl.phonerecorder;
importandroid.content.BroadcastReceiver;
importandroid.content.Context;
importandroid.content.Intent;
importandroid.util.Log;
importcom.sdvdxl.outgoingcall.OutgoingCallState;
publicclassOutgoingCallReciverextendsBroadcastReceiver{
staticfinalStringTAG="Recorder";
privateMyRecorderrecorder;
publicOutgoingCallReciver(){
recorder=newMyRecorder();
}
publicOutgoingCallReciver(MyRecorderrecorder){
this.recorder=recorder;
}
@Override
publicvoidonReceive(Contextctx,Intentintent){
StringphoneState=intent.getAction();
if(phoneState.equals(Intent.ACTION_NEW_OUTGOING_CALL)){
StringphoneNum=intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);//拨出号码
recorder.setPhoneNumber(phoneNum);
recorder.setIsCommingNumber(false);
Log.d(TAG,"设置为去电状态");
Log.d(TAG,"去电状态呼叫:"+phoneNum);
}
if(phoneState.equals(OutgoingCallState.ForeGroundCallState.DIALING)){
Log.d(TAG,"正在拨号...");
}
if(phoneState.equals(OutgoingCallState.ForeGroundCallState.ALERTING)){
Log.d(TAG,"正在呼叫...");
}
if(phoneState.equals(OutgoingCallState.ForeGroundCallState.ACTIVE)){
if(!recorder.isCommingNumber()&&!recorder.isStarted()){
Log.d(TAG,"去电已接通启动录音机");
recorder.start();
}
}
if(phoneState.equals(OutgoingCallState.ForeGroundCallState.DISCONNECTED)){
if(!recorder.isCommingNumber()&&recorder.isStarted()){
Log.d(TAG,"已挂断关闭录音机");
recorder.stop();
}
}
}
}
其中有这么一段代码:
StringphoneState=intent.getAction();
if(phoneState.equals(Intent.ACTION_NEW_OUTGOING_CALL)){
StringphoneNum=intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);//拨出号码
recorder.setPhoneNumber(phoneNum);
recorder.setIsCommingNumber(false);
Log.d(TAG,"设置为去电状态");
Log.d(TAG,"去电状态呼叫:"+phoneNum);
}
这里是接收系统发出的广播,用于接收去电广播。这样,就获得了去电状态。
3、有了以上主要代码,可以说,来去电监听功能算是完成了,下面创建一个service来运行监听:
packagecom.sdvdxl.service;
importandroid.app.Service;
importandroid.content.Context;
importandroid.content.Intent;
importandroid.content.IntentFilter;
importandroid.os.IBinder;
importandroid.telephony.PhoneStateListener;
importandroid.telephony.TelephonyManager;
importandroid.util.Log;
importandroid.widget.Toast;
importcom.sdvdxl.outgoingcall.OutgoingCallState;
importcom.sdvdxl.phonerecorder.MyRecorder;
importcom.sdvdxl.phonerecorder.OutgoingCallReciver;
importcom.sdvdxl.phonerecorder.TelListener;
publicclassPhoneCallStateServiceextendsService{
privateOutgoingCallStateoutgoingCallState;
privateOutgoingCallReciveroutgoingCallReciver;
privateMyRecorderrecorder;
@Override
publicvoidonCreate(){
super.onCreate();
//------以下应放在onStartCommand中,但2.3.5以下版本不会因service重新启动而重新调用--------
//监听电话状态,如果是打入且接听或者打出则开始自动录音
//通话结束,保存文件到外部存储器上
Log.d("Recorder","正在监听中...");
recorder=newMyRecorder();
outgoingCallState=newOutgoingCallState(this);
outgoingCallReciver=newOutgoingCallReciver(recorder);
outgoingCallState.startListen();
Toast.makeText(this,"服务已启动",Toast.LENGTH_LONG).show();
//去电
IntentFilteroutgoingCallFilter=newIntentFilter();
outgoingCallFilter.addAction(OutgoingCallState.ForeGroundCallState.IDLE);
outgoingCallFilter.addAction(OutgoingCallState.ForeGroundCallState.DIALING);
outgoingCallFilter.addAction(OutgoingCallState.ForeGroundCallState.ALERTING);
outgoingCallFilter.addAction(OutgoingCallState.ForeGroundCallState.ACTIVE);
outgoingCallFilter.addAction(OutgoingCallState.ForeGroundCallState.DISCONNECTED);
outgoingCallFilter.addAction("android.intent.action.PHONE_STATE");
outgoingCallFilter.addAction("android.intent.action.NEW_OUTGOING_CALL");
//注册接收者
registerReceiver(outgoingCallReciver,outgoingCallFilter);
//来电
TelephonyManagertelmgr=(TelephonyManager)getSystemService(
Context.TELEPHONY_SERVICE);
telmgr.listen(newTelListener(recorder),PhoneStateListener.LISTEN_CALL_STATE);
}
@Override
publicIBinderonBind(Intentintent){
//TODOAuto-generatedmethodstub
returnnull;
}
@Override
publicvoidonDestroy(){
super.onDestroy();
unregisterReceiver(outgoingCallReciver);
Toast.makeText(
this,"已关闭电话监听服务",Toast.LENGTH_LONG)
.show();
Log.d("Recorder","已关闭电话监听服务");
}
@Override
publicintonStartCommand(Intentintent,intflags,intstartId){
returnSTART_STICKY;
}
}
注册以下service:
XML/HTML代码
<serviceandroid:name="com.sdvdxl.service.PhoneCallStateService"/>
到此为止,来去电状态的监听功能算是完成了,剩下一个录音机,附上录音机代码如下:
packagecom.sdvdxl.phonerecorder;
importjava.io.File;
importjava.io.IOException;
importjava.text.SimpleDateFormat;
importjava.util.Date;
importandroid.media.MediaRecorder;
importandroid.os.Environment;
importandroid.util.Log;
publicclassMyRecorder{
privateStringphoneNumber;
privateMediaRecordermrecorder;
privatebooleanstarted=false;//录音机是否已经启动
privatebooleanisCommingNumber=false;//是否是来电
privateStringTAG="Recorder";
publicMyRecorder(StringphoneNumber){
this.setPhoneNumber(phoneNumber);
}
publicMyRecorder(){
}
publicvoidstart(){
started=true;
mrecorder=newMediaRecorder();
FilerecordPath=newFile(
Environment.getExternalStorageDirectory()
,"/Myrecord");
if(!recordPath.exists()){
recordPath.mkdirs();
Log.d("recorder","创建目录");
}
StringcallDir="呼出";
if(isCommingNumber){
callDir="呼入";
}
StringfileName=callDir+"-"+phoneNumber+"-"
+newSimpleDateFormat("yy-MM-dd_HH-mm-ss")
.format(newDate(System.currentTimeMillis()))+".mp3";//实际是3gp
FilerecordName=newFile(recordPath,fileName);
try{
recordName.createNewFile();
Log.d("recorder","创建文件"+recordName.getName());
}catch(IOExceptione){
e.printStackTrace();
}
mrecorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
mrecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
mrecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
mrecorder.setOutputFile(recordName.getAbsolutePath());
try{
mrecorder.prepare();
}catch(IllegalStateExceptione){
e.printStackTrace();
}catch(IOExceptione){
e.printStackTrace();
}
mrecorder.start();
started=true;
Log.d(TAG,"录音开始");
}
publicvoidstop(){
try{
if(mrecorder!=null){
mrecorder.stop();
mrecorder.release();
mrecorder=null;
}
started=false;
}catch(IllegalStateExceptione){
e.printStackTrace();
}
Log.d(TAG,"录音结束");
}
publicvoidpause(){
}
publicStringgetPhoneNumber(){
returnphoneNumber;
}
publicvoidsetPhoneNumber(StringphoneNumber){
this.phoneNumber=phoneNumber;
}
publicbooleanisStarted(){
returnstarted;
}
publicvoidsetStarted(booleanhasStarted){
this.started=hasStarted;
}
publicbooleanisCommingNumber(){
returnisCommingNumber;
}
publicvoidsetIsCommingNumber(booleanisCommingNumber){
this.isCommingNumber=isCommingNumber;
}
}
到此,来去电通话自动录音的所有功能就完成了,大家可以自己试着编写并实现。
以上就是对Android电话录音的开发,希望能帮助有需要的朋友,谢谢大家对本站的支持!