Android编程实现通话录音功能的方法

本文实例讲述了Android编程实现通话录音功能的方法。分享给大家供大家参考,具体如下:

因受系统限制,只能录自已麦的声音,录不到对方的声音,可能需要改内核才能实现双向录音;

接通电话和挂断电话时,震动一下;

使用广播接收者实现自启动;

服务代码:

package com.eboy.phoneListener;
import java.io.File;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.media.MediaRecorder;
import android.os.Environment;
import android.os.IBinder;
import android.os.Vibrator;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.widget.Toast;
public class phoneService extends Service {
  @Override
  public IBinder onBind(Intent arg0) {
    // TODO Auto-generated method stub
    return null;
  }
  @Override
  public void onCreate() {
    // TODO Auto-generated method stub
    super.onCreate();
    TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
    telephonyManager.listen(new PhoneListener(), PhoneStateListener.LISTEN_CALL_STATE);
  }
  private final class PhoneListener extends PhoneStateListener{
    private MediaRecorder mediaRecorder;
    private File file;
    private Vibrator vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
    @Override
    public void onCallStateChanged(int state, String incomingNumber) {
      try {
        switch (state){
        case TelephonyManager.CALL_STATE_RINGING: //来电
          break;
        case TelephonyManager.CALL_STATE_OFFHOOK: //接通电话
          file = new File(Environment.getExternalStorageDirectory(), incomingNumber + System.currentTimeMillis() + ".3gp");
          mediaRecorder = new MediaRecorder();
          mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
          mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
          mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
          mediaRecorder.setOutputFile(file.getAbsolutePath());
          mediaRecorder.prepare();
          mediaRecorder.start();
          Toast.makeText(getApplicationContext(), "电话已接通,开始录音。", Toast.LENGTH_SHORT).show();
          //震动一下
          vibrator.vibrate(100);
          break;
        case TelephonyManager.CALL_STATE_IDLE: //挂断电话
          if (mediaRecorder != null){
            mediaRecorder.stop();
            mediaRecorder.release();
            mediaRecorder = null;
            Toast.makeText(getApplicationContext(), "电话已挂断,录音停止。", Toast.LENGTH_SHORT).show();
            //震动一下
            vibrator.vibrate(100);
          }
          break;
        }
      }catch (Exception e) {
        e.printStackTrace();
      }
    }
  }
}

通过广播接收者实现自启动,代码:

package com.eboy.phoneListener;
import android.content.Context;
import android.content.Intent;
public class BroadcastReceiver extends android.content.BroadcastReceiver {
  @Override
  public void onReceive(Context arg0, Intent arg1) {
    Intent service = new Intent(arg0, phoneService.class);
    arg0.startService(service);
  }
}

main.xml:


  
  
    
    
      
        
      
    
  
  
  
  
  
  
  
  
  
  


更多关于Android相关内容感兴趣的读者可查看本站专题:《Android多媒体操作技巧汇总(音频,视频,录音等)》、《Android开发入门与进阶教程》、《Android视图View技巧总结》、《Android编程之activity操作技巧总结》、《Android操作json格式数据技巧总结》、《Android文件操作技巧汇总》、《Android资源操作技巧汇总》及《Android控件用法总结》

希望本文所述对大家Android程序设计有所帮助。

你可能感兴趣的:(Android编程实现通话录音功能的方法)