Service not fully resolved

Source: Internet
Author: User

The content of this article is not about service usage and life cycle, but on some of the key points to be recorded and analyzed.

As we all know, service is an application component that executes in the background for long-term operations in the background, such as network transactions, playing background music, and so on. It can be implemented in two ways, if other application components want to communicate with the service in-process (IPC), by binding with that service. On the life cycle of the problem is not verbose, directly put the official file of the figure:

any application component can use the service, regardless of how the service is started. If you want the service to be available only to your own app, you need to include the attribute in the manifest file android:exported and set it to false. Android officially recommends that we try to use an explicit way to start the service to keep the app safe.

In addition, it is important to note that, by default, the service does not reopen a separate thread to run, but instead runs in the main thread of the host process. Obviously, if we need to do time-consuming operations, such as the above mentioned playing background music, network operations, etc., how to solve?

      • We can create a new thread in activity and create a thread in the activity to start and destroy;
      • Use Asynctask to accomplish tasks;
      • A new thread is opened in the service to complete these tasks in case of a ANR error.

Intentservice

When we do not need to process multiple requests at the same time, our best solution is to use Intentservice to complete the task, and we simply rewrite onhandleintent () to complete the processing logic. The following work was done in Intentservice:

      • A new worker thread was started to perform the intent tasks that were submitted to the Onstartcomand method.
      • A work queue is created to hold multiple request tasks, one at a time only to the Onhandleintent () method to pass a intent.
      • When all the requested tasks are completed, the service is automatically stopped.
      • In the default Onstartcommand () method, the received intent parameter is passed to Onhandleintent () for processing.

The following is an example of a intentservice:

 Public classHellointentserviceextendsIntentservice {/*** A constructor is required, and must call the Super Intentservice (String) * constructor with A name for the work   ER thread. */   PublicHellointentservice () {Super("Hellointentservice"); }  /*** The Intentservice calls this method from the default worker thread with a * the intent that started the service.   When this method returns, Intentservice * stops the service, as appropriate. */@Overrideprotected voidonhandleintent (Intent Intent) {//normally we would do some work here, like download a file. //for our sample, we just sleep for 5 seconds.      LongEndTime = System.currenttimemillis () + 5*1000;  while(System.currenttimemillis () <endTime) {          synchronized( This) {              Try{Wait (endTime-System.currenttimemillis ()); } Catch(Exception e) {}}} }}

Here you simply implement a simple constructor and intent processing method. Of course, we can also rewrite other methods in the service life cycle as needed, but remember to call the parent class method. We need to be clear that all the tasks submitted to the service are stored in a queue in the form of intent, and the only worker thread in Intentservice takes one intent at a time and automatically shuts down intentservice after processing.

If you want multiple threads to handle tasks concurrently, you need to inherit the service to handle the task. No specific code is shown here, but there is one thing you need to remind yourself. The Onstartcommand method returns an int value, usually we return the method of the parent class, where the int value describes the operation after the system kills the service, several of its constant options are as follows:

      • Start_not_sticky: When the Onstartcommand method returns, the system kills the service and will not restart the service unless a intent request is passed in. This option is a safe way to ensure that the service starts running when the application restarts those unfinished work and does not need to run the service.
      • Start_sticky: When the Onstartcommand method returns, the system kills the service, then the service is restarted and the Onstartcommand method is executed. However, the re-delivery of the intent is not the last intent, but instead uses null (unless there is a new intent request). This option applies to a service like a media player, does not execute any commands, just keeps running waiting tasks.
      • Start_redeliver_intent: When the Onstartcommand method returns, the system kills the service, then the service is restarted and the Onstartcommand method is executed. The intent passed in is the same as the previous, and subsequent intent requests will be executed sequentially. This option is available for service similar to download tasks and is always active and needs to be restored immediately once it is hung up.

In addition, the service is often used in conjunction with notification, so that the service can be run to the foreground, such as music playback tasks, and so on, can interact with the user, the content will be introduced later.

Service not fully resolved

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.