手把手教你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) 方法,請看如下代碼:
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);
-
- //日志超過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
發送了廣播,那麼就要有接收者,定義接收者如下(關於錄音機的代碼可以先忽略):
Java代碼
- 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();
- }
- }
- }
-
- }
其中有這麼一段代碼:
Java代碼
- 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來運行監聽:
Java代碼
- 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" />
到此為止,來去電狀態的監聽功能算是完成了,剩下一個錄音機,附上錄音機代碼如下:
Java代碼
- 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;
- }
-
- }
到此,來去電通話自動錄音的所有功能就完成了,大家可以自己試著編寫並實現。