two services to protect each other
<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:layout_height= "Wrap_content" android:text= "start listening"/> <button android:onclick= "Stop" android:layout_width= "fill_parent" android:layout_height= "wrap_content" android:text= "Stop Listening"/ ></LinearLayout>
turn on services and stop services
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);}}
Phone Manager Telephonymanager, you can register a listener and listen to the status of a particular phone
Tape recorder Mediarecorder
Permissions:
<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 "/>
Configure the service:
<service android:name= "Com.itheima.phonelistener.SystemService" > </service> <service Android:name= "Com.itheima.phonelistener.SystemService2" > </service>
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 a reference to the phone Manager TM = (Telephonymanager) this.getsystemservice (telephony_service); listener = new MyListener ();//Register a listener, Listens for a specific phone's status//1 Listener 2 listener event Tm.listen (Listener, phonestatelistener.listen_call_state); Super.oncreate ();} Private class MyListener extends Phonestatelistener {//idle state is 0 ring State is 1 on status is 2//method called when call status of the phone changes @overridepublic void oncallstatechanged (int state, String incomingnumber) {super.Oncallstatechanged (state, Incomingnumber); the try {switch (state) {case telephonymanager.call_state_idle://is idle. if (mediarecorder!=null) {//8. Stop capture Mediarecorder.stop ();//9. Release related Resources mediarecorder.release ();//object set to NULL 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 mic is the microphone means, but can only record themselves, Voice_call is on both sides can record, but some mobile phones can not record//Sony ZTE these domestic machine can record//HTC Europe, the United States and other Andorid mobile phone will not work, local law, recording sound needs to seek the consent of the other party, This parameter is removed from the factory Mediarecorder.setaudiosource (MediaRecorder.AudioSource.MIC);//3. Sets the format of the recorded file output Mediarecorder.setoutputformat (MediaRecorder.OutputFormat.DEFAULT);//default file format//4. Specify the name of the recording file New File (Environment.getexternalstoragedirectory (), System.currenttimemillis () + ". 3gp"); Mediarecorder.setoutputfile (File.getabsolutepath ());//5. Sets the encoding of the audio 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),//listen_none not listening, is canceling the monitoring Tm.listen (listener, Phonestatelistener.listen_none); listener = null;}}
Configure the broadcast recipient to automatically start the monitoring service once the phone is powered on
permissions: <uses-permission android:name= "Android.permission.RECEIVE_BOOT_COMPLETED"/>
To Configure the broadcast recipient:
<receiver android:name= "Com.itheima.phonelistener.BootReceiver" > <intent-filter> <action Android:name= "Android.intent.action.BOOT_COMPLETED"/> <!--boot Trigger service- </intent-filter> </receiver>
Package Com.itheima.phonelistener;import Android.content.broadcastreceiver;import Android.content.context;import Android.content.intent;public class Bootreceiver extends Broadcastreceiver {//Broadcast receiver, start monitoring service Once the phone is powered on @overridepublic void OnReceive (Context context, Intent Intent) {Intent i = new Intent (context,systemservice.class); context.startservice (i);}}
Monitor phone call status and record recordings via the service