Common Mistakes of Android Service

Source: Internet
Author: User

After learning about the service, I will not talk about some of the common online services. I would like to say something that is easy to misunderstand.

1. are services created in new processes?
Actually not. I wrote in the dev guide: Reference caution: A service runs in the main thread of its hosting process-the service does not create its own thread and does not run in a separate process (unless you specify otherwise ). this means that, if your service is going to do any CPU intensive work or blocking
Operations (such as MP3 playback or networking), You shoshould create a new thread within the service to do that work. by using a separate thread, you will reduce the risk of application not responding (ANR) errors and the application's main thread can remain
Dedicated to user interaction with your activities.

That is to say, when a service is created, it is created by the main UI thread in the application process by default. If you perform blocking operations in the service, the UI thread will be blocked directly, this may cause no response in five seconds. Therefore, the official recommendation is to create a new thread in the service to prevent blocking and releasing.

2. How to Create a service in the new process?
The general method is to use the process label when declaring the service in manifast to indicate the process in use. The current process is used by default.ProgramMain process: JavaCode

    1. <Service
    2. Android: Name ="Org. foreal. musicservicetest"
    3. Android: enabled ="True"
    4. Android: Process =": Music_process"
    5. > </Service>

If the process name starts with ":", it indicates that the process is a private process of the current program and cannot be accessed by other programs. If the process starts with a lowercase letter, A process is a global process. Multiple programs can access this process to share the process.

3. if the service of the new process is not used, how can we conveniently perform some blocking operations?
You can inherit the intentservice to compile your own service. intentservice automatically creates a worker thread to perform operations such as Java code.

  1. Public ClassMyintentserviceExtendsIntentservice {
  2. Private Final StaticString tag ="Myintentservice";
  3. PublicMyintentservice (){
  4. Super("Myintentservice");
  5. }
  6. @ Override
  7. Protected VoidOnhandleintent (intent ){
  8. Log. I (tag,"Onhandleintent");
  9. Try{
  10. Synchronized(This){
  11. Wait (3000);
  12. }
  13. Log. I (tag,"Completed once");
  14. }Catch(Interruptedexception e ){
  15. E. printstacktrace ();
  16. }
  17. }
  18. }

You only need to override the onhandleintent method to process your own operations. Each operation is included in the queue and executed in sequence.
Note that the intentservice will automatically destroy the current service after all the operations in the queue are executed. That is to say, if your onhandleintent is a non-blocking operation, it is very likely that the service will be destroyed after each call, and a new service will be created next time.
This class can easily complete most of the work, but if you want to perform concurrent operations on the service, this class is not suitable, you should implement it yourself.

3. How to make the service private?
If you declare the intent filter in the service, other programs can also access your service through the intent filter you declare. To avoid this situation, you can first remove the intent filter, each call to the Service uses the intent (context, class) format. However, if you prefer the intent filter format, you can use the exported tag to make the service private, even if you define the intent filter, other programs cannot be accessed.

4. If a service has another bindservice after startservice, what is its lifecycle? Can the stopservice end the service?
The answer is no. If a service performs the BIND operation, then stopservice cannot end the service, knowing that all the bound context ends, or unbinding, service.

5. In the onstart method of service, what does the parameter startid mean and what is its usefulness?
After you start a service using startservice, you must manually disable the service. Therefore, the Service may call the stopself method to stop itself when the task is completed.
However, a problem occurs at this time. If startservice calls twice, the onstart operation is executed twice. After the first onstart operation is completed, the Service may think that the task has been completed, and end yourself. At this time, the second onstart may not be over, causing the task to be forcibly closed before completion.
Startid indicates a number called by the current onstart. when the service is terminated, stopself (startid) is called. In this way, the service can check whether this startid was last called. If yes, the service is terminated. Otherwise, the service is not terminated.

If any errors occur, you are welcome to discuss them.

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.