Android Service and androidService

Source: Internet
Author: User

Android Service and androidService
Service Usage
Function operations that take a long time are stored in the background. (Download, MP3 playback) Service is an application component. The Service does not have a graphical interface. You can use the Service to update ContentProvider, send Intent notifications, and start the system. The Service is not a separate process, and the Service is not a thread.Service LifecycleOnCreat (): called by the system when the Service is created for the first time. OnStartConmmand (): It is used from Versions later than Android, and will be automatically called by the system when the Service is started. OnDestroy (): called by the system when the Service is no longer used.OnStartCommandService Process PriorityThe Service has a high level of process, which is generally not killed in the following situations. 1. if the Service is calling the onCreate, onStart, or onDestroy method, the process used for the current Service changes to the foreground process to avoid being killed. 2. if the Service has been started and its processes are second only to visible processes, but more important than invisible processes, this means that the Service is generally not killed. 3. If the client has been connected to the Service, the process that owns the Service has the highest priority. You can think that the Service is visible and will not be killed. 4. If the Service can use the startForeground (int, Notification) method to set the Service to the foreground state, the system considers it visible to the user, it will not be killed when the memory is insufficient.

I. onStartCommand has four return values:

START_STICKY: if the service process is killed, the service State is retained to the starting state, but the intent object to be delivered is not retained. Then the system will try to re-create the service. Because the service status is starting, the onStartCommand (Intent, int, int) method will be called after the service is created. If no startup command is passed to service during this period, the Intent parameter is null.

START_NOT_STICKY: "non-viscous ". When this return value is used, if the service is kill abnormally after onStartCommand is executed, the system will not automatically restart the service.

START_REDELIVER_INTENT: Re-transmits Intent. When this return value is used, if the service is kill abnormally after onStartCommand is executed, the system automatically restarts the service and passes in the Intent value.

START_STICKY_COMPATIBILITY: compatible version of START_STICKY, but it is not guaranteed that the service will be restarted after it is killed.

Binding a Service to an Activity allows the Activity to provide relevant information.

Service class Creation

1. inherit Service

2. Rewrite the onBind method and return a bound interface to the Service.

3. Declare an internal class to inherit the Binder

4. In onBind, the class that inherits the Binder is instantiated. Let's talk about the object and return it.

Register: service in Manifest

Activity

Bind button

In The OnClick method:

Instantiate an Intent object

Set page jump through Intent Object Reference: intent. setClass (Context. this, class );

Binding Service to Activity: bindService (intent, conn, flags)

Use an anonymous internal class to declare a ServiceConnection object. OnServiceConnected and onServiceDisconnected

Binding Activity to service is mainly completed through the Binder object. Serivic creates a Binder and obtains the binder method in the Activity.

Parcel object

Serialization:Convert the object into binary data and save it.

Transact and onTransact

To call onTransact (int code, Parcel data, Parcel reply, int flags), you must first call Transact (int code, Parcel data, Parcel reply, int flags ).

Code:Is an integer between FIRST_CALL_TRANSACTION and LAST_CALL_TRANSACTION.

Data: Data sent to the service.

Reply:The data returned to the Activity.

Flags: usually written as 0.

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.