如何在Android中正确使用AsyncTask

时间:2013-01-10 04:47:54

标签: android android-asynctask

我不想将任何参数传递给AsyncTask的doInBackground方法。

那么代码应该是什么样的?

4 个答案:

答案 0 :(得分:94)

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;

public class AsyncExample extends Activity{


private String url="http://www.google.co.in";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}


@Override
protected void onResume() {
    // TODO Auto-generated method stub
    super.onResume();

    new AsyncCaller().execute();

}

private class AsyncCaller extends AsyncTask<Void, Void, Void>
{
    ProgressDialog pdLoading = new ProgressDialog(AsyncExample.this);

    @Override
    protected void onPreExecute() {
        super.onPreExecute();

        //this method will be running on UI thread
        pdLoading.setMessage("\tLoading...");
        pdLoading.show();
    }
    @Override
    protected Void doInBackground(Void... params) {

        //this method will be running on background thread so don't update UI frome here
        //do your long running http tasks here,you dont want to pass argument and u can access the parent class' variable url over here


        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);

        //this method will be running on UI thread

        pdLoading.dismiss();
    }

    }
}

答案 1 :(得分:16)

根据AsyncTask

 AsyncTask<Params, Progress, Result>
  • Params,执行时发送给任务的参数类型。
  • 进展,期间发布的进度单位的类型 背景计算。
  • 结果,背景计算结果的类型。

因此,如果你想在 doInBackground 中传递void,只需传递void代替Params。

示例代码:

class DownloadLink extends AsyncTask<Void, Void, Void> {


        @Override
        protected Void doInBackground(Void... params) {
            // TODO Auto-generated method stub

            //Do Your stuff here..
            return null;
        }
    }

并将其命名为:

 new DownloadLink().execute();

答案 2 :(得分:9)

创建AsyncTask课程,就像您不想将任何参数传递给doInBackground一样:

 public class LongOperation extends AsyncTask<Void, Void, String> {


          public LongOperation(Context context) {

          }

          @Override
          protected void onPreExecute() {

          }

          @Override
          protected String doInBackground(Void... params) {

              return null;
          }      

          @Override
          protected void onPostExecute(String result) {                           

          }
    }

并启动AsyncTask而不传递任何参数来执行:

   LongOperation longOperation = new LongOperation(this);
   longOperation.execute();

答案 3 :(得分:8)

为什么不想传递任何参数呢?你应该解释一下......

通常这样做(例子):

 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));
             // Escape early if cancel() is called
             if (isCancelled()) break;
         }
         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);

来源:Android docs