android.os.AsyncTask

來源:互聯網
上載者:User
簡介AsyncTask可以使得使用UI線程變的更容易更適當,它可以在後台運行一些操作然後在UI上展現,不用操作具體的線程和handlers一個 asynchronous task包括三種基本類型(調用參數,進度和結果),和四個步驟(調用開始,在後台運行,處理進度,結束)), and most often will override a second one (onPostExecute(Result).) 使用方法描述Asynchronous Task必須是作為一個子類來使用,task執行個體必須在UI線程建立execute(Params...)必須在UI線程調用不要手工調用onPreExecute(), onPostExecute(Result), doInBackground(Params...), onProgressUpdate(Progress...)。task只可以execute一次,執行多次就報異常一個例子類的定義 private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> {     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); 三種基本類型的說明Params, 傳給task的參數的類型Progress, 表示進度單位的類型Result, 傳回型別不是所有的task都需要定義類型,如果沒有則使用void,如下所示private class MyTask extends AsyncTask<Void, Void, Void> { ... } 四個步驟的說明onPreExecute():在task被執行之後,立刻調用doInBackground(Params...):onPreExecute執行完畢後,執行該方法,參數傳到了這個方法中,執行完畢後必須返回一個值,還可以使用 publishProgress(Progress...) 發布進度到onProgressUpdate(Progress...),便於更新進度onProgressUpdate(Progress...):publishProgress(Progress...)被調用後,就執行該方法,顯示進度資訊通常是顯示一個進度條,或在text域裡顯示日誌資訊onPostExecute(Result)當doInBackground(Params...)執行完畢後即執行該方法手機實名制 OCR功能中的執行個體例子一定義class OcrTask extends AsyncTask<String, Void, OcrMessage>{ @Override protected OcrMessage doInBackground(String... arg0) {  ApplyService service  = new ApplyService(getApplicationContext());  return service.ocrTest(save_file_path); } @Override   protected void onPostExecute(OcrMessage result)   {    returnImageScan(result);   }}調用OcrTask ocr = new OcrTask();ocr.execute(save_file_path);例子二儲存圖片任務,參數是byte數組,是圖片對應的byte數組class SavePhotoTask extends AsyncTask<byte[], Void, Integer>{      String picname;      @Override      protected Integer doInBackground(byte[]... datas)      {   byte[] data = datas[0];   Bitmap bitmap;   BitmapFactory.Options ops2 = new BitmapFactory.Options();   ops2.inSampleSize = 1;   bitmap = BitmapFactory.decodeByteArray(data,0, data.length,ops2);     try {   FileOutputStream fos = OcrCameraActivity.this.openFileOutput("output.jpg",Activity.MODE_WORLD_READABLE);   bitmap.compress(Bitmap.CompressFormat.JPEG, 80, fos);   fos.flush();   fos.close();   save_file_path = "output.jpg";  }catch (Exception e) {   e.printStackTrace();  }  return 0;      }        @Override   protected void onPostExecute(Integer result)   {    call_OCR();   }}調用SavePhotoTask savePhotoTask = new SavePhotoTask();savePhotoTask.execute(data);

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.