Initial learning notes for Android service

Source: Internet
Author: User

Application Scenarios for 1.Service

Conditions: A. Does not depend on the user visual interface (not absolute, such as the front service is used in conjunction with the notification interface)

B. Has a long time running characteristics.

There are many application scenarios for service, such as:

    • When the user leaves the music app, the playback is run as a service;
    • It is implemented as a service when the background file is being downloaded;
    • Google Apps maintains a service to get push services from the network;
    • When some special data synchronization needs to be carried out, sync adapters runs in the background with service;
    • Live wallpaper runs in the background with service;

Basic usage of 2.service

A. Two ways to start

    • Start with StartService
    • Start with Bindservice

The difference between the two ways to start

    • Activated by StartService, it has nothing to do with activity, and can be closed in other places.
    • Started by Bindservice, is bound with activity, activity hangs, it hangs

B. Life cycle

  

The life time of the service active activity (active lifetime) is invoked starting with Onstartcommand () or onbind () , and their respective justification The Intent object passed over by the StartService () or Bindservice () method.

If the service is turned on, its activity life cycle and the entire life cycle end together.

If the service is bound, its activity life cycle is ended after the Onunbind () method returns.

Note: Although an open service is stopped by calling Stopself () or stopservice () , there is no corresponding callback function corresponding to it, i.e. no onstop () callback method. So, when the Stop method is called, unless the service and the client component are bound, the system will destroy it directly, and the Ondestory () method will be called and the only callback method that will be called at this time.

C. Several instances have been created?

No matter how many times a call is made, there are only 1 instances, because only one OnCreate method is gone.

Don't forget to give him a stop at the start (StartService) (StopService)

Also at the time of binding (Bindservice), you have to unbind him (Onunbind) in the back.

3.Service Communication with activity

Communication mode

    • Inheriting binder Classes

      This method can only be used when your acitivity and service are in the same application and process, for example, you have a service that plays background music in the background, so you can communicate in this way. Define an inner class in the service, a subclass of binder, pass the service object to the activity through this class, so that the activity can invoke the common method and the common property in the service, but this way, Be sure to be in the same process and in the same application.

    • Using Messenger

      The above method can only be used in the same process, if you want to communicate with the service of another process, you may use Messenger.

      In fact, the way to achieve the IPC, there are aidl, but the recommended use of Messenger, there are two advantages:

      1. The use of Messenger is much simpler to implement than the Aidl method.

      2. When using Messenger, all messages sent from the activity are queued in one queue, and the service is not requested at the same time, so it is thread-safe. If your program is to multi-threading to access the service, you can use Aidl, otherwise it is best to use the Messenger method.

    • Using Aidl

This method is slightly, if you know the above two methods, this method is rarely used.

The relationship between 4.Service and thread

The relationship between service and thread: a dime doesn't matter .

The difference between service and thread:

    • Under normal conditions the service runs on the main thread, thread is a child thread
    • The service has a higher priority than the suspended activity and is higher than the thread created by the activity

The life cycle of thread

    • Created in activity: The life cycle of thread is the life cycle of the entire activity
    • Created in application: The life cycle of thread is the life cycle of the entire application
    • Created in service: This is the only way to guarantee the thread's longest life cycle, and thread is always in the background as long as the service does not exit

  

  

  

Initial learning notes for Android service

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.