Life cycle of Android service 2

Source: Internet
Author: User
Tags call back

With the Service class, how do we start him? There are two ways to do this:

Context.startservice ()
Context.bindservice ()


     1.  call the StartService () method anywhere in the same application to start the Service. The system then callbacks the OnCreate () and OnStart () methods of the Service class. This starts the Service running in the background until the Context.stopservice () or Selfstop () method is called. In addition, if a Service has been started, the other code attempts to call the StartService () method, is not executed onCreate (), but will be re-executed once OnStart ().

2. Another bindservice () method means that the service is tied to the client class that invokes the service, and if the call to this client class is destroyed, the service is destroyed. One advantage of this method is that after the Bindservice () method executes the service will callback the above mentioned Onbind (), you can return from here a class that implements the Ibind interface, the class will be able to communicate with the client, such as getting service The State or other action of the run. If the service is not running, using this method to start the service will onCreate the () method without calling OnStart ().

Summarize:

1. The purpose of StartService () is to callback the OnStart () method, which is called when the service does not exist, if the service exists (for example, a bindservice was previously called), and the OnCreate () method Then the OnCreate method of the service has been called) then StartService () will skip the OnCreate () method.

2. Bindservice () is intended to call back the Onbind () method, which is used to establish a bridge between the service and the caller, and is not responsible for more work (for example, a service needs to connect to the server), Bindservice is typically used to bind to an existing service (that is, services initiated through StartService).

because the OnStart () method of the service is called only when the service is started by StartService (), it is important to note this when using OnStart ().

communicate with the Service and let it run continuously

What if we want to keep communication with the service and don't want the service to exit with the Activity? You can first StartService () and then Bindservice (). The Unbindservice () method executes when you do not need to bind, and executing this method only triggers the service's onunbind () and does not destroy the service. This allows the communication to be maintained with the Service and will not be destroyed as the Activity is destroyed.

Increase Service Priority

The Android system has its own method for memory management, in order to guarantee the orderly and stable operation of the system, the internal system will automatically allocate and control the memory usage of the program. When the system feels that the current resources are very limited, in order to ensure that some high-priority programs can run, it will kill some of the programs or services that he considers unimportant to free memory. This will ensure that programs that are really useful to the user are still running again. If your Service is in this situation, you will probably be killed first. But if you increase the priority of the service to let him stay a little more, we can use Setforeground (true) to set the service priority.

Why the foreground? The service that is started by default is marked as background, and the activity that is currently running is generally marked as foreground, which means that you set the service to foreground so that he has the same priority as the running activity. A certain increase. When this doesn't guarantee that your Service will never be killed, it just increases his priority.

From the web other materials: About service life Week

The Android service life cycle is similar to the activity life cycle, but there are also important differences in some of the details:

OnCreate and OnStart are different.

We can start a service by calling the Context.startservice (Intent) method from the client. If the service is not yet running, Android will start it and call its OnStart method after the OnCreate method. If the service is already running, then its OnStart method will be called again by the new intent. So it is completely possible and normal for a single running service to have its OnStart method repeatedly invoked.

Onresume, OnPause, and onstop are not needed.

Callback a service usually does not have a user interface, so we do not need onpause, Onresume or OnStop methods. Whenever a running service it is always running in the background.

Onbind

If a client needs a persistent connection to a service, then he can call the Context.bindservice method. If the service does not run the method will create the service by calling the OnCreate method but does not call the OnStart method to start it. Instead, the Onbind method is called by the client's intent, and it returns a Ibind object so that the client can invoke the service later. It is normal for the same service to be started and bound by the client at the same time.

OnDestroy

As with activity, a OnDestroy method will be called when a service is terminated. Android will end this service when no client is started or bound to a service. As with many activity situations, Android may also end a service when memory is low. If this happens, Android may also try to start the terminated service when memory is sufficient, so your service must persist the information for restart, and it is best to do so within the OnStart method.


Life cycle of Android service 2

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.