How to use AsyncTask correctly in Android [closed]

2018-12-31 22:26发布

I don't want to pass any arguments to doInBackground method of the AsyncTask.

So what should be the code like?

4条回答
低头抚发
2楼-- · 2018-12-31 22:55

Create your AsyncTask class as if you don't want to pass any parameter to 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) {                           

          }
    }

and start AsyncTask as without passing any parameter to execute :

   LongOperation longOperation = new LongOperation(this);
   longOperation.execute();
查看更多
泛滥B
3楼-- · 2018-12-31 22:56
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();
    }

    }
}
查看更多
只靠听说
4楼-- · 2018-12-31 22:56

Why don't you want to pass any arguments to it? You should explain...

This is how it usually works (example):

 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");
     }
 }

And to execute it you call:

new DownloadFilesTask().execute(url1, url2, url3);

Source: Android docs

查看更多
忆尘夕之涩
5楼-- · 2018-12-31 23:06

According to AsyncTask, its

 AsyncTask<Params, Progress, Result>
  • Params, the type of the parameters sent to the task upon execution.
  • Progress, the type of the progress units published during the background computation.
  • Result, the type of the result of the background computation.

So if you want to pass void in doInBackground just pass void in place of Params.

Example code:

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


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

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

And call it as:

 new DownloadLink().execute();
查看更多
登录 后发表回答