Learn about Android Service

Source: Internet
Author: User

1. Use Service2. Binding the life cycle of the Service3.service--------------------------------------------1. Using the service
Start Service:Content.startService (intent); Stop Service:Content.stopService (intent);2. Binding service
Bind service Content.bindservice (intent,this,bind_auto_create); Unbind service Content.unbindservice (this);
After binding, there will be two methods to implement: the service is bound to execute onserviceconnected (componentname name, IBinder service) {} The process of the services crashed Execute onservicedisconnected (componentname name) {} after either being destroyed

Here we need to return a binder () that implements the IBinder interface.
Example code:Create a serviceruning, whether run, default is false;
Runs when true in OnCreate, and stops when false.
}.start; when destroyed, set Serviceruning to False to stop the thread from running.
3.Serice life cycle


When you start the service and bind the service, you must unbind the service and stop the service before the service is stopped. Starts the service, exits the current activity, discovers that the service has not been destroyed, is still running, returns activity, clicks Destroy service, the service is destroyed; Binds the service, exits the current activity, discovers an exception, and cancels the binding.Note:

1)can be called by theContext.startservice ()Start aService, which may triggerServiceof theonCreate ()and theOnStart ()operation, specifically executionStartService ()will certainly triggerOnStart (), but if theServicealready exists in the system, theonCreate ()will not be called again, it is only inServicetriggered at the first boot.

throughContext.startservice ()Start theServicewill run until it passes through theContext.stopservice ()orstopself ()Stop it. Multiple passesStartService ()starting a service does not generate multiple instances, but it causes the service'sOnStart ()is called multiple times, of course, because there is only one instance, so no matter how many times it starts, it only needs to be called onceContext.stopservice ()orstopself ()you can do it.

2)you can also passContext.bindservice ()to get a link to a service that is always going to be kept throughContext.unbindservice ()cut it off. If the service does not yet appear on the system at the time of connection, a new service may be created,Serviceof theonCreatefunctions are also called. When a connection is established, itServiceof theOnbinderwill be triggered by theOnbinderYou can return the connection after the establishedIBinderinterface object, a client that uses the service (such as aActivity) can be achieved byIBinderobjects, andServiceinteraction.

     aServiceif it is throughBindservice ()started, then it will persist until no client is connected to it, because there may be many clients that remain connected to the service, and if a link is actively disconnected by the client it will only beServicethe number of links minus1, when reduced to0the time of thisServicewill be destroyed.

3)aServicecan either be started(start)can also be connected(BIND), thenServicethe life cycle depends on how it is created, if it is throughContext.startservice ()is created, as in the first case, if theContext.bindservice ()Using Parameterscontext.bind_auto_createcreated, the situation is the same as the second.

      of course, when the service is stopped and destroyed, its OnDestroy () function is triggered and we need to complete this service here Cleanup of related resources, such as stopping its child threads, unregistering listeners, and so on.

Learn about 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.