Service:
Master the management methods of In-process services;
Master the implicit startup and display startup of the Service;
Master the binding and calling methods of cross-process services;
Communication between processes;
Binding method: convenient data sharing;
Bindservice () establishes a service connection and unbindservice () stops a service connection );
If bindservice is not started, the service is automatically called;
The same service can be bound to multiple service connections;
Run the following command to install the APK file:
Start an AVD first. Run the ADB install [full path of the APK file]
To write a service, you must inherit the service class and start a service using startservice (intent ),
End Service, stopservice (intent)
Register a service in manifest:
<Servcie/>
To use serviceconnection, bindservice () is used for Service binding:
1. The value returned by the onbind method in service cannot be null;
2. If the onbind method in service returns NULL, the callback function in serviceconnetion
The onserviceconnected method does not respond;
3. Call the unbindservice method in the activity.
Service:
Create an internal class in the service and inherit the bundle class. Create a method to return the service object, which is obtained in the activity.
Service object, call other methods
For example:
Class LocalService extends binder {
Testservice getservice (){
Return testservice. This;
}
}
Lifecycle:
Startservice ==> oncreate () --> onstart (), the second startservice ==> onstart ()
Stopservice ==> ondestroy ()
Bindservice ==> oncreate () --> onbind (), the second bindservice, instead of calling the oncreate (), onbind () method
Onserviceconnected method in serviceconnection
Unbindservice ==> ondestroy ()
① The service object cannot be obtained using startservice, which can be obtained by bindservice;
② The bindservice method has weak control over the service lifecycle. startservice can obtain some resources in onstart.