Android Service vs Asynctask vs Thread

Source: Internet
Author: User

The three methods are designed to be different.

Service: For actions that run continuously in the background for long periods of time, such as: Play music, view network data. Note that in the development documentation, the service itself is in the UI thread, so the desired action should be to create a new thread to complete the work.

Asynctask: Used primarily to prevent a one-time/short action blocking the UI thread. For example: Click button to get the network data, here asynctask more appropriate. Because the action is finished when the data is received. The service can also be used to complete the work, but then the service is closed, inefficient.

Thread: This is the usual meaning of threading, note that the UI cannot be updated in threads. Because the UI framework itself is non-thread safe. But Android provides the following ways to update the UI.

    • Activity.runOnUiThread(Runnable)
    • View.post(Runnable)
    • View.postDelayed(Runnable, long)

eg

Public voidOnClick(ViewV) {
New Thread(New Runnable() {
Public voidRun() {
Final BitmapBitmap=Loadimagefromnetwork("Http://example.com/image.png");
Mimageview.Post(New Runnable() {
Public void Run () {
                    Mimageview. Setimagebitmapbitmap                 }
           
        }
    start ();
}

See official documentation: http://developer.android.com/guide/components/processes-and-threads.html
/span>

Android Service vs Asynctask vs Thread

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.