Android Service)

Source: Internet
Author: User

With the service class, how can we start it? There are two methods:

    • Context. startservice ()
    • Context. bindservice ()

Call the startservice () method anywhere in the same application to start the service. Then the system calls back the oncreate () and onstart () Methods of the service class. In this way, the started service will always run in the background until the context. stopservice () or selfstop () method is called. If a service has been startedCodeIf you try to call the startservice () method again, oncreate () is not executed, but onstart () is re-executed ().

Another bindservice () method means to bind the service to the customer class that calls the service. If the customer class called is destroyed, the service is also destroyed. One advantage of using this method is that after the bindservice () method is executed, the service calls back the onbind () method mentioned above. You can return a class that implements the ibind interface from here, the class can communicate with the service by operating on the client, for example, obtaining the service running status or other operations. If the service is not running, the oncreate () method will be used to start the service without calling onstart ().

Next, let's discuss some issues that may occur when using the service and how to solve them.

Communicates with the service and keeps it running

What if we want to maintain communication with the service and do not want the service to exit with the activity? You can first startservice () and then bindservice (). When you do not need to bind, run the unbindservice () method. executing this method will only trigger the onunbind () of the service and will not destroy the service. In this way, the communication with the service can be maintained and will not be destroyed with the activity destruction.

Improve Service priority

The Android system has its own method for memory management. In order to ensure the orderly and stable operation of the system, the system automatically allocates and controls the memory.ProgramMemory usage. When the system finds that the current resources are very limited, it will kill programs or services that he thinks are not important to ensure that some programs with higher priority can run. This ensures that the programs that are truly useful to users are still running. If your service encounters this situation, it will probably be killed first. However, if you increase the service priority, you can leave it for a while. We can use setforeground (true) to set the service priority.

Why is it foreground? By default, the started service is marked as background, and the currently running activity is usually marked as foreground, that is to say, if you set foreground for the service, the priority of the running activity will be improved. This does not guarantee that your service will never be killed, but increases the priority of your service.

There is a way to give you a clearer demonstration, go to $ SDK/tools to run the command

When a large number of items are returned, observe the oom_adj value. If the value is greater than 8, it is generally the backgroud which may be killed at any time. The smaller the value, the higher the priority, and the later the killing time. The phone program is-12, indicating that the phone number is the phone number. If everything else is done, you can answer the phone, right. In addition, there is another-100, which is even worse because if the system is finished, your system will be suspended.

Start the service in other ways

In fact, it is not only necessary to start the service from the activity, but also a very useful method is to receive the broadcast of the system, which requires the use of aggreger. In the mainfest file, configure the types of broadcast messages that the receiver can receive. Even if your program is not displayed to the user, your service can be started. What you need to do is inherit Android. content. broadcastreceiver and implement the onreceive (context, intent) method to start your service. Here, bindservice cannot be used. Because a consumer is a temporary object, BIND has no meaning.

Resource Consumption

Everyone said that the G1 battery is too weak to use. In fact, I think most of this problem is software problems. 1150 mA of the battery is not big, but not small, considering that the 500 MHz CPU still consumes a lot of power. Because a service needs to run in the background for a long time, if your service is too resource-consuming, the battery will not take long.

I have a little bit of consideration for this issue and share it with you. Generally, the service starts other threads to repeatedly perform some operations, and the cycle frequency is not too high. Do not perform operations that are too resource-consuming, especially CPU resources, because the backend service users do not see it, it will be strange. You can monitor the use of top and logcat. If the GC of the Virtual Machine frequently occurs in the log, it also indicates that there is still much room for improvement in the program. Because GC is also very CPU-consuming. It is possible that these services should not only be paid attention to, but should be considered as long as mobile devices, in order to give your users the best experience.

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.