Service of four Android Components

Source: Internet
Author: User

Service is one of the four Android components and can be run in the background without displaying the interface. Another role is to implement inter-process communication through AIDL.

 

Service Startup Method

Service can be started in either of the following ways: startService () and bindService ()

StartService (): onCreate-> onStartCommand (the old version is onStart). The onDestroy is called back through stopService ().

If the service has been created before the startService () method is called, multiple call of the startService () method will not lead to multiple creation of the service, but will lead to multiple calls of the onStartCommand () method

 

BindService (): onCreate-> onBind, end through unBindService (), callback onUnbind-> onDestroy.

 

The first method is to start the Service. The Service is not associated with the caller. After the caller, such as Activity, finishes, the Service is still running. Bind means that the two are bound together, the caller ends, and the Service ends. Of course, the caller can also unbind directly through unbindService.

 

 

Service and Thread

Many people do not understand the difference between Service and Thread. Here I will talk about my understanding. Service can be understood as an Activity without interface. It runs in the main thread (UI thread), rather than another thread, so why cannot it be in onStart (), onCreate () because it will block the main thread and even cause ANR exceptions. Generally, a separate thread is required in the Service for time-consuming operations. At the same time, the Service is divided into local Service and remote Service, which are local Service and remote Service respectively.

 

Local Service: in the main thread of the current process, it is in the same process as the Client, and it is in the running UI thread.

Remote service: Start another process. Note that it is a process, not a thread. Run on the main thread of the process. It is often used for inter-process communication.

You can add the android: process = "remote" code to set the Service to remote Service (local by default)

 

As for the Thread, we don't need to introduce it, so we can see that the Service has nothing to do with the Thread half-cent.

So why does the local Service need to be introduced? Why not use Thread directly? My understanding is that the local Service can manage and maintain threads more conveniently. For example, if you start a subthread in the current Activity and when you leave the Activity, you will lose the reference of this Sub-thread. Regardless of how your Activity jumps, the Service can maintain sub-threads securely.

 

 

Service Communication

Service communicates through IBinder. First, create the internal class MyBinder to inherit the Binder class that implements the IBinder interface. Override the onBind () method to return the MyBinder instance.

  MyService    MyBinder binder =  MyBinder          System.out.println("onbind"         "Create" (!1000++ "unbind"          .quit = "destroy"

Create a ServiceConnection instance in the Activity that calls the Service, override onServiceConnected, and obtain the IBinder object.

 ServiceConnection conn =              System.out.println("discouted"             binder =

When bindService (Intent service, ServiceConnection conn, int flags) is called, the second parameter is the ServiceConnection instance. After obtaining the IBinder object, you can call the method in it.

Related Article

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.