Android study notes 23. Service component entry (1). What is Service ?, Android. service

Source: Internet
Author: User

Android study notes 23. Service component entry (1). What is Service ?, Android. service
What is Service?I. Service1. Service IntroductionThe Service is one of the four Android components. Similar to the Activity component, the Service represents executable programs and has its own life cycle, the only difference is that the Activity component provides an interface to facilitate human-computer interaction, while the Service only runs in the background without an interactive interface. It should be noted that the Service is not a separate process or a separate thread to Prevent the application from being unresponsive errors. It runs in the main thread of its hosted process like other application objects. Of course, if we want our Service to run MP3 or network downloads in the background, we can create a thread for implementation.2. Service LifecycleBecause the Service can be started in two ways: Context. startService () and Context. bindService (). (1) Context. startService () method: This method is used to start the Service. There is no association between the visitor (client) and the Service. Even if the visitor exits, the Service still runs. Call Context. startService ()-> onStartCommand (Intent, int, int)-> Service Running-> Context. stopService () or stopSelf ()-> Service is disabled (2) Context. bindService () method: This method is used to start the Service. The visitor (client) is bound with the Service. Once the visitor exits, the Service is terminated. Call Context. method of Service lifecycle triggered by startService (): onCreate ()-> OnBind (Intent)-> bind the client to Service-> OnUnbind ()-> onDestory ()-> Service is Disabled

Sublimation note. when the Context. when the bindService () method starts a Service, the onStartCommand (Intent, int, int) method is not executed; 2. when Activity (client Activiy) calls BindService () to bind a started Service (the Activity is initially started using the startService () method, the system only transmits the IBinder object inside the Service (returned by the onBind () method) to the acitinder, and does not completely "bind" the Service lifecycle to the Activity, therefore, when the Activity calls the unBindService () method to cancel binding to the Service, it only disconnects the Activity from the Service and cannot stop the Service component. 3. API-ServicePublic abstract class Service (1) Inheritance relation java. lang. object upload android. content. context encryption android. content. contextWrapper extends android. app. service (2) constructor Service (). However, when developing a Service, we mainly use Content. getService () method to obtain Service class objects. (3) Common method (Service component method) void OnCreate (): the Service will be called immediately after it 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 a specified Service, it will be called. The parameter passed in by the Client: Intent: "Intent" passed in by startService (Intent ". public abstract boolean stopService (Intent service): the client calls this method to disable Servicepublic final void stopSelf (): Service automatically disables public abstract IBinder onBind (Intent intent ): this method is required by the Service subclass and returns an I Binder object. Client Applications can communicate with Service components through this object. The Intent object is used to bind the client to the Service and pass it to Context. bindService. Boolean onUnbind (Intent intent): This method void onDestory () is called when all clients bound to this Service are disconnected (): this method will be called before the Service is closed, and the Service will clean up all resources it occupies (including all threads and recipients registered on the Service ). Ii. Basic Idea of Service Development 1. Create and configure a Service(1) define a subclass that inherits from the Service. If you want the Service component to do something, you only need to define the relevant business code in the onCreate () or onStartCommand () method. The framework of a Service component is as follows: \ src \ service \ FirstService. java
Public class FirstService extends Service {/*. the required method returns an IBinder object to the client for communication */@ Override public IBinder onBind (Intent arg0) {return null;}/* B. this method is called back when the Service is created */@ Override public void onCreate () {super. onCreate (); System. out. println ("Service is Created");}/* c. this method is called back when the Service is started */@ Override public int onStartCommand (Intent intent, int flags, int startId) {Sytem. out. println ("Service is Started"); return START_STICKY;}/* d. callback */@ Override public void onDestroy () {super. onDestroy (); System. out. println ("Service is Destroyed ");}}
2. Configure the Service in the AndroidManifest. xml fileAfter the preceding Service is defined, configure the Service in the AndroidManifest. xml file and configure the Service to use the <service ../> element. Similar to Activity configuration, you can configure the <Service .. /> element configuration <intent-filter .. /> child element, used to indicate which Intent can start the Service.
<! -- Configure a Service component --> <service android: name = ". FirstService"> <intent-filter> <! -- Configure action for the intent-filter of the service component --> <action android: name = "com. example. service. FIRST_SERVICE"/> </intent-filter> </service>
Sublimation NOTE 2: Service is the most similar component to Activity among the four Android components. They all represent executable programs. Therefore, the development Service is similar to the development Activity. We only need to define a subclass that inherits the Service and configure the Service in the AndroidManifest. xml file to run the Serviec in the program. How to start a Service? We can use Context in the Activity subclass of this application. startService () method or Context. bindService () method to start a service, you can also use Context in the Activity subclass of another application. startService () method or Context. bindService () method to start this service. 3. Start and Stop the ServiceAfter the Service is developed, you can start the Service in the Activity subclass of your application, or in other applications. Here, we start (close) The Service in this application and use 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); // get the start and stop buttons in the program interface. 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 attribute Intent for intent. setAction ("com. example. service. FIRST_SERVICE "); start. setOnClickListener (new OnClickListener () {@ Override public void onClick (View arg0) {// start the specified Service startService (intent) ;}}); stop. setOnClickListener (new OnClickListener () {@ Override public void onClick (View arg0) {// disable the specified Service stopService (intent );}});}}
Through the above three steps, we have completed a Service and the application that uses the Service (the Service is an integral part of the application ). Sublimation note. Set the Action attribute for Intent to specify which Service to start. Where, "com. example. service. FIRST_SERVICE "in the Service's AndroidManifest. <service .. /> <intent-filter .. /> Configure the action attribute of the Service in the sub-element. 2. this Activity uses Content. startService (Intent intent) method to start a specified Service. When a Service is created, the onCreate method is called back, when a Service is started, the onStart method is called back. When an existing Service component is started multiple times, the onCreate method is no longer called back, but the onStartCommand () method is called back every time the Service is started.
Reference: http://wear.techbrood.com/reference/android/app/Service.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.