First come to the point of time a total of three states, idle, ringing, off-hook (answer phone)
<uses-permission android:name= "Android.permission.READ_PHONE_STATE"/>
<uses-permission android:name= "Android.permission.RECORD_AUDIO"/>
<uses-permission android:name= "Android.permission.WRITE_EXTERNAL_STORAGE"/>
Permissions are for phone operations, recording permissions, SD card permissions, respectively
To start the service, you can use the radio recipient to do it.
PackageCom.example.luyin;ImportAndroid.app.Service;Importandroid.content.Intent;ImportAndroid.media.MediaRecorder;ImportAndroid.os.IBinder;ImportAndroid.telephony.PhoneStateListener;ImportAndroid.telephony.TelephonyManager;ImportAndroid.widget.Toast; Public classMyServiceextendsService {PrivateMediarecorder Recorder; PublicMyService () {} @Override Publicibinder onbind (Intent Intent) {//Todo:return The communication channel to the service. Throw NewUnsupportedoperationexception ("not yet implemented"); } @Override Public voidonCreate () {Toast.maketext (myservice. This, "service Start", 1). Show (); //TODO auto-generated Method Stub Super. OnCreate (); //get the phone manager to monitor the status of the phoneTelephonymanager Manager =(Telephonymanager) Getsystemservice (Telephony_service); //Event Settings phone only monitor phone status changeManager.listen (NewListen (), phonestatelistener.listen_call_state); } Private classListenextendsPhonestatelistener {//phone status once changed call//use the return value of state to determine if there are any calls.@Override Public voidOncallstatechanged (intState , String Incomingnumber) { //TODO auto-generated Method Stub Super. oncallstatechanged (State, Incomingnumber); Switch(state) {//Idle CaseTelephonyManager.CALL_STATE_IDLE:Toast.makeText (myservice. This, "Idle", 1). Show (); if(recorder!=NULL){ //recording ends and releasing resourcesRecorder.stop (); Recorder.release (); Recorder=NULL; } Break; //Bells CaseTelephonyManager.CALL_STATE_RINGING:Toast.makeText (myservice. This, "Bells", 1). Show (); if(Recorder = =NULL) { //Tape recorderRecorder =NewMediarecorder (); //set the recording source to be the microphoneRecorder.setaudiosource (MediaRecorder.AudioSource.MIC); //set the format of the recording to be savedRecorder.setoutputformat (MediaRecorder.OutputFormat.THREE_GPP); //Set Save pathRecorder.setoutputfile ("sdcard/voice.3gp"); //Set EncodingRecorder.setaudioencoder (MediaRecorder.AudioEncoder.AMR_NB); //prepare for recording . Try{recorder.prepare (); } Catch(Exception e) {//TODO auto-generated Catch blockE.printstacktrace (); } } Break; //Answer CaseTelephonyManager.CALL_STATE_OFFHOOK:Toast.makeText (myservice. This, "Off-hook", 1). Show (); if(recorder!=NULL){ //Start RecordingRecorder.start (); } Break; } } }}
<?XML version= "1.0" encoding= "Utf-8"?><Manifestxmlns:android= "Http://schemas.android.com/apk/res/android" Package= "Com.example.luyin"Android:versioncode= "1"Android:versionname= "1.0" > <USES-SDKandroid:minsdkversion= "8"android:targetsdkversion= "+" /> <uses-permissionAndroid:name= "Android.permission.READ_PHONE_STATE"/> <uses-permissionAndroid:name= "Android.permission.RECORD_AUDIO"/> <uses-permissionAndroid:name= "Android.permission.WRITE_EXTERNAL_STORAGE"/> <ApplicationAndroid:allowbackup= "true"Android:icon= "@drawable/ic_launcher"Android:label= "@string/app_name"Android:theme= "@style/apptheme" > <ActivityAndroid:name= "Com.example.luyin.MainActivity"Android:label= "@string/app_name" > <Intent-filter> <ActionAndroid:name= "Android.intent.action.MAIN" /> <categoryAndroid:name= "Android.intent.category.LAUNCHER" /> </Intent-filter> </Activity> <ServiceAndroid:name= "Com.example.luyin.MyService" > </Service> </Application></Manifest>
Service phone recording of Android four components