Skip to main content

Vincent Tsao's Library tagged thread   View Popular, Search in Google

May
21
2009

    • public void onClick( View v ) {  
    •      new DownloadImageTask().execute( "http://example.com/image.png" );  
    • }  
    •   
    • private class DownloadImageTask extends AsyncTask {  
    •      protected Bitmap doInBackground( String... urls ) {  
    •           return loadImageFormNetwork( urls[0] );  
    •      }  
    •   
    •      protected void onPostExecute( Bitmap result ) {  
    •          mImageView.setImageBitmap( result );  
    •      }  
    • }  
    • 正如你看到的,使用AsyncTask必须要继承它。使用AsyncTask非常重要的是:AsyncTask的实例必须在UI线程中创建而且只能被使用一次。你可以使用预读AsyncTask的文档来来了解如何使用这个类,下面大概地了解一下它是如何工作的:

       
      • 你可以使用泛型参数制定任务的参数、中间值(progress values)和任何的最终执行结果
      • doInBackground()方法会自动地在工作者线程中执行
      • onPreExecute()、onPostExecute()和onProgressUpdate()方法会在UI线程中被调用
      • doInBackground()方法的返回值会被传递给onPostExecute()方法
      • 在doInBackground()方法中你可以调用publishProgress()方法,每一次调用都会使UI线程执行一次onProgressUpdate()方法
      • 你可以在任何时候任何线程中取消这个任务
  • 1 more annotation(s)...
1 - 11 of 11
Showing 20 items per page

Diigo is about better ways to research, share and collaborate on information. Learn more »

Join Diigo
Move to top