Service of Android four components

Source: Internet
Author: User

A service is an application component that runs in the background and does not interact with the user. As with activity, each service must be declared in the manifest by <Service>. Can be started by StartService and Bindserverice.

Service, like other application components, runs in the main thread. This means that if the service requires a lot of time-consuming or blocking operations, it needs to be implemented in its child threads.

The two Modes of service (StartService ()/bindservice () are not completely detached):Local ServiceThe Local Service is used inside the application. Call Context.startservice () to start, call Context.stopservice () to end. It can call Service.stopself () or Service.stopselfresult () to stop itself. No matter how many times the StartService () method is called, you only need to call once StopService () to stop the service.Remote ServiceRemote Service is used between applications within the Android system. It can be manipulated by its own defined and exposed interfaces. The client establishes a connection to the service object and invokes the service through that connection. The connection is established to call the Context.bindservice () method to call Context.unbindservice () to close. Multiple clients can bind to the same service. If the service is not loaded at this time, bindservice () loads it first. Life cycle

context.startservice () ->oncreate ()->onstart ()->service running--call Context.stopservice () OnDestroy ()

Context . Bindservice ()->oncreate ()->onbind ()->service running--call >onunbind (), OnDestroy ()

In addition, the acitity sends a message to the service available IBinder, which can be implemented with Broadcastreceiver if the service sends a message to the activity.

Resources:

Http://www.cnblogs.com/zhangdongzi/archive/2012/01/08/2316711.html

http://android.blog.51cto.com/268543/527314/

http://zhangyan1158.blog.51cto.com/2487362/491358

Service of Android four components

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.