Android study notes 9

Source: Internet
Author: User

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.

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.