国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android 實現電話來去自動錄音的功能

2019-12-12 05:43:35
字體:
來源:轉載
供稿:網友

我們在使用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 電話錄音的開發,希望能幫助有需要的朋友,謝謝大家對本站的支持!

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 鲜城| 万年县| 容城县| 泸州市| 托克逊县| 铁岭市| 通州市| 尼木县| 卢龙县| 深圳市| 海南省| 山西省| 平阴县| 广安市| 海伦市| 乾安县| 宜丰县| 无为县| 北宁市| 多伦县| 康乐县| 东海县| 佛山市| 新宾| 乐至县| 襄樊市| 边坝县| 巴林右旗| 两当县| 阿巴嘎旗| 通河县| 广州市| 茂名市| 汉源县| 惠来县| 龙泉市| 孟村| 沈丘县| 黔南| 莒南县| 铜川市|