我們在使用Android手機打電話時,有時可能會需要對來去電通話自動錄音,本文就詳細講解實現Android來去電通話自動錄音的方法,大家按照文中的方法編寫程式就可以完成此功能。 來去電自動錄音的關鍵在於如何監聽手機電話狀態的轉變: 1)來電的狀態的轉換如下(紅色標記是我們要用到的狀態) 空閑(IDE
我們在使用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) 方法,請看如下代碼:
Java代碼- 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中實現監聽,方法很簡單,代碼如下:
Java代碼- /**
- * 在activity 或者 service中加入如下代碼,以實現來電狀態監聽
- */
- TelephonyManager telMgr = (TelephonyManager)context.getSystemService(
- Context.TELEPHONY_SERVICE);
- telMgr.listen(new TelListener(), PhoneStateListener.LISTEN_CALL_STATE);
這樣就實現了來電狀態監聽功能,但要能夠在設備中跑起來,這還不夠,它還需要兩個獲取手機電話狀態的許可權:
XML/HTML代碼- <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 的狀態時關閉錄音機停止錄音。這樣,來電錄音功能就完成了,不要忘記錄音功能同樣需要許可權:
XML/HTML代碼- <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
Java代碼- 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
Java代碼- 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
從上面的日誌可以看到,每一行的末尾的大寫英文詞就是去電的狀態,狀態說明如下:
DIALING 撥號,對方還未響鈴
ACTIVE 對方接通,通話建立
DISCONNECTING 通話斷開時
DISCONNECTED 通話已斷開,可以認為是掛機了
由於我撥打的是10010,沒有響鈴過程(電腦自動接通的夠快),還少了一個狀態,狀態是ALERTING ,這個就是對方正在響鈴的狀態。
有了這幾個去電狀態就好辦了,現在我們要做的就是讀取系統日誌,然後找到這些狀態,提取的關鍵詞就是上面提到的 mforeground(前臺通話狀態) 和 mbackground (後臺通話狀態)(可能不一樣的設備生成的不一樣,根據自己具體設備設置,這裡只提取前臺的),如果讀取的這一行日誌中 包含 mforground ,再看看是否包含上面的狀態的單詞。既然說的如此,那麼看看讀取系統日誌的代碼吧。
Java代碼- 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
以上代碼中,之所以用線程,是為了防止讀取日誌過程中阻滯主方法的其他方法的執行,影響到程式捕捉對應的電話狀態。
好了,捕捉到了去電過程中各個狀態的轉變,那麼,如何通知給程式呢,我採用的方法是捕獲後立馬給系統發送廣播,然後程式進行廣播接收,接收後再處理錄音事件。要發送廣播,就要發送一個唯一的廣播,為此,建立如下類:
Java代碼- 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"/>
然後,在讀取日誌的類中檢測到去電各個狀態的地方發送一個廣播,那麼,讀取日誌的完整代碼如下:
Java代碼- 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);
- //