Service is one of the four components of the Android system that runs in the background for an indefinite period of time and does not interact with the user application component.
Service can be used in many applications, such as multimedia playback when the user initiates other activity this time the program to continue to play in the background, such as the detection of changes in the file on the SD card and so on.
Life cycle
Context.startservice () Start process:
Context.startservice (), OnCreate (), OnStart (), Service running, Context.stopservice (), O Ndestroy (), Service stop
If the service is not running, Android calls OnCreate () first and then calls OnStart ();
If the service is already running, only OnStart () is called, so a service's OnStart method may be called repeatedly.
If the stopservice will be directly OnDestroy, if the caller is directly exiting without calling StopService, the service will be running in the background, The service can be shut down by StopService after the caller has started up again.
So the life cycle of calling StartService is: onCreate---OnStart (can be called multiple times) --OnDestroy
Context.bindservice () Start process:
Context.bindservice (), OnCreate (), Onbind (), Service running, Onunbind (), OnDestroy () Service stop
Onbind () returns a Ibind interface instance to the client, Ibind a method that allows the client to callback the service, such as getting an instance of the services, running state, or other operations. This time the caller (context, for example, activity) is bound together with the service, the context exits, Srevice will call Onunbind->ondestroy exit accordingly.
So the lifetime of the call Bindservice is: onCreate---Onbind (one time, not multiple bindings)--onunbind--and ondestory.
Only OnStart can be called multiple times (through multiple StartService calls) during each turn-off of the service, and the other oncreate,onbind,onunbind,ondestory can only be called once in a life cycle.
Turn off
Service launches are available in two ways:context.startservice () and context.bindservice ()
Service shutdown is available in two ways:context.stopservice () and context.unbindservice ()
Engineering
<android:name= ". Phonestatusservice "></service>
Content of Service:
Packagecom.yydcdut.servicestudy1;Importjava.io.IOException;ImportAndroid.app.Service;Importandroid.content.Intent;ImportAndroid.media.MediaRecorder;ImportAndroid.os.IBinder;ImportAndroid.telephony.PhoneStateListener;ImportAndroid.telephony.TelephonyManager; Public classPhonestatusserviceextendsService {@Override Publicibinder onbind (Intent Intent) {return NULL; } @Override Public voidonCreate () {System.out.println ("OnCreate"); Super. OnCreate (); } @Override Public voidOnStart (Intent Intent,intStartid) {System.out.println ("OnStart"); //Monitor the change of phone statusTelephonymanager TM =(Telephonymanager) Getsystemservice (Telephony_service); Tm.listen (NewMyphonestatuslistener (), phonestatelistener.listen_call_state); Super. OnStart (Intent, Startid); } @Override Public intOnstartcommand (Intent Intent,intFlagsintStartid) {System.out.println ("Onstartcommand"); return Super. Onstartcommand (Intent, flags, Startid); } @Override Public voidOnDestroy () {System.out.println ("OnDestroy"); Super. OnDestroy (); }}classMyphonestatuslistenerextendsphonestatelistener{PrivateMediarecorder Recorder; @Override Public voidOncallstatechanged (intState , String Incomingnumber) { Switch(state) { CaseTelephonymanager.call_state_idle://Idle if(Recorder! =NULL) {recorder.stop (); Recorder.reset (); Recorder.release (); Recorder=NULL; } Break; CaseTelephonymanager.call_state_ringing://BellsSYSTEM.OUT.PRINTLN ("Discovery call!!!!!"); if("5556". Equals (Incomingnumber)) {System.out.println ("Hang up the phone ..."); } Recorder=NewMediarecorder (); Recorder.setaudiosource (MediaRecorder.AudioSource.MIC); Recorder.setoutputformat (MediaRecorder.OutputFormat.THREE_GPP); Recorder.setaudioencoder (MediaRecorder.AudioEncoder.AMR_NB); Recorder.setoutputfile ("/data/data/com.yydcdut.servicestudy1/" +system.currenttimemillis () + ". 3gp"); Try{recorder.prepare (); } Catch(IllegalStateException e) {//TODO Auto-generated catch blockE.printstacktrace (); } Catch(IOException e) {//TODO Auto-generated catch blockE.printstacktrace (); } Break; CaseTelephonymanager.call_state_offhook://Call Status if(Recorder! =NULL) Recorder.start (); Break; default: Break; } Super. oncallstatechanged (State, Incomingnumber); } }
Main interface Two button to turn service on and off:
Public void Click (View view) { new Intent (Getapplicationcontext (), Phonestatusservice. Class); StartService (intent); } Public void Click2 (view view) { new Intent (Getapplicationcontext (), Phonestatusservice. Class); StopService (intent); }
About the life cycle:
I'm the dividing line of the king of the Land Tiger.
Source code: HTTP://PAN.BAIDU.COM/S/1DD1QX01
Service Learning 1.zip
Reprint Please specify source: Http://www.cnblogs.com/yydcdut