Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing nothing to an AsyncTask in Android

How do I instantiate and call and class extending AsyncTask if I am passing nothing to it?

Also, I am setting a textview in the UI to the calculated result.

Thanks, Ben

like image 205
Ben Taliadoros Avatar asked Oct 03 '11 13:10

Ben Taliadoros


2 Answers

Example implementation for Async without params and result Bitmap in onPostExecute result

/**
 * My Async Implementation without doInBackground params
 * 
 */
private class MyAsyncTask extends AsyncTask<Void, Void, Bitmap> {

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

        Bitmap bitmap;

        .... 

        return bitmap;
    }

    protected void onPostExecute(Bitmap bitmap) {

        ....
    }
}

In your activity, you should to add this implementation:

MyAsyncTask myAsyncTask = new MyAsyncTask();
myAsyncTask.execute();
like image 120
d.danailov Avatar answered Sep 19 '22 05:09

d.danailov


I think what you meant to ask was how do I write an AsyncTask that doesn't ask for any parameters. The trick is to define what you expect to use as parameter and return value in the extension of your class: class MyClass extends AsyncTask<Void, Void, Void> for example doesn't expect any parameters and doesn't return any either. AsyncTask<String, Void, Drawable> expects a String (or multiple strings) and returns a Drawable (from its own doInBackground method to its own onPostExecute method)

like image 28
Lars Avatar answered Sep 22 '22 05:09

Lars