After starting the service, monitor the phone Telephonymanager status, according to different circumstances to make a choice, the source code as follows:
Generated audio files:
Log logs:
This does not know why, click to stop the service multiple times, will always appear the following bug:
The picture is not clear, I copied the log out as follows:
09-21 19:56:31.850:error/audio_input (Unsupported): parameter:x-pvmf/media-input-node/cap-config-interface; Valtype=key_specific_value
09-21 19:56:31.850:error/audio_input (): Verifyandsetparameter failed
Know the reason, trouble tell me, Gray often grateful ... I Baidu for a long time do not know why.
Source:
Mainactivity:
PackageCom.song; Importandroid.app.Activity; Importandroid.content.Intent; ImportAndroid.os.Bundle; ImportAndroid.util.Log; ImportAndroid.view.View; ImportAndroid.widget.Button; Public classPhonelisteneractivityextendsActivity {protected Static FinalString ACTION = "Recordingflag"; protected Static FinalString tag = "Tag"; Button btnstart; Button Btnstop; @Override Public voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.main); Btnstart=(Button) Findviewbyid (R.id.btnstart); Btnstop=(Button) Findviewbyid (r.id.btnstop); Btnstart.setonclicklistener (NewButton.onclicklistener () {@Override Public voidOnClick (View v) {log.v (TAG,"StartService"); StartService (NewIntent (ACTION)); } }); Btnstop.setonclicklistener (NewButton.onclicklistener () {@Override Public voidOnClick (View v) {log.v (TAG,"StopService"); StopService (NewIntent (ACTION)); } }); } }
Service class:
PackageCom.song; Importjava.io.IOException; ImportAndroid.app.Service; ImportAndroid.content.Context; Importandroid.content.Intent; ImportAndroid.media.MediaRecorder; Importandroid.os.Environment; ImportAndroid.os.IBinder; ImportAndroid.telephony.PhoneStateListener; ImportAndroid.telephony.TelephonyManager; ImportAndroid.util.Log; Public classPhonelistenerserviceextendsService {PrivateMediarecorder Recorder; Private BooleanRecording =false; @Override Publicibinder onbind (Intent Intent) {return NULL; } @Override Public voidonCreate () {Telephonymanager Manager=(Telephonymanager) Getsystemservice (Context.telephony_service); Manager.listen (Listener, phonestatelistener.listen_call_state); Super. OnCreate (); } PrivatePhonestatelistener listener =NewPhonestatelistener () {@Override Public voidOncallstatechanged (intState , String Incomingnumber) { Super. oncallstatechanged (State, Incomingnumber); Switch(state) { CaseTelephonymanager.call_state_idle://idle or hang-off//Stop Recording when you hang up.Stoprecord (); Break; CaseTelephonymanager.call_state_ringing://Bells//bells don't do anything. Break; CaseTelephonymanager.call_state_offhook://pick up the phone//pick up the phone and start recording .recordcalling (); Break; default: Break; } } }; Private voidStoprecord () {if(recording) {LOG.V ("TAG", "Stoprecord"); Recorder.stop (); Recorder.release ();//Freeing Resources } } Private voidrecordcalling () {Try{LOG.V ("TAG", "recordcalling"); Recorder=NewMediarecorder (); Recorder.setaudiosource (MediaRecorder.AudioSource.MIC); Recorder.setoutputformat (MediaRecorder.OutputFormat.THREE_GPP); Recorder.setaudioencoder (MediaRecorder.AudioEncoder.AMR_NB); Recorder.setoutputfile (Environment.getexternalstoragedirectory (). GetAbsolutePath ()+ "/" + system.currenttimemillis () + ". 3gp"); Recorder.prepare (); Recorder.start (); Recording=true; }Catch(Exception e) {e.printstacktrace (); } } }
Configuration file:
<?xml version= "1.0" encoding= "Utf-8"?> <manifest xmlns:android= "Http://schemas.android.com/apk/res/android" Package= "Com.song"Android:versioncode= "1"Android:versionname= "1.0" > <uses-sdk android:minsdkversion= "8"/> <application android:icon= "@drawable/icon" Android: Label= "@string/app_name" > <activity android:name= ". Phonelisteneractivity "Android:label= "@string/app_name" > <intent-filter> <action android:name= "android.intent.action . MAIN "/> <category android:name=" Android.intent.category.LAUNCHER "/> </intent-f ilter> </activity> <service android:name= ". Phonelistenerservice "> <intent-filter> <action android:name=" Recordingflag "/> </intent-filter> </service> </application> <uses-permission android:name= "Android Oid.permission.READ_PHONE_STATE "/> <uses-permission android:name=" Android.permission.RECORD_AUDIO "/> &L t;! --Write data to SDcard and <uses-permission android:name= "Android.permission.WRITE_EXTERNAL_STORAGE"/> </man Ifest>
Android Services (Service) preliminary--Call recording