in Android development, the service is used when you need to create a Program running in the background. Service can be divided into two types: infinite life and limited life. Note that the service and activities are different (in simple terms, it can be understood as the difference between the background and the foreground). For example, if you need to use the service, you need to call startservice (), startservice () is used to call the oncreate () and onstart () methods in the service to start a backend service.
the process of starting a service is as follows: context. startservice ()-> oncreate ()-> onstart ()-> Service Running where oncreate () can initialize some services, while onstart () starts the service.
the process of stopping a service is as follows: context. stopservice () |-> ondestroy ()-> service stop
the following example shows a small example of using the background service to play music. Click start to run the service and click STOP to stop the service.
servicesdemo. Java (an activity)
Package COM. android. myservice; import android. app. activity; import android. content. intent; import android. OS. bundle; import android. util. log; import android. view. view; import android. view. view. onclicklistener; import android. widget. button; public class servicedemo extends activity implements onclicklistener {Private Static final string tag = "servicedemo"; button buttonstart, buttonstop; @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); buttonstart = (button) findviewbyid (R. id. buttonstart); buttonstop = (button) findviewbyid (R. id. buttonstop); buttonstart. setonclicklistener (this); buttonstop. setonclicklistener (this);} public void onclick (view SRC) {Switch (SRC. GETID () {case R. id. buttonstart: log. I (TAG, "onclick: starting service"); startservice (new intent (this, myservice. class); break; case R. id. buttonstop: log. I (TAG, "onclick: stopping service"); stopservice (new intent (this, myservice. class); break ;}}}
In addition, the Service must be declared in manifest: (androidmanifest. XML)
<? XML version = "1.0" encoding = "UTF-8"?> <Manifest xmlns: Android = "http://schemas.android.com/apk/res/android" package = "com. android. myservice "> <application Android: Label =" @ string/app_name "> <activity Android: Name = ". servicedemo "Android: Label =" @ string/app_name "> <intent-filter> <action Android: Name =" android. intent. action. main "/> <category Android: Name =" android. intent. category. launcher "/> </intent-filter> </activity> <service android: enabled =" true "Android: Name = ". myservice "/> </Application> </manifest>
Define Service (myservice. Java)
Package COM. android. myservice; import android. app. service; import android. content. intent; import android. media. mediaplayer; import android. OS. ibinder; import android. util. log; import android. widget. toast; public class myservice extends Service {Private Static final string tag = "myservice"; mediaplayer player; @ override public ibinder onbind (intent) {return NULL ;} @ override public void oncreate () {toast. maketext (this, "my service created", toast. length_long ). show (); log. I (TAG, "oncreate"); player = mediaplayer. create (this, R. raw. braincandy); player. setlooping (false) ;}@ override public void ondestroy () {toast. maketext (this, "my service stoped", toast. length_long ). show (); log. I (TAG, "ondestroy"); player. stop () ;}@ override public void onstart (intent, int startid) {toast. maketext (this, "my service start", toast. length_long ). show (); log. I (TAG, "onstart"); player. start ();}}
Main. XML is in the layout folder.
<? XML version = "1.0" encoding = "UTF-8"?> <Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" Android: Orientation = "vertical" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent" Android: gravity = "center"> <textview Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: text = "@ string/services_demo" Android: gravity = "center" Android: textsize = "20sp" Android: padding = "20dp"/> <button Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: id = "@ + ID/buttonstart" Android: text = "@ string/start"> </button> <button Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: text = "@ string/Stop" Android: Id = "@ + ID/buttonstop"> </button> </linearlayout>
The value folder is strings. xml.
<? XML version = "1.0" encoding = "UTF-8"?> <Resources> <string name = "start"> Start </string> <string name = "stop"> stop </string> <string name = "services_demo"> Service Demo </string> </resources>