标签:
需求:设置来电后自动录音。
首先设置一个按钮,代码很简单这里就不再给出。
建一个类,RecorderServicer extends Service
package com.ydl.recorder; import java.io.IOException; import android.app.Service; import android.content.Intent; import android.media.MediaRecorder; import android.os.IBinder; import android.telecom.TelecomManager; import android.telephony.PhoneStateListener; import android.telephony.TelephonyManager; public class RecorderServicer extends Service { private MediaRecorder recorder; @Override public IBinder onBind(Intent intent) { return null; } @Override public void onCreate() { super.onCreate(); TelephonyManager tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE); //监听电话状态 tm.listen(new MyListener(), PhoneStateListener.LISTEN_CALL_STATE); } class MyListener extends PhoneStateListener { @Override public void onCallStateChanged(int state, String incomingNumber) { // TODO Auto-generated method stub super.onCallStateChanged(state, incomingNumber); switch (state) { case TelephonyManager.CALL_STATE_IDLE: System.out.println("空闲"); if (recorder != null) { recorder.stop();// 停止录音 recorder.release(); recorder = null; } break; case TelephonyManager.CALL_STATE_RINGING: System.out.println("响铃"); // 进行初始化 if (recorder != null) { recorder = new MediaRecorder(); recorder.setAudioSource(MediaRecorder.AudioSource.MIC);// Microphone // audio // source recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);// 设置输出3gp格式 recorder.setOutputFile("sdcard/luyin.3gp"); recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);// 设置音频编码格式 try { recorder.prepare();// 做好准备 } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } break; case TelephonyManager.CALL_STATE_OFFHOOK: System.out.println("摘机"); // 开始录音 if (recorder != null) { recorder.start(); } default: break; } } } }
package com.ydl.recorder; import android.os.Bundle; import android.app.Activity; import android.content.Intent; import android.view.Menu; import android.view.View; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void click(View v){ Intent intent = new Intent(this, RecorderServicer.class); startService(intent); } }为了做的更逼真一些,设置开机启动服务:因此用到了上一章的开机广播机制
新建一个类:BootReceiver extends BroadcastReceiver 用于开机启动服务。因为服务是在后台的,不可见面,因此不必设置任务栈即不需要addFlags()方法
package com.ydl.recorder; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; public class BootReceiver extends BroadcastReceiver { //设置开机启动服务 @Override public void onReceive(Context context, Intent intent) { Intent it = new Intent(context, RecorderServicer.class); context.startService(it); } }
清单文件的权限和服务广播注册如下:
<uses-permission android:name="android.permission.READ_PHONE_STATE"/> <uses-permission android:name="android.permission.RECORD_AUDIO"/> <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<service android:name="com.ydl.recorder.RecorderServicer"></service> <receiver android:name="com.ydl.recorder.BootReceiver"> <intent-filter > <action android:name="android.intent.action.BOOT_COMPLETED"/> </intent-filter> </receiver>
标签:
原文地址:http://blog.csdn.net/qq_32059827/article/details/51689061