"Android Beginner" 12, Service

Source: Internet
Author: User

1. Service Usage
    1. Define a class (for example, EchoService), Inherit Service (Android.app.Service)
    2. Register the service in Androidmanifest.xml, such as <service android:name= "EchoService" ></service>
    3. Declares a intent:intent serviceintent = new Intent (mainactivity,echoservice.class);
    4. Start Service:startservice (serviceintent);
    5. Close Service:stopservice (serviceintent);
2. Service life cycle
    1. Create Service:oncreate ()
    2. Onstartcommand ()
    3. OnStart ()
    4. Destroy Service:ondestroy ()
    5. After the service is created, only the first oncreate is called before it is destroyed. During the run of the service, the OnCreate method is not executed, but Onstartcommand (),OnStart () are executed at the time the StartService is run.
3. Binding Service
    1. Binding: Bindservice (serviceintent,this,context.bind_auto_create);
      1. Bind_auto_create: Indicates that the service is automatically created if the service you want to bind is not already open.
    2. Unbind: Unbindservice (this);
The parameter in the above method of this, refers to a class implementation of the Serviceconnection interface, here is the mainacitivty implementation. And the method of the replication interface public void onserviceconnected (componentname name, IBinder service) and public void onservicedisconnected (Com    Ponentname name) Method 3.    Chen Gong is bound to execute the EchoService Onbind method, which returns an instance of the binder subclass. 4. Through the onserviceconnected got it.Binder Instance object, because binder is the internal class of the service and can be called into various methods within the services, so the activity can also do a variety of operations. remark:If the service is initiated through StartService, he will not be destroyed by the destruction of the activity, but services initiated through Bindservice will be destroyed. If the service is started through StartService, it is only destroyed by StopService. The StartService method is automatically executed by starting the service through Unbindservice (this will crash the program) through Bindservice. and cannot be stopped by StopService, you can use Unbindservice to stop

"Android Beginner" 12, Service

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.