Analysis of Android Service

Source: Internet
Author: User

This blog uses the following structure to explain the service:

1. First check what the service is.

AServiceIs an application component that can perform long-running operations in the background and does not provide a user interface.

A service is an application component that can run for a long time in the background and does not provide a user interface. Service is one of the four main components of Android (in fact, I prefer to call them "Four Kings.

2. I started to think that the service runs components in other threads in the background, and later I found an error. The answer is in the following sentence:

A service runs in the main thread of its hosting process-the service does
Not
Create its own thread and doesNotRun in a separate process (unless you specify otherwise ).

The service runs in the main thread (also known as the UI thread) of the main process. The service does not create its own thread, it is also not run in a separate process (different from the process of the application where the service is located) (unless you specify ).

3. I especially like the articles or documents written by Yang Mao, especially rigorous. First, let's talk about the definition of service, that is, what service is, and then what service is (1.a service is
NotA separate process. service is not a separate process. 2. A service is
Not
A thread. service is not a thread .), In this way, the basic concepts of service can be well understood in both positive and negative aspects.

4. After talking about the basic concepts of service, we should say when to use service. When to use the service is not clearly defined, but it can be inferred based on the characteristics of the service. For example, it can be executed in the background, and the MP3 music can be played using the service, for example, you can perform an operation for a long time, and you can use the service to upload and download files.

5. Now it is time to explain the service life cycle. Sometimes the picture is clearer than the text. The service lifecycle is as follows:

The service lifecycle. The digoal on the left shows the lifecycle when the service is created
startService()And the distrion the right shows the lifecycle when the service is created
bindService().

The left side of the figure shows the life cycle of the Service created with startservice, and the right side of the figure shows the life cycle of the Service created by bindservice.

This figure also shows that there are two ways to create a service: 1.startService()Method. 2.bindService()Method.

6. The service code example is as follows:

6.1 if the service is used in the app, create a class that inherits the service class or service subclass.

6.2 declare this service in the manifest file, for example, <service android: Name = ". myservice"> </service>

6.3.1 start myservice to rewrite onstartcommand () in startservice mode, as follows:

@ Override
Public int onstartcommand (intent, int flags, int startid ){
Log. I ("LocalService", "received start ID" + startid + ":" + intent );
// We want this service to continue running until it is explicitly
// Stopped, so return sticky.
Return start_sticky;
}

Use startservice (new intent (this, myservice. Class); to start the service, and use stopservice (new intent (this, myservice. Class) to stop the service.

6.3.2 enable myservice to rewrite onstartcommand () in bindservice mode.

Use bindservice (); to start the service, and use unbindservice () to stop the service.

I am an android novice. Please forgive me for your correction if you have any errors.

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.