Android 01 Service Introduction

Source: Internet
Author: User

 

 

 

Summary

If you have never touched the service before, you can have a general understanding of the following content. After using the service, read this chapter to learn more deeply.

Service is one of the four Android components, and the rest are activity, BroadcastReceiver, and Content Provider. A service is an application component that can perform long-term operations in the background. It does not provide a user interface. Applications can start background operations through services, and different applications can also implement inter-process communication (IPC) through services ).

After a service is started, it actually runs in the main thread, that is, the UI thread. If you want to perform time-consuming operations, it is recommended to create a new thread for processing; otherwise, the UI thread can be blocked, affecting the user experience.

1. service types and differences

There are four types of android Services described here, which are described below.

1.1 Started Service

That is, the started Service. It is one of the two common services, and the other is the Bound Service. It is often used to execute a background operation of a process, such as file downloading through this service.
To provide the started Service: first, create a class that inherits from the Service. Other applications can then start the service through startService (). To stop the service, you can execute stopService () in the application or stopSelf () in the service () to stop the service.

Once a service is started, it can run on the background indefinitely even if its components are destroyed. Generally, a started Service performs a single operation and does not return results to the caller. To return results, you can use BroadcastReceiver. These are described in detail in "Android Service -- Started Service.
 

1.2Bound Service

That is, the bound Service, like Started Service, is also one of the two common services. It is also often used to execute a background operation of a process, such as using this service to download files and other functions.
To provide the Bound Service: first, create a class that inherits from the Service. Then, other applications can bind the service through bindService (). After the service is bound, the functions provided by the service can be called. To remove the service, you can run unbindService () in the application ().

 

The main differences between "Started Service" and "Bound Service" are as follows:

(01)Different start/stop Methods:

  • To start "Started Service", call startService (); to stop the Service, call stopService () or stopSelf ().
  • To bind "Bound Service", call bindService (); To unbind, call unbindService ().

    (02)Different implementation Interfaces

    • In "Started Service", the onStartCommand () interface of the service is generally implemented. Because startService () calls the onStartCommand () interface, we generally execute some functions provided by the Service in onStartCommand (). If a thread is started for download.
    • In "Bound Service", the onBind () interface of the service is generally implemented. The onBind () function returns an IBinder object, which is called when the client calls bindService () to bind the service. This binding is asynchronous. The bindService () method returns immediately and does not return an IBinder object to the client. To receive an IBinder object, the client must create a ServiceConnection class and create an instance of ServiceConnection. Then, the client calls the ServiceConnection instance in the bindService () method. Here we only need to have a general concept. Later, in "Android Service Bound Service", we will detail the functions of each interface and how to implement these interfaces.

      (03)Different active lifecycles

      Here, "active Life Cycle" is different from "entire Life Cycle ). Their full lifecycle begins with onCreat () and ends with onDestroy (). However, "active life cycle" means that the service is still active.

      • The active life cycle of "Started Service" begins with startService (), stops at stopService () or stopSelf (). A "Started Service" can be Started multiple times, and the onStartCommand () interface of the service is called each time it is Started. However, once stopService () or stopSelf () is called, its active life cycle ends!
      • The active life cycle of "Bound Service" starts with bindService () and stops calling unbindService () on the last client (). It means that the "Bound Service" can be Bound by multiple clients, that is, multiple clients can call bindService () to bind the same Service. When the first client Bound to the Service calls bindService (), the active life cycle of "Bound Service" begins. When all clients are unbound, its active life cycle is over.

         

        Supplement:A service can provide both "Started Service" and "Bound Service ". To provide the starting service, you must implement the onStartCommand () interface. To provide the bound service, you must implement onBind (). Two interfaces are implemented at the same time, that is, two services can be provided.

         

        1.3 IntentService

        It processes service requests one by one, but cannot process multiple requests at the same time.
        To provide IntentService: first, create a class that inherits from the Service. Other applications can then start the service through startService (). To stop the service, you can execute stopService () in the application or stopSelf () in the service () to stop the service. In addition, the IntentService automatically stops after the request is processed.

        Since the start and stop modes of "IntentService" and "Started Service" are the same, the main differences between them are described here:
        (01)Different implementation principles

        • IntentService has a working queue to process tasks one by one. Because you don't have to worry about multithreading.
        • "Started Service" supports single-thread or multi-thread, but must be implemented in the Service by ourselves.

          (02)Different APIs are implemented.

          • IntentService must implement the onHandleIntent () interface and start Intent of IntentService in onHandlerIntent () for processing.
          • "Started Service" must implement the onStartCommand () interface. The functions required by the Service are generally processed in this interface.

            (03) different lifecycles.

            • All startup requests in IntentService are automatically terminated after they are processed. Therefore, you do not need to call the stopSelf () method on your own.
            • The life cycle of "Started Service" starts from startService (), stops from stopService () or stopSelf ().

               

              1.4 AIDL

              AIDL (Android Interface Definition Language) is used to complete inter-process communication (IPC ).
              Generally, AIDL is used only when the service needs to accept multi-threaded requests from different applications. If the request in the same application is implemented using a Binder; if you only use inter-Application Communication instead of multi-thread processing, you can also use AIDL in both cases. The use of AIDL varies between local and remote processes. both local and remote processes are executed in the called thread during internal calls, A remote process is called through a thread pool maintained by the system in the Service process. Therefore, it may be called by unknown threads at the same time. Pay attention to thread security issues.

               

               

              2. Service APIs

              Both "Started Service" and "Bound Service" are inheritance and Service classes. The following describes the main APIs in the Service class.

              OnStartCommand (): When other programs call startService (), the system automatically calls this function. Once the function is executed, the service is started. In this case, the Service is Started as "Started Service ".

              OnBind (): When other programs call bindService (), the system automatically calls this function. Once this function is executed, the service is started. In this case, the started Service is "Bounder Service ". To provide the binding function, you must implement this function and return the IBinder object. If you do not want to provide the binding function, you can directly return null.

              OnUnbind (): When other programs call unbindService (), the system automatically calls this function.

              OnCreate (): The system automatically calls this function when a service is created. Initialization is generally performed in this function, for example, creating a thread.

              OnDestroy (): The system automatically calls this function when the service is destroyed. In this function, purge the job, for example, terminate and recycle the thread.

               

               

               

              3. service declaration method

              In manifest, declare the service using the following method:

                           
                             ...      
                            
                                  ... 
                            
                           

              Like activity. You can register intent-filter in the manifest corresponding to the service, so that other programs can call the service by implicit intent. You can also not register intent-filter, the service is called directly through explicit intent. To make a service a private service of an application, you can set the android: exported attribute to false.

              4. Service Lifecycle

              The service lifecycle sequence is shown as follows:



              1. service types and differences

              There are four types of android Services described here, which are described below.

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.