Tuesday, June 26, 2012

The original link 

http://android-developers.blogspot.com/2009/05/painless-threading.html 

Painless thread 

Whenever you start android application 
Will be called to the implementation of the main thread automatically generated. 

The main thread (also called the UI thread) is very important, 


Because it used to schedule events to the appropriate component also contains the event to draw the user interface. 



It is also the Android interactive widget where the thread. 



For example, suppose you touch a button on the screen, 
UI thread will touch event is assigned to that order is set to "press the" status and 
Proposed a "failure" of the request (request) to the event queue. Widget. 
The UI thread from the queue to obtain the request and informed the components to redraw themselves. 
This single-threaded model did not take into account its impact in the Android application 
May result in poor performance. 
If everything happened in a single thread to do the computation of the long 
Such access or information with the database, this thread will lock the user's screen. 
When these long operations in progress, will be no event can be triggered 
Contains events redraw the screen. 
When the user sees the screen froze in motion, or even worse situation, 
Like to occupy the screen of the UI thread is about more than 5 seconds. 
The screen will jump out of the evil the ANR warning window. 
Suppose you want to see how bad such a situation, write a simple application 
Above a button down event to the Thread.sleep (2000), 
This button will appear about 2 seconds the screen will return to normal bounce of the screen is pressed, 
When this happens, the program makes it easy for users to feel "roar ~ slow enough. 
Now you know you need to avoid long operation on the UI thread. 
You may use extra threads (background processing thread) to perform these operations, 
This is indeed a must do. 
Let's look at an example, when the implementation of the click event, 
It will download pictures from the network and load the ImageView inside. 


  public void onClick (View v) { 
         new Thread (new Runnable () { 
                public void run () { 
                      Bitmap b = loadImageFromNetwork (); 
                     mImageView.setImageBitmap (b); 
                } 
         }). Start (); 
  } 



First of all, this code seems to be a simple solution to your problem. 


It does not occupy the UI thread, but unfortunately 


It violates the single thread model: the Android UI tools are not thread-safe 


(A lot of thread execution does not affect each other), and must operate in the UI thread above. 


In the above code, the ImageView is performed in the other thread, 


It will result in a very strange question to find out that this error is very difficult and time-consuming. 


Android offers several ways to access the UI thread from other threads. 


The following is the complete list of these methods, you may already be familiar with some of them. 



  ‧ Activity.runOnUiThread (Runnable) 
  ‧ View.post (Runnable) 
  ‧ View.postDelayed (Runnable, long) 
  ‧ Handler 


In one of these classes and methods can be used to solve the problem of our previous example. 


  public void onClick (View v) { 
    new Thread (new Runnable () { 
      public void run () { 
        final Bitmap b = loadImageFromNetwork (); 
        mImageView.post (new Runnable () { 
          public void run () { 
            mImageView.setImageBitmap (b); 
          } 
        }); 
      } 
    }). Start (); 
  } 



Unfortunately, these categories with the method that drives your code becomes more complex and difficult to read. 


It is even worse is when you perform complex calculations, frequent UI updates. 


To solve this problem, Android1.5 a new category called AsyncTask 


It simplifies the creation of the long-running tasks need to communicate with the user interface program. 


With 1.1 AsyncTask in Android1.0 also UserTask this name exists. 


It provides additional same API, and you only need to copy the source code in your program inside. 


AsyncTask The purpose is to help you deal with thread management, 


Our previous example can be easily AsyncTask be rewritten as follows: 


  public void onClick (View v) { 
    new DownloadImageTask (). execute ("http://example.com/image.png"); 
  } 
 
  private class DownloadImageTask extends AsyncTask { 
       protected Bitmap doInBackground (String. .. urls) { 
           return loadImageFromNetwork (urls [0]); 
       } 
 
       protected void onPostExecute (Bitmap result) { 
           mImageView.setImageBitmap (result); 
       } 
   } 





As you can see, AsyncTask must be inherited, 


AsyncTask entity must be created in the UI thread and can only be executed once, 


This is very important. 


AsyncTask the file you can get a complete understanding of how to use this class, 


This is how it works a quick overview. 

Leave a Reply

Subscribe to Posts | Subscribe to Comments

- Copyright © .Hacking Cracking Tricks And Tutorials, Paid Scripts, Latest Exploits, 0Day Vulnerability, - Skyblue - Powered by Blogger - Designed by Johanes Djogan -