AsyncTask:是Android提供的輕量級的異步類,能夠直接繼承AsyncTask,在類中實現異步操做,並提供接口反饋當前異步執行的程度(能夠經過接口實現UI進度更新),最後反饋執行的結果給UI主線程。android
AsyncTask (異步任務),從字面上來講,就是在咱們的UI主線程運行的時候,異步的完成一些操做。AsyncTask容許咱們的執行一個異步的任務在後臺。咱們能夠將耗時的操做放在異步任務當中來執行,並隨時將任務執行的結果返回給咱們的UI線程來更新咱們的UI控件。經過AsyncTask咱們能夠輕鬆的解決多線程之間的通訊問題。git
4個步驟:當咱們執行一個異步任務的時候,其須要按照下面的4個步驟分別執行github
一個超簡單的理解 AsyncTask 的例子:AsyncTask來從網絡上加載一張圖片網絡
Activity代碼:多線程
1 private Button btn; 2 private ImageViewimageView; 3 privateProgressDialog progressDialog; 4 private final String IMGURL= "http://img0.pconline.com.cn/pconline/1206/18/2829090_3867bd63fd673471aa184c02_500.jpg"; 5 @Override 6 protected void onCreate(Bundle savedInstanceState) 7 { 8 super.onCreate(savedInstanceState); 9 setContentView(R.layout.asynctask_img); 10 btn=(Button)findViewById(R.id.buttonOnClicksAsyncTask); 11 imageView =(ImageView)findViewById(R.id.imageViewss); 12 progressDialog = newProgressDialog(this); 13 progressDialog.setTitle("提示信息"); 14 progressDialog.setMessage("正在下載中,請稍後......"); 15 progressDialog.setCancelable(false); 16 progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER); 17 btn.setOnClickListener(newView.OnClickListener() 18 { 19 @Override 20 public voidonClick(View v) 21 { 22 // 在UI Thread當中實例化AsyncTask對象,並調用execute方法 23 newMAsyncTask().execute(IMGURL); 24 } 25 }); 26 } 27 28 public class MAsyncTask extends AsyncTask<String, Integer, byte[]> 29 { 30 @Override 31 protected voidonPreExecute() 32 { 33 super.onPreExecute(); 34 progressDialog.show(); 35 } 36 @Override 37 protected byte[]doInBackground(String... params) 38 { 39 HttpClient httpClient = newDefaultHttpClient(); 40 HttpGet httpGet = newHttpGet(params[0]); 41 byte[] image =new byte[]{}; 42 try 43 { 44 HttpResponse httpResponse =httpClient.execute(httpGet); 45 HttpEntity httpEntity =httpResponse.getEntity(); 46 if(httpEntity!= null &&httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) 47 { 48 image = EntityUtils.toByteArray(httpEntity); 49 } 50 } 51 catch(Exception e) 52 { 53 e.printStackTrace(); 54 } 55 finally 56 { 57 httpClient.getConnectionManager().shutdown(); 58 } 59 return image; 60 } 61 @Override 62 protected voidonProgressUpdate(Integer... values) 63 { 64 super.onProgressUpdate(values); 65 } 66 @Override 67 protected voidonPostExecute(byte[] result) 68 { 69 super.onPostExecute(result); 70 // 將doInBackground方法返回的byte[]解碼成要給Bitmap 71 Bitmap bitmap = BitmapFactory.decodeByteArray(result,0, result.length); 72 // 更新咱們的ImageView控件 73 imageView.setImageBitmap(bitmap); 74 progressDialog.dismiss(); 75 } 76 } 77 }
一個ImageView控件和一個Button控件,當點擊Button控件時,彈出一個ProgressDialog,而後開啓一個異步任務,從網絡中下載一張圖片,並更新到咱們的ImageView上。這裏還要注意一點,若是咱們要訪問網絡,必須還要給其受權才行app
AndroidManifest.xml文件:異步
<uses-permission android:name="android.permission.INTERNET"/> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <activity android:name=".MainActivity" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application>
效果圖:async
源碼下載:ide
AndroidStudio下載: https://github.com/DickyQie/AsynTaskDemo.gitthis