2015年3月18日 星期三

[Android] Painless threading (轉貼)


Android offers several ways to access the UI thread from other threads. You may already be familiar with some of them but here is a comprehensive list:


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 classes and methods also tend to make your code more complicated and more difficult to read. It becomes even worse when your implement complex operations that require frequent UI updates. To remedy this problem, Android 1.5 offers a new utility class, called AsyncTask, that simplifies the creation of long-running tasks that need to communicate with the user interface.

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

沒有留言:

張貼留言