The use of Android programming services to realize the method of telephone listening _android

Source: Internet
Author: User
Tags prepare

This article describes the application of Android programming to the use of services to achieve telephone monitoring method. Share to everyone for your reference, specific as follows:

1. Launch Simulator, deploy application
2. Use simulator controller to send SMS Start service (check log output to determine success)
3. To the simulator to call, and answer, hang up the phone, use file management to view the corresponding cache directory or sdcard generated 3gp files, and copy it to the PC playback to verify.

Manifest settings (one receiver, one service, several permissions)

<uses-permission android:name= "Android.permission.READ_PHONE_STATE"/>
<uses-permission android:name = "Android.permission.RECORD_AUDIO"/>
<uses-permission android:name= "android.permission.MOUNT_UNMOUNT_ Filesystems "/>
<uses-permission android:name=" Android.permission.WRITE_EXTERNAL_STORAGE "/>"
<uses-permission android:name= "Android.permission.RECEIVE_SMS"/>
...
<application ...>
<receiver android:name= "Bootbroadcastreceiver" >
  <intent-filter>
    <action android:name= "Android.provider.Telephony.SMS_RECEIVED"/>
  </intent-filter>
</ receiver>
<service android:name= "Phonelistenerservice"/>

Phone Listener service class: Phonelistenerservice

Package Cn.class3g.phonelistener;
 ... public class Phonelistenerservice extends Service {public IBinder onbind (Intent arg0) {return null;
 public void OnCreate () {super.oncreate ();
 LOG.I ("TAG", "service Started");
 The caller status of the telephone is monitored telephonymanager Telmanager = (telephonymanager) this.getsystemservice (Context.telephony_service);
 Register a listener to monitor the status of the phone Telmanager.listen (new Myphonestatelistener (), phonestatelistener.listen_call_state); 
 Private class Myphonestatelistener extends Phonestatelistener {mediarecorder recorder; 
 File Audiofile; 
 String PhoneNumber; public void oncallstatechanged (int state, String incomingnumber) {switch (state) {case telephonymanager.call_state_id LE:/* No State when/if (recorder!= null) {recorder.stop ()//Stop Burning Recorder.reset ()//reset Recorder.release ();//burn complete must be released 
 Put resources} break; 
 Case Telephonymanager.call_state_offhook:/* When the phone is picked up//try {recorder = new mediarecorder (); Recorder.setaudiosource (MediaRecorder.AudioSource.MIC); Set Audio capture Original recOrder.setoutputformat (MediaRecorder.OutputFormat.THREE_GPP);//Content Output format Recorder.setaudioencoder ( MediaRecorder.AudioEncoder.AMR_NB); 
 Audio Coding mode//Recorder.setoutputfile ("/sdcard/myvoice.amr"); Audiofile = new File (Environment.getexternalstoragedirectory (), PhoneNumber + "_" + system.currenttimemillis () + ". 
 3GP ");
 Recorder.setoutputfile (Audiofile.getabsolutepath ()); 
 LOG.I ("TAG", Audiofile.getabsolutepath ()); Recorder.prepare (); 
 Expected to prepare Recorder.start (); 
 catch (IllegalStateException e) {e.printstacktrace (); 
 catch (IOException e) {e.printstacktrace (); 
 } break; 
 Case telephonymanager.call_state_ringing: * * When the phone came in/PhoneNumber = Incomingnumber; 
 Break 
 Default:break; 
 } super.oncallstatechanged (state, Incomingnumber);

 } 
 } 
}

Start the service with a boot up broadcast (for the sake of convenience, you can switch to short message broadcasting in the experimental environment)

Power-on Start completes broadcast action

Copy Code code as follows:
<action android:name= "Android.intent.action.BOOT_COMPLETED"/>

Package Cn.class3g.phonelistener; 
... 
public class Bootbroadcastreceiver extends Broadcastreceiver {public 
 void OnReceive (context context, Intent Intent) { 
 log.i ("TAG", "Broadcast received"); 
Intent serviceintent = new Intent (context, phonelistenerservice.class); 
 Context.startservice (serviceintent); 
 } 
 

I hope this article will help you with the Android program.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.