欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android 实现电话来去自动录音的功能

程序员文章站 2024-03-31 19:08:22
我们在使用android手机打电话时,有时可能会需要对来去电通话自动录音,本文就详细讲解实现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、先进行来电的分析和实现。

       相对去电来说,来电状态的转换检测要简单些。android api 中的phonestatelistener 类提供了相应的方法,但我们需要覆盖其中的 oncallstatechanged(int state, string incomingnumber) 方法即可实现来电状态的检测,并在此基础上添加录音功能即可。其中 state 参数就是各种电话状态,到时我们将它跟下面我们要用到的状态进行比较,若是电话处在我们想要的状态上,则进行一系列操作,否则就不管他。想要获取这些状态,还需要另一个电话相关类,那就是 telephonymanager, 该类 提供了一些电话状态,其中我们要用到的是:telephonymanager.call_state_idle(空闲)、telephonymanager.call_state_offhook(摘机)和 telephonymanager.call_state_ringing(来电响铃)这三个状态。判别这三种状态,可以继承 android.telephony.phonestatelistener 类,实现上面提到的 oncallstatechanged(int state, string incomingnumber) 方法,请看如下代码:

public class tellistener extends phonestatelistener {  
  
 @override  
 public void oncallstatechanged(int state, string incomingnumber) {  
  super.oncallstatechanged(state, incomingnumber);  
  
  switch (state) {  
  case telephonymanager.call_state_idle: // 空闲状态,即无来电也无去电  
   log.i("telephonestate", "idle");  
   //此处添加一系列功能代码 
   break;  
  case telephonymanager.call_state_ringing: // 来电响铃  
   log.i("telephonestate", "ringing");  
   //此处添加一系列功能代码 
   break;  
  case telephonymanager.call_state_offhook: // 摘机,即接通 
   log.i("telephonestate", "offhook");  
   //此处添加一系列功能代码 
   break;  
  }  
  
  log.i("telephonestate", string.valueof(incomingnumber));  
 }  
  
} 

有了以上来电状态监听代码还不足以实现监听功能,还需要在我们的一个activity或者service中实现监听,方法很简单,代码如下:

/** 
* 在activity 或者 service中加入如下代码,以实现来电状态监听 
*/ 
telephonymanager telmgr = (telephonymanager)context.getsystemservice( 
    context.telephony_service); 
  telmgr.listen(new tellistener(), phonestatelistener.listen_call_state); 

这样就实现了来电状态监听功能,但要能够在设备中跑起来,这还不够,它还需要两个获取手机电话状态的权限:

<uses-permission android:name="android.permission.read_phone_state" /> 
<uses-permission android:name="android.permission.process_outgoing_calls" /> 

这样的话就可以跑起来了。

       说到这,我想如果你可以实现录音功能的话,在此基础上实现来电自动录音就应该没什么问题了,不过请容我简单罗嗦几句。既然是来电,那么要想录音的话,那么应该就是在监听到 telephonymanager.call_state_offhook 的状态时开启录音机开始录音, 在监听到telephonymanager.call_state_idle 的状态时关闭录音机停止录音。这样,来电录音功能就完成了,不要忘记录音功能同样需要权限:

<uses-permission android:name="android.permission.record_audio"/>  
  
<!-- 要存储文件或者创建文件夹的话还需要以下两个权限 -->  
<uses-permission android:name="android.permission.mount_unmount_filesystems"/>  
<uses-permission android:name="android.permission.write_external_storage"/> 

2、介绍完了来电自动录音,下面就来介绍去电自动录音的实现方法。

       上面说过,相比来电状态的监听,去电的要麻烦些,甚至这种方法不是通用的,这个主要是因为android api 中没有提供去电状态监听的相应类和方法(也许我刚接触,没有找到)。刚开始网上搜索了一通也没有找到对应的解决方法,大多是 来电监听的,也就是上面的方法。不过中途发现一篇博文(后来就搜不到了),记得是查询系统日志的方式,从中找到去电过程中的各个状态的关键词。无奈之中,最终妥协了此方法。

       我的(联想a65上的)去电日志内容如下:

       过滤关键词为 mforeground

01-06 16:29:54.225: d/incallscreen(251): onphonestatechanged: mforegroundcall.getstate() : dialing 
01-06 16:29:54.245: v/loginfo outgoing call(2492): d/incallscreen( 251): onphonestatechanged: mforegroundcall.getstate() : dialing 
01-06 16:29:54.631: d/incallscreen(251): onphonestatechanged: mforegroundcall.getstate() : dialing 
01-06 16:29:54.645: v/loginfo outgoing call(2492): d/incallscreen( 251): onphonestatechanged: mforegroundcall.getstate() : dialing 
01-06 16:29:54.742: d/incallscreen(251): onphonestatechanged: mforegroundcall.getstate() : dialing 
01-06 16:29:54.766: v/loginfo outgoing call(2492): d/incallscreen( 251): onphonestatechanged: mforegroundcall.getstate() : dialing 
01-06 16:29:54.873: d/incallscreen(251): onphonestatechanged: mforegroundcall.getstate() : dialing 
01-06 16:29:54.877: v/loginfo outgoing call(2492): d/incallscreen( 251): onphonestatechanged: mforegroundcall.getstate() : dialing 
01-06 16:29:55.108: d/incallscreen(251): onphonestatechanged: mforegroundcall.getstate() : dialing 
01-06 16:29:55.125: v/loginfo outgoing call(2492): d/incallscreen( 251): onphonestatechanged: mforegroundcall.getstate() : dialing 
01-06 16:29:57.030: d/incallscreen(251): onphonestatechanged: mforegroundcall.getstate() : active 
01-06 16:29:57.155: v/loginfo outgoing call(2492): d/incallscreen( 251): onphonestatechanged: mforegroundcall.getstate() : active 
01-06 16:29:57.480: d/incallscreen(251): onphonestatechanged: mforegroundcall.getstate() : active 
01-06 16:29:57.598: v/loginfo outgoing call(2492): d/incallscreen( 251): onphonestatechanged: mforegroundcall.getstate() : active 
01-06 16:29:59.319: d/incallscreen(251): onphonestatechanged: mforegroundcall.getstate() : disconnecting 
01-06 16:29:59.373: v/loginfo outgoing call(2492): d/incallscreen( 251): onphonestatechanged: mforegroundcall.getstate() : disconnecting 
01-06 16:30:00.392: d/incallscreen(251): - ondisconnect: currentlyidle:true ; mforegroundcall.getstate():disconnected 
01-06 16:30:00.399: v/loginfo outgoing call(2492): d/incallscreen( 251): - ondisconnect: currentlyidle:true ; mforegroundcall.getstate():disconnected 
01-06 16:30:01.042: d/incallscreen(251): onphonestatechanged: mforegroundcall.getstate() : idle 
01-06 16:30:01.070: v/loginfo outgoing call(2492): d/incallscreen( 251): onphonestatechanged: mforegroundcall.getstate() : idle 
01-06 16:30:01.558: d/incallscreen(251): onphonestatechanged: mforegroundcall.getstate() : idle 
01-06 16:30:01.572: v/loginfo outgoing call(2492): d/incallscreen( 251): onphonestatechanged: mforegroundcall.getstate() : idle 

            过滤关键词  mbackground

01-06 16:29:54.226: d/incallscreen(251): onphonestatechanged: mbackgroundcall.getstate() : idle 
01-06 16:29:54.256: v/loginfo outgoing call(2492): d/incallscreen( 251): onphonestatechanged: mbackgroundcall.getstate() : idle 
01-06 16:29:54.638: d/incallscreen(251): onphonestatechanged: mbackgroundcall.getstate() : idle 
01-06 16:29:54.652: v/loginfo outgoing call(2492): d/incallscreen( 251): onphonestatechanged: mbackgroundcall.getstate() : idle 
01-06 16:29:54.743: d/incallscreen(251): onphonestatechanged: mbackgroundcall.getstate() : idle 
01-06 16:29:54.770: v/loginfo outgoing call(2492): d/incallscreen( 251): onphonestatechanged: mbackgroundcall.getstate() : idle 
01-06 16:29:54.875: d/incallscreen(251): onphonestatechanged: mbackgroundcall.getstate() : idle 
01-06 16:29:54.882: v/loginfo outgoing call(2492): d/incallscreen( 251): onphonestatechanged: mbackgroundcall.getstate() : idle 
01-06 16:29:55.109: d/incallscreen(251): onphonestatechanged: mbackgroundcall.getstate() : idle 
01-06 16:29:55.142: v/loginfo outgoing call(2492): d/incallscreen( 251): onphonestatechanged: mbackgroundcall.getstate() : idle 
01-06 16:29:57.031: d/incallscreen(251): onphonestatechanged: mbackgroundcall.getstate() : idle 
01-06 16:29:57.160: v/loginfo outgoing call(2492): d/incallscreen( 251): onphonestatechanged: mbackgroundcall.getstate() : idle 
01-06 16:29:57.481: d/incallscreen(251): onphonestatechanged: mbackgroundcall.getstate() : idle 
01-06 16:29:57.622: v/loginfo outgoing call(2492): d/incallscreen( 251): onphonestatechanged: mbackgroundcall.getstate() : idle 
01-06 16:29:59.319: d/incallscreen(251): onphonestatechanged: mbackgroundcall.getstate() : idle 
01-06 16:29:59.373: v/loginfo outgoing call(2492): d/incallscreen( 251): onphonestatechanged: mbackgroundcall.getstate() : idle 
01-06 16:30:01.042: d/incallscreen(251): onphonestatechanged: mbackgroundcall.getstate() : idle 
01-06 16:30:01.070: v/loginfo outgoing call(2492): d/incallscreen( 251): onphonestatechanged: mbackgroundcall.getstate() : idle 
01-06 16:30:01.559: d/incallscreen(251): onphonestatechanged: mbackgroundcall.getstate() : idle 
01-06 16:30:01.573: v/loginfo outgoing call(2492): d/incallscreen( 251): onphonestatechanged: mbackgroundcall.getstate() : idle 

从上面的日志可以看到,每一行的末尾的大写英文词就是去电的状态,状态说明如下:

  1.        dialing 拨号,对方还未响铃
  2.        active   对方接通,通话建立
  3.        disconnecting 通话断开时
  4.        disconnected  通话已断开,可以认为是挂机了

       由于我拨打的是10010,没有响铃过程(电脑自动接通的够快),还少了一个状态,状态是alerting ,这个就是对方正在响铃的状态。

       有了这几个去电状态就好办了,现在我们要做的就是读取系统日志,然后找到这些状态,提取的关键词就是上面提到的 mforeground(前台通话状态) 和 mbackground (后台通话状态)(可能不一样的设备生成的不一样,根据自己具体设备设置,这里只提取前台的),如果读取的这一行日志中 包含 mforground ,再看看是否包含上面的状态的单词。既然说的如此,那么看看读取系统日志的代码吧。

package com.sdvdxl.phonerecorder; 
 
import java.io.bufferedreader; 
import java.io.ioexception; 
import java.io.inputstream; 
import java.io.inputstreamreader; 
 
import com.sdvdxl.outgoingcall.outgoingcallstate; 
 
import android.content.context; 
import android.content.intent; 
import android.util.log; 
 
/** 
 * 
 * @author sdvdxl 
 * 找到 日志中的 
 * onphonestatechanged: mforegroundcall.getstate() 这个是前台呼叫状态 
 * mbackgroundcall.getstate() 后台电话 
 * 若 是 dialing 则是正在拨号,等待建立连接,但对方还没有响铃, 
 * alerting 呼叫成功,即对方正在响铃, 
 * 若是 active 则已经接通 
 * 若是 disconnected 则本号码呼叫已经挂断 
 * 若是 idle 则是处于 空闲状态 
 *  
 */ 
public class readlog extends thread { 
 private context ctx; 
 private int logcount; 
  
 private static final string tag = "loginfo outgoing call"; 
  
 /** 
  * 前后台电话 
  * @author sdvdxl 
  *  
  */ 
 private static class callviewstate { 
  public static final string fore_ground_call_state = "mforeground"; 
 } 
  
 /** 
  * 呼叫状态 
  * @author sdvdxl 
  * 
  */ 
 private static class callstate { 
  public static final string dialing = "dialing"; 
  public static final string alerting = "alerting"; 
  public static final string active = "active"; 
  public static final string idle = "idle"; 
  public static final string disconnected = "disconnected"; 
 } 
  
 public readlog(context ctx) { 
  this.ctx = ctx; 
 } 
  
 /** 
  * 读取log流 
  * 取得呼出状态的log 
  * 从而得到转换状态 
  */ 
 @override 
 public void run() { 
  log.d(tag, "开始读取日志记录"); 
   
  string[] catchparams = {"logcat", "incallscreen *:s"}; 
  string[] clearparams = {"logcat", "-c"}; 
   
  try { 
   process process=runtime.getruntime().exec(catchparams); 
   inputstream is = process.getinputstream(); 
   bufferedreader reader = new bufferedreader(new inputstreamreader(is)); 
    
   string line = 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); 
    } 
     
   } //end while 
    
  } catch (ioexception e) { 
   e.printstacktrace(); 
  } //end try-catch 
 } //end run 
} //end class readlog 

以上代码中,之所以用线程,是为了防止读取日志过程中阻滞主方法的其他方法的执行,影响到程序捕捉对应的电话状态。

       好了,捕捉到了去电过程中各个状态的转变,那么,如何通知给程序呢,我采用的方法是捕获后立马给系统发送广播,然后程序进行广播接收,接收后再处理录音事件。要发送广播,就要发送一个唯一的广播,为此,建立如下类:

package com.sdvdxl.outgoingcall; 
 
import com.sdvdxl.phonerecorder.readlog; 
 
import android.content.context; 
import android.util.log; 
 
public class outgoingcallstate { 
 context ctx; 
 public outgoingcallstate(context ctx) { 
  this.ctx = ctx; 
 } 
  
 /** 
  * 前台呼叫状态 
  * @author sdvdxl 
  * 
  */ 
 public static final class foregroundcallstate { 
  public static final string dialing =  
    "com.sdvdxl.phonerecorder.fore_ground_dialing"; 
  public static final string alerting =  
    "com.sdvdxl.phonerecorder.fore_ground_alerting"; 
  public static final string active =  
    "com.sdvdxl.phonerecorder.fore_ground_active"; 
  public static final string idle =  
    "com.sdvdxl.phonerecorder.fore_ground_idle"; 
  public static final string disconnected =  
    "com.sdvdxl.phonerecorder.fore_ground_disconnected"; 
 } 
  
 /** 
  * 开始监听呼出状态的转变, 
  * 并在对应状态发送广播 
  */ 
 public void startlisten() { 
  new readlog(ctx).start(); 
  log.d("recorder", "开始监听呼出状态的转变,并在对应状态发送广播"); 
 } 
  
} 

 程序需要读取系统日志权限:

xml/html代码

       <uses-permission android:name="android.permission.read_logs"/>   

       然后,在读取日志的类中检测到去电各个状态的地方发送一个广播,那么,读取日志的完整代码如下:

package com.sdvdxl.phonerecorder; 
 
import java.io.bufferedreader; 
import java.io.ioexception; 
import java.io.inputstream; 
import java.io.inputstreamreader; 
 
import com.sdvdxl.outgoingcall.outgoingcallstate; 
 
import android.content.context; 
import android.content.intent; 
import android.util.log; 
 
/** 
 * 
 * @author mrloong 
 * 找到 日志中的 
 * onphonestatechanged: mforegroundcall.getstate() 这个是前台呼叫状态 
 * mbackgroundcall.getstate() 后台电话 
 * 若 是 dialing 则是正在拨号,等待建立连接,但对方还没有响铃, 
 * alerting 呼叫成功,即对方正在响铃, 
 * 若是 active 则已经接通 
 * 若是 disconnected 则本号码呼叫已经挂断 
 * 若是 idle 则是处于 空闲状态 
 *  
 */ 
public class readlog extends thread { 
 private context ctx; 
 private int logcount; 
  
 private static final string tag = "loginfo outgoing call"; 
  
 /** 
  * 前后台电话 
  * @author sdvdxl 
  *  
  */ 
 private static class callviewstate { 
  public static final string fore_ground_call_state = "mforeground"; 
 } 
  
 /** 
  * 呼叫状态 
  * @author sdvdxl 
  * 
  */ 
 private static class callstate { 
  public static final string dialing = "dialing"; 
  public static final string alerting = "alerting"; 
  public static final string active = "active"; 
  public static final string idle = "idle"; 
  public static final string disconnected = "disconnected"; 
 } 
  
 public readlog(context ctx) { 
  this.ctx = ctx; 
 } 
  
 /** 
  * 读取log流 
  * 取得呼出状态的log 
  * 从而得到转换状态 
  */ 
 @override 
 public void run() { 
  log.d(tag, "开始读取日志记录"); 
   
  string[] catchparams = {"logcat", "incallscreen *:s"}; 
  string[] clearparams = {"logcat", "-c"}; 
   
  try { 
   process process=runtime.getruntime().exec(catchparams); 
   inputstream is = process.getinputstream(); 
   bufferedreader reader = new bufferedreader(new inputstreamreader(is)); 
    
   string line = 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)) { 
     //发送广播 
     intent dialingintent = new intent(); 
     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)) { 
     //发送广播 
     intent dialingintent = new intent(); 
     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)) { 
     //发送广播 
     intent dialingintent = new intent(); 
     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)) { 
     //发送广播 
     intent dialingintent = new intent(); 
     dialingintent.setaction(outgoingcallstate.foregroundcallstate.disconnected); 
     ctx.sendbroadcast(dialingintent); 
      
     log.d(tag, readlog.callstate.disconnected); 
    } 
     
   } //end while 
    
  } catch (ioexception e) { 
   e.printstacktrace(); 
  } //end try-catch 
 } //end run 
} //end class readlog 

发送了广播,那么就要有接收者,定义接收者如下(关于录音机的代码可以先忽略):

package com.sdvdxl.phonerecorder; 
 
import android.content.broadcastreceiver; 
import android.content.context; 
import android.content.intent; 
import android.util.log; 
 
import com.sdvdxl.outgoingcall.outgoingcallstate; 
 
public class outgoingcallreciver extends broadcastreceiver { 
 static final string tag = "recorder"; 
 private myrecorder recorder; 
  
 public outgoingcallreciver() { 
  recorder = new myrecorder(); 
 } 
  
 public outgoingcallreciver (myrecorder recorder) { 
  this.recorder = recorder; 
 } 
  
 @override 
 public void onreceive(context ctx, intent intent) { 
  string phonestate = intent.getaction(); 
   
  if (phonestate.equals(intent.action_new_outgoing_call)) { 
   string phonenum = 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(); 
   } 
  } 
 } 
 
} 

      其中有这么一段代码:

string phonestate = intent.getaction();  
    
  if (phonestate.equals(intent.action_new_outgoing_call)) {  
   string phonenum = intent.getstringextra(intent.extra_phone_number);//拨出号码  
   recorder.setphonenumber(phonenum);  
   recorder.setiscommingnumber(false);  
   log.d(tag, "设置为去电状态");  
   log.d(tag, "去电状态 呼叫:" + phonenum);  
  } 

 这里是接收系统发出的广播,用于接收去电广播。这样,就获得了去电状态。

       3、有了以上主要代码,可以说,来去电监听功能算是完成了,下面创建一个service来运行监听:

package com.sdvdxl.service; 
 
import android.app.service; 
import android.content.context; 
import android.content.intent; 
import android.content.intentfilter; 
import android.os.ibinder; 
import android.telephony.phonestatelistener; 
import android.telephony.telephonymanager; 
import android.util.log; 
import android.widget.toast; 
 
import com.sdvdxl.outgoingcall.outgoingcallstate; 
import com.sdvdxl.phonerecorder.myrecorder; 
import com.sdvdxl.phonerecorder.outgoingcallreciver; 
import com.sdvdxl.phonerecorder.tellistener; 
 
public class phonecallstateservice extends service { 
 private outgoingcallstate outgoingcallstate; 
 private outgoingcallreciver outgoingcallreciver; 
 private myrecorder recorder; 
  
 @override 
 public void oncreate() { 
  super.oncreate(); 
   
  //------以下应放在onstartcommand中,但2.3.5以下版本不会因service重新启动而重新调用-------- 
  //监听电话状态,如果是打入且接听 或者 打出 则开始自动录音 
  //通话结束,保存文件到外部存储器上 
  log.d("recorder", "正在监听中..."); 
  recorder = new myrecorder(); 
  outgoingcallstate = new outgoingcallstate(this); 
  outgoingcallreciver = new outgoingcallreciver(recorder); 
  outgoingcallstate.startlisten(); 
  toast.maketext(this, "服务已启动", toast.length_long).show(); 
   
  //去电 
  intentfilter outgoingcallfilter = new intentfilter(); 
  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); 
   
  //来电 
  telephonymanager telmgr = (telephonymanager)getsystemservice( 
    context.telephony_service); 
  telmgr.listen(new tellistener(recorder), phonestatelistener.listen_call_state); 
   
   
 } 
  
 @override 
 public ibinder onbind(intent intent) { 
  // todo auto-generated method stub 
  return null; 
 } 
 
 @override 
 public void ondestroy() { 
  super.ondestroy(); 
  unregisterreceiver(outgoingcallreciver); 
  toast.maketext( 
    this, "已关闭电话监听服务", toast.length_long) 
    .show(); 
  log.d("recorder", "已关闭电话监听服务"); 
 } 
 
 @override 
 public int onstartcommand(intent intent, int flags, int startid) { 
   
  return start_sticky; 
 } 
 
} 

注册以下service:

xml/html代码

<service android:name="com.sdvdxl.service.phonecallstateservice" />  

       到此为止,来去电状态的监听功能算是完成了,剩下一个录音机,附上录音机代码如下:

package com.sdvdxl.phonerecorder; 
 
import java.io.file; 
import java.io.ioexception; 
import java.text.simpledateformat; 
import java.util.date; 
 
import android.media.mediarecorder; 
import android.os.environment; 
import android.util.log; 
 
public class myrecorder { 
 private string phonenumber; 
 private mediarecorder mrecorder; 
 private boolean started = false; //录音机是否已经启动 
 private boolean iscommingnumber = false;//是否是来电 
 private string tag = "recorder"; 
  
  
 public myrecorder(string phonenumber) { 
  this.setphonenumber(phonenumber); 
 } 
  
 public myrecorder() { 
 } 
 
 public void start() { 
  started = true; 
  mrecorder = new mediarecorder(); 
   
  file recordpath = new file( 
    environment.getexternalstoragedirectory() 
    , "/my record");  
  if (!recordpath.exists()) { 
   recordpath.mkdirs(); 
   log.d("recorder", "创建目录"); 
  } 
   
  string calldir = "呼出"; 
  if (iscommingnumber) { 
   calldir = "呼入"; 
  } 
  string filename = calldir + "-" + phonenumber + "-"  
    + new simpledateformat("yy-mm-dd_hh-mm-ss") 
     .format(new date(system.currenttimemillis())) + ".mp3";//实际是3gp 
  file recordname = new file(recordpath, filename); 
   
  try { 
   recordname.createnewfile(); 
   log.d("recorder", "创建文件" + recordname.getname()); 
  } catch (ioexception e) { 
   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 (illegalstateexception e) { 
   e.printstacktrace(); 
  } catch (ioexception e) { 
   e.printstacktrace(); 
  } 
  mrecorder.start(); 
  started = true; 
  log.d(tag , "录音开始"); 
 } 
  
 public void stop() { 
  try { 
   if (mrecorder!=null) { 
    mrecorder.stop(); 
    mrecorder.release(); 
    mrecorder = null; 
   } 
   started = false; 
  } catch (illegalstateexception e) { 
   e.printstacktrace(); 
  } 
   
   
  log.d(tag , "录音结束"); 
 } 
  
 public void pause() { 
   
 } 
 
 public string getphonenumber() { 
  return phonenumber; 
 } 
 
 public void setphonenumber(string phonenumber) { 
  this.phonenumber = phonenumber; 
 } 
 
 public boolean isstarted() { 
  return started; 
 } 
 
 public void setstarted(boolean hasstarted) { 
  this.started = hasstarted; 
 } 
 
 public boolean iscommingnumber() { 
  return iscommingnumber; 
 } 
 
 public void setiscommingnumber(boolean iscommingnumber) { 
  this.iscommingnumber = iscommingnumber; 
 } 
 
} 

       到此,来去电通话自动录音的所有功能就完成了,大家可以自己试着编写并实现。

       以上就是对android 电话录音的开发,希望能帮助有需要的朋友,谢谢大家对本站的支持!