Android service-start the service

Source: Internet
Author: User

You can pass an intent object (specify the service to be started) to the startservice () method to start the service from an activity or other application component. The Android system calls the onstartcommand () method of the service and passes the intent object to it (you should not directly call the onstartcommand () method)

For example, an activity can use the startservice () method with a clear intent object to start a service:

Intent intent = new intent (this, helloservice. Class );
Startservice (intent );

The startservice () method returns immediately, and the Android system calls the onstartcommand () method of the service. If the service is not running, the system first calls the oncreate () method and then calls the onstartcommand () method.

If the service does not support binding, the intent object sent by the permanent startservice () method is the only communication mode between the application component and the service. However, if you want the service to send a feedback, the client that starts the service can create a pendingintent object for broadcasting (using the getbroadcast () method ), and send it to the service where the intent object of the startup service is located. Then the service can use this broadcast object to send results.

The results of starting multiple services are in the onstartcommand () method of calling the service, but as long as one request terminates the Service (using the stopself () method or stopservice () method), the service needs to be terminated.

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.