新网创想网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
这期内容当中小编将会给大家带来有关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(); } } } }