What is a service?
First, Service
1.Service IntroductionService is one of Android's four components, service and activity components are similar, both represent the executable program and have its own life cycle, The only difference is that the activity component provides a convenient interface for human-computer interaction while the service runs in the background with no interface. It is important to note that the service is not a separate process or a separate thread in order to prevent the application from responding with a non-reactive error, which runs in the main thread of its managed process like other application objects. Of course, if we want our service to be able to run MP3 or network downloads in the background, we can create a thread to implement it.
life cycle of 2.Servicebecause the service can be started in two ways: Context.startservice () and Context.bindservice ().(1) Context.startservice () method: The service is started by this method, there is no association between the Visitor (client) and the service, even if the visitor exits, the service is still running. called by the Context.startservice () trigger.Servicelife cycle methods:onCreate ()->onstartcommand (Intent, int, int),service run , Context.stopservice () or stopself () ->service is closed .(2) Context.bindservice () method: The service is started by this method, the visitor (client) is bound to the service, and once the visitor exits, the service terminates. Call Context.startservice () to trigger the service life cycle method:onCreate ()->onbind (Intent), client bound to service->onunbind ()->ondestory ()->service closed sublimation Note 1:1. The Onstartcommand (Intent, int, int) method is not executed when the Context.bindservice () method is invoked to start a service;2. When the activity (client's Activiy) calls Bindservice () to bind athe service that was started(activity initially starts with the StartService () method), the system simply passes the service internal IBinder object (returned by the Onbind () method) to Acitvity, and does not fully "bind" the service life cycle to the activity, Thus when the activity calls the Unbindservice () method to cancel binding with the service, it simply disconnects the activity from the service and cannot stop the service component.
3.api-servicePublic abstract class Service (1) Succession relationsJava.lang.Object? Android.content.Context? Android.content.ContextWrapper? Android.app.Service(2) Construction methodService ()However, we typically use the Content.getservice () method to get the service class object when we develop the service. (3) Common methods (service component method)void OnCreate (): will be called immediately after the service is created for the first time;public int Onstartcommand (Intent Intent, int flags, int startid): Each time the client calls the StartService (Intent) method to start the specified service, the client passes in the parameter: Intent: "Intent" passed in by StartService (Intent).Public Abstract Boolean stopservice (Intent service): The client calls the method to close the servicePublic final void stopself (): Service automatically shuts downPublic abstract IBinder onbind (Intent Intent): This method is a method that the service subclass must implement, which returns an IBinder object that the client application can use to communicate with the service component. The parameter intent object is used to bind the client to the service and pass it to Context.bindservice. boolean onunbind (Intent Intent): This method is called when all clients bound by the service are disconnectedvoid Ondestory (): The method will be called before the service is closed, and the service will clean up all the resources it occupies (including all threads, the recipients registered on the service).
second, the basic idea of developing service
1. Create, configure service(1) Define a subclass that inherits from the service, and if you want the service component to do something, we just need to define the relevant business code in the OnCreate () or Onstartcommand () method . the framework for a service component is as follows:\src\service\Firstservice.java
public class FirstService extends service{/*a. A method that must be implemented to return a IBinder object to the client for communication */@Override public ibinder onbind (Intent arg0) {return null; }/*b.service is created when the method is invoked/@Override public void OnCreate () {super.oncreate (); System.out.println ("Service is Created"); The method is called back when/*c.service is started */@Override public int Onstartcommand (Intent intent,int flags,int startid) {Syt Em.out.println ("Service is Started"); return start_sticky; }/*d.service was closed before the callback */@Override public void OnDestroy () {Super.ondestroy (); System.out.println ("Service is destroyed"); }}
2. Configure the service in the Androidmanifest.xml fileafter defining the above service, you need to configure the service in the Androidmanifest.xml file to configure the service to use <service. The/> element. Similar to configuring activity, the service can also be configured as <service. /> Element Configuration <intent-filter: A/> child element that describes which intent the service can be started .
<!--Configure a service component--><service android:name= ". FirstService "> <intent-filter> <!--configure action--> for Intent-filter of the service component <action Android:name= "Com.example.service.FIRST_SERVICE"/> </intent-filter></service>
Sublimation NOTE 2:Service is the most common component in Android's four components, and they all represent executable programs. So, development service is similar to development activity, we just need to define a subclass that inherits the service and after configuring the service in the Androidmanifest.xml file, we can run the Serviec in the program. How do I start a service? We can start a service either through the Context.startservice () method or the Context.bindservice () method in the activity subclass of this application, or in the activity subclass of another applicationthe Context.startservice () method or the Context.bindservice () method starts the service.
3. Start and stop servicewhen service development is complete, we can start the service in the activity subclass of our app, or we can start the service in another app. Here, we start (close) the service in this application, using the Content.startservice (Intent Intent) method. \src\service\startservice
public class Startservicetest extends activity{Button start,stop; @Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.main); Gets the start, stop two buttons in the program interface button start = (Button) Findviewbyid (R.id.start); Stop = (Button) Findviewbyid (r.id.stop); Create Intent final Intent Intent = new Intent () to start the service; Set the Action property intent.setaction ("Com.example.service.FIRST_SERVICE") for intent; Start.setonclicklistener (New Onclicklistener () {@Override public void onclic K (View arg0) {//Start specified service Startser Vice (intent); } }); Stop.setonclicklistener (New Onclicklistener () {@Override public void OnClick(View arg0) {//Close specified service StopService (intent); } }); }}
with the above 3 steps, we have completed a service and an application that uses the service (the service is part of the application). Sublimation Note 3:1. To set the Action property for intent, the primary role is to specify which service to start. Wherein, "Com.example.service.FIRST_SERVICE" is in the service's androidmanifest.xml configuration file in <service: <intent-filter of the/> element: The Action property of the service is configured in the/> child element. 2. The activity initiates the specified service through the Content.startservice (Intent Intent) method, and the OnCreate method is recalled whenever the service is created. The OnStart method is called back every time the service is started--launching an existing service component multiple times will no longer callback the OnCreate method, but will callback the Onstartcommand () method each time it is started.
Reference: Http://wear.techbrood.com/reference/android/app/Service.html
Android Learning Note 23. Getting Started with service components (a). What is a service?