Requirement: Set up automatic recording after incoming call.
First set a button, the code is very simple here is no longer given.
Build a class, Recorderservicer extends Service
Package Com.ydl.recorder;import Java.io.ioexception;import Android.app.service;import android.content.Intent; Import Android.media.mediarecorder;import Android.os.ibinder;import Android.telecom.telecommanager;import Android.telephony.phonestatelistener;import Android.telephony.telephonymanager;public class RecorderServicer Extends Service {private Mediarecorder recorder; @Overridepublic ibinder onbind (Intent Intent) {return null;} @Overridepublic void OnCreate () {super.oncreate (); Telephonymanager TM = (Telephonymanager) getsystemservice (telephony_service);//Monitor phone status Tm.listen (new MyListener (), Phonestatelistener.listen_call_state);} Class MyListener extends Phonestatelistener {@Overridepublic void oncallstatechanged (int state, String incomingnumber) { TODO auto-generated Method Stubsuper.oncallstatechanged (state, Incomingnumber); TELEPHONYMANAGER.CALL_STATE_IDLE:SYSTEM.OUT.PRINTLN ("idle"); if (recorder! = null) {recorder.stop ();// Stop recording recorder.release (); recorder = null;} BreAk;case TelephonyManager.CALL_STATE_RINGING:System.out.println ("Bell");//Initialize if (recorder! = null) {recorder = new Mediarecorder (); Recorder.setaudiosource (MediaRecorder.AudioSource.MIC);//microphone//audio// Sourcerecorder.setoutputformat (MediaRecorder.OutputFormat.THREE_GPP);//Set Output 3gp format recorder.setoutputfile ("SDcard /luyin.3gp "); Recorder.setaudioencoder (MediaRecorder.AudioEncoder.AMR_NB);//Set the audio encoding format try {recorder.prepare ();// Prepare} catch (Exception e) {//TODO auto-generated catch Blocke.printstacktrace ();}} Break;case TelephonyManager.CALL_STATE_OFFHOOK:System.out.println ("Off-hook");//Start recording if (recorder! = null) {Recorder.start ();} Default:break;}}}}
Set button click events on the main activity screen: Used to start the above services
Package Com.ydl.recorder;import Android.os.bundle;import Android.app.activity;import android.content.intent;import Android.view.menu;import Android.view.view;public class Mainactivity extends Activity { @Override protected void OnCreate (Bundle savedinstancestate) { super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); } public void Click (View v) { Intent Intent = new Intent (this, recorderservicer.class); StartService (intent); }}
In order to do more realistic, set up the boot service: So the last chapter of the start-up broadcasting mechanism
Create a new class: Bootreceiver extends Broadcastreceiver for start-up service. Because the service is in the background, not to meet, so do not have to set the task stack is not required addflags () method
Package Com.ydl.recorder;import Android.content.broadcastreceiver;import Android.content.context;import Android.content.intent;public class Bootreceiver extends Broadcastreceiver {//Set boot start service @overridepublic void OnReceive ( Context context, Intent Intent) {Intent it = new Intent (context, recorderservicer.class); Context.startservice (it);}}
The above code is the operation of the system, such as: Listen to the phone status, set the power on the radio, write data to SDcard, set the recording mode. Therefore, you need to configure permissions, while the top of the broadcast and services are configured accordingly.
The permissions for the manifest file and the service broadcast registration are as follows:
<uses-permission android:name= "Android.permission.READ_PHONE_STATE"/> <uses-permission android:name = "Android.permission.RECORD_AUDIO"/> <uses-permission android:name= "android.permission.RECEIVE_BOOT_ Completed "/> <uses-permission android:name=" Android.permission.WRITE_EXTERNAL_STORAGE "/>
<service android:name= "Com.ydl.recorder.RecorderServicer" ></service> <receiver android:name= " Com.ydl.recorder.BootReceiver "> <intent-filter > <action android:name=" Android.intent.action.BOOT_COMPLETED "/> </intent-filter> </receiver>
Android Beginner tutorials for phone recording