Service Use
Long-time function operations are placed in the background. (download, MP3 playback) The service is an application component service without a graphical interface that can use the service to update ContentProvider, send intent and boot system notifications, and so on. The service is not a separate process, and the service is not a thread.
life cycle of serviceOncreat (): When the service was firstCreateCalled by the system. Onstartconmmand (): is used from the later version of Android2.0,service startupis automatically called by the system. OnDestroy (): When serviceno longer usedCalled by the system.
Onstartcommand
process priority for serviceThe service has a high process, and generally will not be killed in the following cases. 1. If the service is calling the OnCreate, OnStart, or OnDestroy method, then the process used for the current service becomes the foreground process to avoid being killed. 2. If the service has been started, the process that owns it is second only to the visible process, and is more important than the invisible process, which means that the service is generally not killed. 3. If the client is already connected to the service, the process that owns it has the highest priority and can be considered visible and not killed. 4. If the service can use the Startforeground (int,notification) method to set the service as the foreground state, then the system is considered to be visible to the user and will not be killed when the memory is low.
Onstartcommand has 4 kinds of return values:
Start_sticky: If the service process is killed, the status of the reserved service is the start state, but the delivery intent object is not preserved. The system then tries to recreatethe service, and since the service status is started, the Onstartcommand (Intent,int,int) method must be called after the services are created. If no startup commands are passed to the service during this time, then the parameter intent will be null.
Start_not_sticky: "Non-sticky". When this return value is used, the service is not automatically restarted if the service is killed by an exception after Onstartcommand is executed.
Start_redeliver_intent: Retransmission INTENT. With this return value, if the service is killed by an exception after Onstartcommand is executed, the service is automatically restarted and the value of intent is passed in.
Start_sticky_compatibility:start_sticky compatible version, but does not guarantee that the service will be restarted after kill .
The process of binding service in activity to enable activity to provide relevant information
Service Class creation
1. Inherit Service
2, the replication Onbind method, returns a bound interface to the service.
3. Declare an inner class to inherit binder
4. Instantiate the class that inherits binder in Onbind. Again the object to return.
Register in manifest: Service
In activity
Bind button
In the OnClick method:
Instantiating a Intent Object
Set page jump by intent Object reference: Intent.setclass (Context.this,class);
Service and Activity bindings: Bindservice (Intent,conn,flags)
Declares an Serviceconnection object with an anonymous inner class. There's onserviceconnected and onservicedisconnected.
The binding of activity and service is done mainly through binder objects. Serivic creates a binder that gets the binder in the activity and gets the method in the binder.
Parcel Object
Serialization: data that is transformed into binary is stored.
Transact and Ontransact
If you want 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: Information sent to service.
reply: The data returned to the activity.
Flags: usually written in 0.
Service in Android