Talking about Android Service

Source: Internet
Author: User



Basic usage of Android service:

The most basic usage of service is naturally the start and stop operations.

There are two ways to start a service:

1, start by StartService (Intent Intent) mode, automatically execute OnCreate (), Onstartcommand () method.

2. Through Bindservice (Intent intent,serviceconnection connection,int flag)

The first parameter is a intent object, and the second parameter is an instance of the connection service.

The third parameter is a flag bit where the bind_auto_create is passed in,

Indicates that the service is created automatically after the activity and service are established, which causes the OnCreate () method in the MyService to be executed,

However, the Onstartcommand () method does not execute.

There are two ways to destroy a service

1, by StopService (Intent Intent) method to destroy. The OnDestroy () method is called at this time

2, through Unbindservice (Serviceconnection conn);

Note: If a separate operation StartService start and stopservice destroy service or Bindservice start and unbindservice unbind is no problem,

But if StartService and then Bindservice, then StopService or unbindservice alone can not destroy the service.

The service will be destroyed only if all two methods are called. This is because the service is already connected and will be created automatically when the Bindservice is opened.

。 That is, clicking the Stop Service button will only stop the service, and clicking the Unbind Service button will only disassociate the service from the activity.

A service must be destroyed when it is not associated with any activity and the stop state is processed.


In the service, the main thread communicates with the child thread using handler.

Talking about Android Service

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.