"Android API Guides (iv)" Using a service or using threads?

Source: Internet
Author: User

A service is a component that can be run in the Android background, even if your app is turned off, and it's still running. So only if you need a background program that is not relevant to the current program, just create it.

And if you just want your app to work outside the main thread, and the operation is limited to the current program, just create another thread inside.

For example, if you just want to put a piece of music in your activity while it is running, you should create another thread in the Oncreat () method, start the thread in OnStart (), end the thread in OnStop (), and consider using Asynctask Orhandlerthread, instead of using the traditional threading class thread. (PS: Here you can refer to the processes and threading documentation, if you do not understand, I can translate to you see)

Keep in mind that if you do create a service, you should also create another thread in the service, otherwise, by default, the service takes up the main thread, which can cause threads to clog or memory to be overused. This is because unless you create another thread, a service will only run on his managed thread, which means that if you use the service to do any CPU intensive or blocking operations,

Your app will appear stuck or not responding to errors (ANR).


"Android API Guides (iv)" Using a service or using threads?

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.