"Android" service

Source: Internet
Author: User

Creation of service

Service only needs to create a class and then inherit from service, life cycle only oncreat () and OnStop method ()

Binding

You can start the service with intent, or bind it by using the Bind method, Activicy inherits from Serviceconnection,bindservice (own intent,this,context.bind_auto_ CREATE), overriding the interface's two methods, Onserviceconnection and Onservicedisconnection. The service class overrides the Onbind method and lets it return the value of the binder type. You can create an inner class that inherits from Binder, and then instantiate an object that lets the IBinder method return this class.

Service runs in the background

If the service is started with intent, if not manually destroyed, the service will not stop even if the program stops running. If a service is bound, it should be untied before stop can turn off the service

If the service is run in a binding manner, the Activity destruction service ends

After binding, you can communicate with the activity

Gets the value in the service

The parameter of onserviceconnection in activity is the value returned in the service, that is, the class we created ourselves, in this class, create a method ret () to return to himself, Then define the Get method in the service class, in Activity Onserviceconnection, Ibinder.ret () assigns to a parameter of our own service type, then obtains the data through this parameter's Get method.

"Android" 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.