欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android 實(shí)現(xiàn)電話來(lái)去自動(dòng)錄音的功能

 更新時(shí)間:2016年08月11日 10:13:48   投稿:lqh  
本文主要介紹Android 電話自動(dòng)錄音功能的開(kāi)發(fā),這里提供實(shí)現(xiàn)代碼和實(shí)現(xiàn)效果圖,有需要的小伙伴可以參考下

我們?cè)谑褂肁ndroid手機(jī)打電話時(shí),有時(shí)可能會(huì)需要對(duì)來(lái)去電通話自動(dòng)錄音,本文就詳細(xì)講解實(shí)現(xiàn)Android來(lái)去電通話自動(dòng)錄音的方法,大家按照文中的方法編寫(xiě)程序就可以完成此功能。

       來(lái)去電自動(dòng)錄音的關(guān)鍵在于如何監(jiān)聽(tīng)手機(jī)電話狀態(tài)的轉(zhuǎn)變:

       1)來(lái)電的狀態(tài)的轉(zhuǎn)換如下(紅色標(biāo)記是我們要用到的狀態(tài))

       空閑(IDEL)——> 響鈴(RINGING)——> 接聽(tīng)(ACTIVE)——> 掛斷(經(jīng)歷DISCONNECTING——DISCONNECTED)——> 空閑(IDEL)

       或者  空閑(IDEL)——> 響鈴(RINGING)——> 拒接 ——> 空閑(IDEL)

       2)去電狀態(tài)的轉(zhuǎn)換如下

       空閑(IDEL)——> 撥號(hào) (DIALING)——> (對(duì)方)響鈴(ALERTING) ——> 建立連接(ACTIVE)—— 掛斷(經(jīng)歷DISCONNECTING——DISCONNECTED)——> 空閑(IDEL)

       或者 空閑(IDEL)——> 撥號(hào) (DIALING)——> (對(duì)方)響鈴(ALERTING)——> 掛斷/對(duì)方拒接 ——> 空閑(IDEL)

       下面就分別就來(lái)電和去電這兩種狀態(tài)分析并實(shí)現(xiàn)。

       1、先進(jìn)行來(lái)電的分析和實(shí)現(xiàn)。

       相對(duì)去電來(lái)說(shuō),來(lái)電狀態(tài)的轉(zhuǎn)換檢測(cè)要簡(jiǎn)單些。android api 中的PhoneStateListener 類提供了相應(yīng)的方法,但我們需要覆蓋其中的 onCallStateChanged(int state, String incomingNumber) 方法即可實(shí)現(xiàn)來(lái)電狀態(tài)的檢測(cè),并在此基礎(chǔ)上添加錄音功能即可。其中 state 參數(shù)就是各種電話狀態(tài),到時(shí)我們將它跟下面我們要用到的狀態(tài)進(jìn)行比較,若是電話處在我們想要的狀態(tài)上,則進(jìn)行一系列操作,否則就不管他。想要獲取這些狀態(tài),還需要另一個(gè)電話相關(guān)類,那就是 TelephonyManager, 該類 提供了一些電話狀態(tài),其中我們要用到的是:TelephonyManager.CALL_STATE_IDLE(空閑)、TelephonyManager.CALL_STATE_OFFHOOK(摘機(jī))和 TelephonyManager.CALL_STATE_RINGING(來(lái)電響鈴)這三個(gè)狀態(tài)。判別這三種狀態(tài),可以繼承 android.telephony.PhoneStateListener 類,實(shí)現(xiàn)上面提到的 onCallStateChanged(int state, String incomingNumber) 方法,請(qǐng)看如下代碼:

public class TelListener extends PhoneStateListener {  
  
 @Override  
 public void onCallStateChanged(int state, String incomingNumber) {  
  super.onCallStateChanged(state, incomingNumber);  
  
  switch (state) {  
  case TelephonyManager.CALL_STATE_IDLE: // 空閑狀態(tài),即無(wú)來(lái)電也無(wú)去電  
   Log.i("TelephoneState", "IDLE");  
   //此處添加一系列功能代碼 
   break;  
  case TelephonyManager.CALL_STATE_RINGING: // 來(lái)電響鈴  
   Log.i("TelephoneState", "RINGING");  
   //此處添加一系列功能代碼 
   break;  
  case TelephonyManager.CALL_STATE_OFFHOOK: // 摘機(jī),即接通 
   Log.i("TelephoneState", "OFFHOOK");  
   //此處添加一系列功能代碼 
   break;  
  }  
  
  Log.i("TelephoneState", String.valueOf(incomingNumber));  
 }  
  
} 

有了以上來(lái)電狀態(tài)監(jiān)聽(tīng)代碼還不足以實(shí)現(xiàn)監(jiān)聽(tīng)功能,還需要在我們的一個(gè)Activity或者Service中實(shí)現(xiàn)監(jiān)聽(tīng),方法很簡(jiǎn)單,代碼如下:

/** 
* 在activity 或者 service中加入如下代碼,以實(shí)現(xiàn)來(lái)電狀態(tài)監(jiān)聽(tīng) 
*/ 
TelephonyManager telMgr = (TelephonyManager)context.getSystemService( 
    Context.TELEPHONY_SERVICE); 
  telMgr.listen(new TelListener(), PhoneStateListener.LISTEN_CALL_STATE); 

這樣就實(shí)現(xiàn)了來(lái)電狀態(tài)監(jiān)聽(tīng)功能,但要能夠在設(shè)備中跑起來(lái),這還不夠,它還需要兩個(gè)獲取手機(jī)電話狀態(tài)的權(quán)限:

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

這樣的話就可以跑起來(lái)了。

       說(shuō)到這,我想如果你可以實(shí)現(xiàn)錄音功能的話,在此基礎(chǔ)上實(shí)現(xiàn)來(lái)電自動(dòng)錄音就應(yīng)該沒(méi)什么問(wèn)題了,不過(guò)請(qǐng)容我簡(jiǎn)單羅嗦幾句。既然是來(lái)電,那么要想錄音的話,那么應(yīng)該就是在監(jiān)聽(tīng)到 TelephonyManager.CALL_STATE_OFFHOOK 的狀態(tài)時(shí)開(kāi)啟錄音機(jī)開(kāi)始錄音, 在監(jiān)聽(tīng)到TelephonyManager.CALL_STATE_IDLE 的狀態(tài)時(shí)關(guān)閉錄音機(jī)停止錄音。這樣,來(lái)電錄音功能就完成了,不要忘記錄音功能同樣需要權(quán)限:

<uses-permission android:name="android.permission.RECORD_AUDIO"/>  
  
<!-- 要存儲(chǔ)文件或者創(chuàng)建文件夾的話還需要以下兩個(gè)權(quán)限 -->  
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>  
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 

2、介紹完了來(lái)電自動(dòng)錄音,下面就來(lái)介紹去電自動(dòng)錄音的實(shí)現(xiàn)方法。

       上面說(shuō)過(guò),相比來(lái)電狀態(tài)的監(jiān)聽(tīng),去電的要麻煩些,甚至這種方法不是通用的,這個(gè)主要是因?yàn)閍ndroid api 中沒(méi)有提供去電狀態(tài)監(jiān)聽(tīng)的相應(yīng)類和方法(也許我剛接觸,沒(méi)有找到)。剛開(kāi)始網(wǎng)上搜索了一通也沒(méi)有找到對(duì)應(yīng)的解決方法,大多是 來(lái)電監(jiān)聽(tīng)的,也就是上面的方法。不過(guò)中途發(fā)現(xiàn)一篇博文(后來(lái)就搜不到了),記得是查詢系統(tǒng)日志的方式,從中找到去電過(guò)程中的各個(gè)狀態(tài)的關(guān)鍵詞。無(wú)奈之中,最終妥協(xié)了此方法。

       我的(聯(lián)想A65上的)去電日志內(nèi)容如下:

       過(guò)濾關(guān)鍵詞為 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 

            過(guò)濾關(guān)鍵詞  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 

從上面的日志可以看到,每一行的末尾的大寫(xiě)英文詞就是去電的狀態(tài),狀態(tài)說(shuō)明如下:

  1.        DIALING 撥號(hào),對(duì)方還未響鈴
  2.        ACTIVE   對(duì)方接通,通話建立
  3.        DISCONNECTING 通話斷開(kāi)時(shí)
  4.        DISCONNECTED  通話已斷開(kāi),可以認(rèn)為是掛機(jī)了

       由于我撥打的是10010,沒(méi)有響鈴過(guò)程(電腦自動(dòng)接通的夠快),還少了一個(gè)狀態(tài),狀態(tài)是ALERTING ,這個(gè)就是對(duì)方正在響鈴的狀態(tài)。

       有了這幾個(gè)去電狀態(tài)就好辦了,現(xiàn)在我們要做的就是讀取系統(tǒng)日志,然后找到這些狀態(tài),提取的關(guān)鍵詞就是上面提到的 mforeground(前臺(tái)通話狀態(tài)) 和 mbackground (后臺(tái)通話狀態(tài))(可能不一樣的設(shè)備生成的不一樣,根據(jù)自己具體設(shè)備設(shè)置,這里只提取前臺(tái)的),如果讀取的這一行日志中 包含 mforground ,再看看是否包含上面的狀態(tài)的單詞。既然說(shuō)的如此,那么看看讀取系統(tǒng)日志的代碼吧。

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() 這個(gè)是前臺(tái)呼叫狀態(tài) 
 * mBackgroundCall.getState() 后臺(tái)電話 
 * 若 是 DIALING 則是正在撥號(hào),等待建立連接,但對(duì)方還沒(méi)有響鈴, 
 * ALERTING 呼叫成功,即對(duì)方正在響鈴, 
 * 若是 ACTIVE 則已經(jīng)接通 
 * 若是 DISCONNECTED 則本號(hào)碼呼叫已經(jīng)掛斷 
 * 若是 IDLE 則是處于 空閑狀態(tài) 
 *  
 */ 
public class ReadLog extends Thread { 
 private Context ctx; 
 private int logCount; 
  
 private static final String TAG = "LogInfo OutGoing Call"; 
  
 /** 
  * 前后臺(tái)電話 
  * @author sdvdxl 
  *  
  */ 
 private static class CallViewState { 
  public static final String FORE_GROUND_CALL_STATE = "mForeground"; 
 } 
  
 /** 
  * 呼叫狀態(tài) 
  * @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流 
  * 取得呼出狀態(tài)的log 
  * 從而得到轉(zhuǎn)換狀態(tài) 
  */ 
 @Override 
 public void run() { 
  Log.d(TAG, "開(kāi)始讀取日志記錄"); 
   
  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); 
     
    //日志超過(guò)512條就清理 
    if (logCount>512) { 
     //清理日志 
     Runtime.getRuntime().exec(clearParams) 
      .destroy();//銷毀進(jìn)程,釋放資源 
     logCount = 0; 
     Log.v(TAG, "-----------清理日志---------------"); 
    }  
     
    /*---------------------------------前臺(tái)呼叫-----------------------*/ 
    //空閑 
    if (line.contains(ReadLog.CallViewState.FORE_GROUND_CALL_STATE) 
      && line.contains(ReadLog.CallState.IDLE)) { 
     Log.d(TAG, ReadLog.CallState.IDLE); 
    } 
     
    //正在撥號(hào),等待建立連接,即已撥號(hào),但對(duì)方還沒(méi)有響鈴, 
    if (line.contains(ReadLog.CallViewState.FORE_GROUND_CALL_STATE) 
      && line.contains(ReadLog.CallState.DIALING)) { 
     Log.d(TAG, ReadLog.CallState.DIALING); 
    } 
     
    //呼叫對(duì)方 正在響鈴 
    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); 
    } 
     
    //斷開(kāi)連接,即掛機(jī) 
    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 

以上代碼中,之所以用線程,是為了防止讀取日志過(guò)程中阻滯主方法的其他方法的執(zhí)行,影響到程序捕捉對(duì)應(yīng)的電話狀態(tài)。

       好了,捕捉到了去電過(guò)程中各個(gè)狀態(tài)的轉(zhuǎn)變,那么,如何通知給程序呢,我采用的方法是捕獲后立馬給系統(tǒng)發(fā)送廣播,然后程序進(jìn)行廣播接收,接收后再處理錄音事件。要發(fā)送廣播,就要發(fā)送一個(gè)唯一的廣播,為此,建立如下類:

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; 
 } 
  
 /** 
  * 前臺(tái)呼叫狀態(tài) 
  * @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"; 
 } 
  
 /** 
  * 開(kāi)始監(jiān)聽(tīng)呼出狀態(tài)的轉(zhuǎn)變, 
  * 并在對(duì)應(yīng)狀態(tài)發(fā)送廣播 
  */ 
 public void startListen() { 
  new ReadLog(ctx).start(); 
  Log.d("Recorder", "開(kāi)始監(jiān)聽(tīng)呼出狀態(tài)的轉(zhuǎn)變,并在對(duì)應(yīng)狀態(tài)發(fā)送廣播"); 
 } 
  
} 

 程序需要讀取系統(tǒng)日志權(quán)限:

XML/HTML代碼

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

       然后,在讀取日志的類中檢測(cè)到去電各個(gè)狀態(tài)的地方發(fā)送一個(gè)廣播,那么,讀取日志的完整代碼如下:

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() 這個(gè)是前臺(tái)呼叫狀態(tài) 
 * mBackgroundCall.getState() 后臺(tái)電話 
 * 若 是 DIALING 則是正在撥號(hào),等待建立連接,但對(duì)方還沒(méi)有響鈴, 
 * ALERTING 呼叫成功,即對(duì)方正在響鈴, 
 * 若是 ACTIVE 則已經(jīng)接通 
 * 若是 DISCONNECTED 則本號(hào)碼呼叫已經(jīng)掛斷 
 * 若是 IDLE 則是處于 空閑狀態(tài) 
 *  
 */ 
public class ReadLog extends Thread { 
 private Context ctx; 
 private int logCount; 
  
 private static final String TAG = "LogInfo OutGoing Call"; 
  
 /** 
  * 前后臺(tái)電話 
  * @author sdvdxl 
  *  
  */ 
 private static class CallViewState { 
  public static final String FORE_GROUND_CALL_STATE = "mForeground"; 
 } 
  
 /** 
  * 呼叫狀態(tài) 
  * @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流 
  * 取得呼出狀態(tài)的log 
  * 從而得到轉(zhuǎn)換狀態(tài) 
  */ 
 @Override 
 public void run() { 
  Log.d(TAG, "開(kāi)始讀取日志記錄"); 
   
  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); 
     
    //日志超過(guò)512條就清理 
    if (logCount>512) { 
     //清理日志 
     Runtime.getRuntime().exec(clearParams) 
      .destroy();//銷毀進(jìn)程,釋放資源 
     logCount = 0; 
     Log.v(TAG, "-----------清理日志---------------"); 
    }  
     
    /*---------------------------------前臺(tái)呼叫-----------------------*/ 
    //空閑 
    if (line.contains(ReadLog.CallViewState.FORE_GROUND_CALL_STATE) 
      && line.contains(ReadLog.CallState.IDLE)) { 
     Log.d(TAG, ReadLog.CallState.IDLE); 
    } 
     
    //正在撥號(hào),等待建立連接,即已撥號(hào),但對(duì)方還沒(méi)有響鈴, 
    if (line.contains(ReadLog.CallViewState.FORE_GROUND_CALL_STATE) 
      && line.contains(ReadLog.CallState.DIALING)) { 
     //發(fā)送廣播 
     Intent dialingIntent = new Intent(); 
     dialingIntent.setAction(OutgoingCallState.ForeGroundCallState.DIALING); 
     ctx.sendBroadcast(dialingIntent); 
      
     Log.d(TAG, ReadLog.CallState.DIALING); 
    } 
     
    //呼叫對(duì)方 正在響鈴 
    if (line.contains(ReadLog.CallViewState.FORE_GROUND_CALL_STATE) 
      && line.contains(ReadLog.CallState.ALERTING)) { 
     //發(fā)送廣播 
     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)) { 
     //發(fā)送廣播 
     Intent dialingIntent = new Intent(); 
     dialingIntent.setAction(OutgoingCallState.ForeGroundCallState.ACTIVE); 
     ctx.sendBroadcast(dialingIntent); 
      
     Log.d(TAG, ReadLog.CallState.ACTIVE); 
    } 
     
    //斷開(kāi)連接,即掛機(jī) 
    if (line.contains(ReadLog.CallViewState.FORE_GROUND_CALL_STATE) 
      && line.contains(ReadLog.CallState.DISCONNECTED)) { 
     //發(fā)送廣播 
     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 

發(fā)送了廣播,那么就要有接收者,定義接收者如下(關(guān)于錄音機(jī)的代碼可以先忽略):

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);//撥出號(hào)碼 
   recorder.setPhoneNumber(phoneNum); 
   recorder.setIsCommingNumber(false); 
   Log.d(TAG, "設(shè)置為去電狀態(tài)"); 
   Log.d(TAG, "去電狀態(tài) 呼叫:" + phoneNum); 
  } 
   
  if (phoneState.equals(OutgoingCallState.ForeGroundCallState.DIALING)) { 
   Log.d(TAG, "正在撥號(hào)..."); 
  } 
   
  if (phoneState.equals(OutgoingCallState.ForeGroundCallState.ALERTING)) { 
   Log.d(TAG, "正在呼叫..."); 
  } 
   
  if (phoneState.equals(OutgoingCallState.ForeGroundCallState.ACTIVE)) { 
   if (!recorder.isCommingNumber() && !recorder.isStarted()) { 
    Log.d(TAG, "去電已接通 啟動(dòng)錄音機(jī)"); 
    recorder.start(); 
     
   } 
  } 
   
  if (phoneState.equals(OutgoingCallState.ForeGroundCallState.DISCONNECTED)) { 
   if (!recorder.isCommingNumber() && recorder.isStarted()) { 
    Log.d(TAG, "已掛斷 關(guān)閉錄音機(jī)"); 
    recorder.stop(); 
   } 
  } 
 } 
 
} 

      其中有這么一段代碼:

String phoneState = intent.getAction();  
    
  if (phoneState.equals(Intent.ACTION_NEW_OUTGOING_CALL)) {  
   String phoneNum = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);//撥出號(hào)碼  
   recorder.setPhoneNumber(phoneNum);  
   recorder.setIsCommingNumber(false);  
   Log.d(TAG, "設(shè)置為去電狀態(tài)");  
   Log.d(TAG, "去電狀態(tài) 呼叫:" + phoneNum);  
  } 

 這里是接收系統(tǒng)發(fā)出的廣播,用于接收去電廣播。這樣,就獲得了去電狀態(tài)。

       3、有了以上主要代碼,可以說(shuō),來(lái)去電監(jiān)聽(tīng)功能算是完成了,下面創(chuàng)建一個(gè)service來(lái)運(yùn)行監(jiān)聽(tīng):

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(); 
   
  //------以下應(yīng)放在onStartCommand中,但2.3.5以下版本不會(huì)因service重新啟動(dòng)而重新調(diào)用-------- 
  //監(jiān)聽(tīng)電話狀態(tài),如果是打入且接聽(tīng) 或者 打出 則開(kāi)始自動(dòng)錄音 
  //通話結(jié)束,保存文件到外部存儲(chǔ)器上 
  Log.d("Recorder", "正在監(jiān)聽(tīng)中..."); 
  recorder = new MyRecorder(); 
  outgoingCallState = new OutgoingCallState(this); 
  outgoingCallReciver = new OutgoingCallReciver(recorder); 
  outgoingCallState.startListen(); 
  Toast.makeText(this, "服務(wù)已啟動(dòng)", 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"); 
   
  //注冊(cè)接收者 
  registerReceiver(outgoingCallReciver, outgoingCallFilter); 
   
  //來(lái)電 
  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, "已關(guān)閉電話監(jiān)聽(tīng)服務(wù)", Toast.LENGTH_LONG) 
    .show(); 
  Log.d("Recorder", "已關(guān)閉電話監(jiān)聽(tīng)服務(wù)"); 
 } 
 
 @Override 
 public int onStartCommand(Intent intent, int flags, int startId) { 
   
  return START_STICKY; 
 } 
 
} 

注冊(cè)以下service:

XML/HTML代碼

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

       到此為止,來(lái)去電狀態(tài)的監(jiān)聽(tīng)功能算是完成了,剩下一個(gè)錄音機(jī),附上錄音機(jī)代碼如下:

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; //錄音機(jī)是否已經(jīng)啟動(dòng) 
 private boolean isCommingNumber = false;//是否是來(lái)電 
 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", "創(chuàng)建目錄"); 
  } 
   
  String callDir = "呼出"; 
  if (isCommingNumber) { 
   callDir = "呼入"; 
  } 
  String fileName = callDir + "-" + phoneNumber + "-"  
    + new SimpleDateFormat("yy-MM-dd_HH-mm-ss") 
     .format(new Date(System.currentTimeMillis())) + ".mp3";//實(shí)際是3gp 
  File recordName = new File(recordPath, fileName); 
   
  try { 
   recordName.createNewFile(); 
   Log.d("recorder", "創(chuàng)建文件" + 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 , "錄音開(kāi)始"); 
 } 
  
 public void stop() { 
  try { 
   if (mrecorder!=null) { 
    mrecorder.stop(); 
    mrecorder.release(); 
    mrecorder = null; 
   } 
   started = false; 
  } catch (IllegalStateException e) { 
   e.printStackTrace(); 
  } 
   
   
  Log.d(TAG , "錄音結(jié)束"); 
 } 
  
 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; 
 } 
 
} 

       到此,來(lái)去電通話自動(dòng)錄音的所有功能就完成了,大家可以自己試著編寫(xiě)并實(shí)現(xiàn)。

       以上就是對(duì)Android 電話錄音的開(kāi)發(fā),希望能幫助有需要的朋友,謝謝大家對(duì)本站的支持!

相關(guān)文章

最新評(píng)論