好了,捕捉到了去電過程當中各個狀態的轉變,那麼,如何通知給程序呢,我採用的方法是捕獲後立馬給系統發送廣播,而後程序進行廣播接受,接受後在處理錄音事件。要發送廣播,就要發送一個惟一的廣播,爲此,創建以下類,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", "開始監聽呼出狀態的轉變,並在對應狀態發送廣播");
- }
- }
程序須要讀取系統日誌權限android
- <uses-permission android:name="android.permission.READ_LOGS"/>
而後,在讀取日誌的類中檢測到去電各個狀態的地方發送一個廣播,那麼,讀取日誌的完整代碼以下
app
- 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
發送了廣播,那麼就要有接受者,定義接收者以下
(關於錄音機的代碼能夠先忽略)ide
- 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();
- }
- }
- }
- }
其中有這麼一段代碼this
- 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);
- }
這裏是接收系統發出的廣播,用於接收去電廣播。這樣,就得到了去電狀態。spa
三、有了以上主要代碼,能夠說,來去電監聽功能算是完成了,下面建立一個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
code
- <service android:name="com.sdvdxl.service.PhoneCallStateService" />
到此爲止,來去電狀態的監聽功能算是完成了,剩下一個錄音機,附上錄音機代碼以下
orm
- 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;
- }
- }
寫到這兒,算是完成了來去電自動錄音的全部功能,若有不懂,或者更好的建議歡迎評論。xml
另附 工程源代碼