Requirements Analysis:Record the recording during the call and save to the specified path ~
Knowledge Points: 1. The mobile phone starts broadcasting, the broadcast recipient receives the notification, opens the service service 2.Service work flow:
1. Get the phone management object ·
2. Register the Listener, corresponding to the phone status
3. According to the phone status (ringing, calling, spare), create Mediaplay object, pause here to monitor the status of the phone, join? permissions <uses-permission android:name= "Android.permission.READ_ Phone_state "/> Code:Telephonymanager manager= (Telephonymanager) Getsystemservice (telephony_service);//Get Phone managementManager.listen (New Myphonestatelistener (), phonestatelistener.listen_call_state);//Use the Phone Manager to register a listener to monitor phone statusNote: The phone status monitoring this, can listen to multiple states,listen_call_state Monitoring is the change in the status of the phone, the following listener other listenersState three values for phone status change: Spare, call, hang up
According to three states, processing MediaPlayer, code
Public void oncallstatechanged (int state, String incomingnumber) {//Phone state changes when you run the methodswitch (state) { case telephonymanager.call_state_ringing:log.d ("Debug", "ringing"); num = incomingnumber; / /When ringing, record number break; case Telephonymanager.call_state_offhook:log.d ("Debug", "Start recording"); Startrecording (); / //Pick-up, start recording break; case Telephonymanager.call_state_idle:log.d ("Debug", "spare status"); Stoprecording (); / /Spare, end recording break;}}think: Suppose the phone doesn't ring? Stop recording:private void stoprecording () {if (Mrecorder! = null) {Mrecorder.stop ();Stop it Mrecorder.release (); Freeing resources Mrecorder = null;Garbage collection}} Start Recording:private void startrecording () {try {LOG.D ("Debug", "startrecording");Mrecorder = new Mediarecorder (); Create Media recorderMrecorder.setaudiosource (MediaRecorder.AudioSource.MIC);Set up an audio sourceMrecorder.setoutputformat (MediaRecorder.OutputFormat.THREE_GPP);//Set Output formatMrecorder.setoutputfile ("/mnt/sdcard/" + num + "_" + system.currenttimemillis () + ". 3gp"); Set Output file pathMrecorder.setaudioencoder (MediaRecorder.AudioEncoder.AMR_NB);//Set encodingMrecorder.prepare ();Get readyMrecorder.start ();Start} catch (Exception e) {E.printstacktrace ();} }}}
Summary: MediaPlayer operation process, stop and recording process ~, here used to sdcard so, you must declare permissions.
<uses-permission android:name= "Android.permission.RECORD_AUDIO"/>//Consent Program for recording
<uses-permission android:name= "Android.permission.WRITE_EXTERNAL_STORAGE"/>//write memory cardconfiguration information for the manifest file:<receiver android:name= ". Phonebroacast "><intent-filter ><action android:name= "Android.intent.action.BOOT_COMPLETED"/></intent-filter></receiver><service android:name= ". Phoneservice "></service>