首页 > 代码库 > 监听电话录音

监听电话录音

package com.example.calling;
import java.io.IOException;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.media.MediaRecorder;
import android.os.IBinder;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
public class PhoneStatusService extends Service {
   private TelephonyManager tm;
   private MyPhoneStatusListener  mPhoneStatusListener;
   private MediaRecorder recorder;
   @Override
   public IBinder onBind(Intent intent) {
      return null;
   }
   @Override
   public void onCreate() {
     super.onCreate();
      // 1. 获取手机管理器
      tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
      // 2. 设置监听
      mPhoneStatusListener = new MyPhoneStatusListener();
      tm.listen(mPhoneStatusListener, PhoneStateListener.LISTEN_CALL_STATE);
   }
   /**
    * 3. 用内部类实现监听
    * 手机状态监听
    */
   private class MyPhoneStatusListener extends PhoneStateListener {
      //当手机通话状态变化时,回调
      //state  :通话的3中状态
      //incomingNumber:来电号码
      @Override
      public void onCallStateChanged(int state, String incomingNumber) {
         super.onCallStateChanged(state, incomingNumber);
         System.out.println("来电号码:"+incomingNumber);
         switch (state) {
            case TelephonyManager.CALL_STATE_IDLE://空闲
                System.out.println("空闲");
                if (recorder != null) {
                    //8.停止录音
                    recorder.stop();
                    //9.重置
//                 recorder.reset();   // You can reuse the object by going back to setAudioSource() step
                    //10. 释放资源
                    // 释放c申请的内存,释放java
                    recorder.release(); // Now the object cannot be reused
                    recorder = null;
                }
                break;
            case TelephonyManager.CALL_STATE_RINGING://响铃
                System.out.println("响铃");
                break;
            case TelephonyManager.CALL_STATE_OFFHOOK://通话
                System.out.println("通话");
                //1. 创建录音机
                 recorder = new MediaRecorder();
                 //2.设置音频来源
                 recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
                 //3. 设置音频格式
                 recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
                 //4. 设置音频的编码方式
                 recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
                 //5. 录音的文件保存位置
                 recorder.setOutputFile("/mnt/sdcard/luyin.3gp");
                 try {
                    //6. 准备录音
                   recorder.prepare();
                } catch (IllegalStateException e) {
                   e.printStackTrace();
                } catch (IOException e) {
                   e.printStackTrace();
                }
                 //7.开始录音
                 recorder.start();   // Recording is now started
                break;
            default:
                break;
         }
      }
   }
   @Override
   public void onDestroy() {
      super.onDestroy();
      //4. 注销监听
      tm.listen(mPhoneStatusListener, PhoneStateListener.LISTEN_NONE);
   }
}

package com.example.calling;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
public class MainActivity extends Activity {
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      startService(new Intent(this,PhoneStatusService.class));
   }
}

清单文件中:
  application标签中
    <service android:name="com.example.calling.PhoneStatusService" ></service>
  manifest标签中
    <uses-permission android:name="android.permission.READ_PHONE_STATE"/>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.RECORD_AUDIO"/>

 

监听电话录音