Android Four-pack service

Source: Internet
Author: User

    • The following is a service overview written in Xmind

    • Service component Development

First step: Inherit service class public class SMSService extends service {}

Step Two: Configure the service in the <application> node in the Androidmanifest.xml file: <service android:name= ". SMSService "/>

Step three: Start the service

Method One: Context.startservice (): The caller is not connected to the service and the service is still running even if the caller exits

Method Two: Context.bindservice (): The caller and the service are bound together, once the caller exits, the service terminates, there is a "no need to live at the same time, must Die" feature

    • How to start/stop/BIND service
    1. Start the specified service

Pubic componentname StartService (Intent dervice)

The intent parameter, which specifies the target service to start in this intent. There are both explicit and implicit intentions of the start-up method

Description: StartService () is defined in the Contextwrapper class, and the parent class of the class is the context

The Contextwrapper class is the immediate parent class of the activity's indirect parent and service

Example: Intent Intent = new Intent (meta component, target service.class); StartService (Intent);

2. Stop Service

Boolean StopService (Intent Service)

and start the same

The StopService method is defined in the Contextwrapper class

3. Stop the service inside the service

Final Stopself ()

This method does not callback the Ondestory method in the service

4. Target service bindings specified with intent

Bindservice (Intent, serviceconnection, int flag)

Intent: Starts the service for the specified target in the intent object

Conn: An object that implements the Serviceconnection interface that listens to the binding between the visitor and the service, and succeeds in invoking the Onserviceconnectend method of the Scnn object

Flags: Whether the service is created automatically when binding. A value of 0 is not automatically created when the service is created automatically when the Bind_auto_create constant (value is 1)

The relationship between the service and the activity life cycle of its bindings

5. Serviceconnection interface

Function: Used to listen for a connection between a visitor and a service, which defines two methods

onserviceconnected (conponentname name, Ininder Service)

Role: Binding success is triggering this method

Name: The service instance being bound

Service: Returns an object of type IBinder through the Onbind method in the bound service class

onservicedisconnected (conponontname name)

This method is triggered when an exception occurs in the binding and the reference program crashes

6. Bubindservice (Serviceconnection conn)

Unbind Conn-related service

Conn is an object that implements the Scnn interface.

After the Bubindservice executes, the system will callback the service's Ondestory () method

    • Service life cycle

OnCreate ():

This method is called when the service is created, and the method is only called once, and the service is created only once, regardless of how many times the StartService () or Bindservice () method is called.

OnDestroy ():

This method is called when the service is terminated. Life cycle methods related to starting services with the Context.startservice () method

OnStart ():

This method is only invoked when the service is started with the Context.startservice () method. This method is called when the service starts running. The StartService () method is called multiple times although the service is not created more than once, but the OnStart () method is called multiple times.

Onbind ():

This method is only invoked when the service is started with the Context.bindservice () method. This method is called when the caller and the service are bound, and the Context.bindservice () method is called multiple times when the caller is tied to the service and does not cause the method to be called more than once.

Onunbind ():

This method is only invoked when the service is started with the Context.bindservice () method. This method is called when the caller and the service are unbound

    • Service binding principle

Overview: 11 ser and one activity can only be bound once

21 activity can bind more than one service at a time

31 Ser can be bound to more than one act

4 when binding to a service, and when the flag is set to Bind_auto_create, if the service is not created, the SERVICEONCREATE0 is created earlier, and then the binding, if created, is directly bound

5 Ser is bound by multiple Act bindings only if all clients are unbound will be destory

6 Binder class for multiple clients communicating with the server, binder means pin, binds the client to the server

Data passing analysis (code) when binding components

Comparison between Bindservice and StartService:

1 the Onstartconnand () in the service class is triggered each time by a multiple call to StartService, and the service is controlled by using the intent object to pass different data to the service, but inconvenient

2 through the way of binding, the use of Onbind passed to Serviceconnection IBinder Control service mode is relatively convenient, the actual development of the recommended use of this way

About the invocation of the Onservicedisconnected method:

1 when activity is bound to service, if a process crashes, the onservicedisconnected method triggers

2 when the process is terminated yes, onservicedisconnected will be triggered

3 onservicedisconnected method called by Android system, do not program trigger, do not write business logic code in this method

Onrebind (): rebinding

    • Implicit intent to start service

Overview: 1 Use implicit intent to launch services from another application, enabling cross-process startup services

2 registering a service with a manifest is done by adding intent-filter, allowing other applications on the device to start their service implicitly with an intent to

Tip: If you do not want to have other applications launch the service in this program, do not declare intent-filter in the service tag

Android Four-pack 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.