------

[ AD ] Port Monitor ( Try to use a Best WebSite Monitoring Tool )

------
http://goo.gl/70pR 

Painless Threading
고통 없는 쓰레드

AsyncTask를 사용하거나  사용하지 않더라도, 항상 기억하라 두가지 법칙 싱글 쓰레드 모델에서

Regardless of whether or not you use AsyncTask, always remember these two rules about the single thread model:

  1. Do not block the UI thread, and 

UI 쓰레드를 묶어 두지 말라

 

  1. Make sure that you access the Android UI toolkit only on the UI thread.

오직 UI 쓰레드 위에서만 안드로이드 UI 툴킷을 접근한다는 것을 명심하라

AsyncTask just makes it easier to do both of these things.

AsyncTask는 단지 이런 것의 모두를 쉽게 만들어 준다.



  • You can specify the type, using generics, of the parameters, the progress values and the final value of the task
  • The method doInBackground() executes automatically on a worker thread
  • onPreExecute(), onPostExecute() and onProgressUpdate() are all invoked on the UI thread
  • The value returned by doInBackground() is sent to onPostExecute()
  • You can call publishProgress() at anytime in doInBackground() to execute onProgressUpdate() on the UI thread
  • You can cancel the task at any time, from any thread

  • source code of Shelves (ShelvesActivity.java and AddBookActivity.java)

     Photostream (LoginActivity.java, PhotostreamActivity.java and ViewPhotoActivity.java)


    We highly recommend reading the source code of Shelves to see
    how to persist tasks across configuration changes and
    how to cancel them properly when the activity is destroyed.
    어떻게 액티비티가 파괴될때 그것들을 적절하게 취소하는가


    사용법
    클래스화
     private class DownloadFilesTask extends AsyncTask {     protected Long doInBackground(URL... urls) {         int count = urls.length;         long totalSize = 0;         for (int i = 0; i < count; i++) {             totalSize += Downloader.downloadFile(urls[i]);             publishProgress((int) ((i / (float) count) * 100));         }         return totalSize;     }     protected void onProgressUpdate(Integer... progress) {         setProgressPercent(progress[0]);     }     protected void onPostExecute(Long result) {         showDialog("Downloaded " + result + " bytes");     } }
    

    생성,행
    new DownloadFilesTask().execute(url1, url2, url3);
    

    + Recent posts