0

I have an async task that loads image urls from server.After loading urls than i load the images one by one through another asynctask. On the click of a button i start the first asynctask

    public void getit(View v)
    {

        new getdata().execute("http://10.0.2.2/geturls.php");
// line no 2
    }

After i get the urls i use another async task to load images. How can i find out when the image urls have been loaded and i can call the second async task at line no 2. if i use a boolean variable which i toggle in the onpostexecute

 @Override
            protected void onPostExecute() {
urlgot=true;
}

then i shall have to use some repeating loop inside getit method at line no 2 to check the status of this variable urlgot. but it may take more time than allowed for ui thread. Can there be a more cleaner method to do this check. thanks

user2779311
  • 1,688
  • 4
  • 23
  • 31
  • Are you using listview for your images ? or are they just an imageView ? if you are using listView you can use the asyncTask once in ur adapter and give it's position to set it automatically. – Rudi Dec 05 '13 at 13:48

5 Answers5

2

There are two solutions I can think of:

1) You create one AsyncTask that does everything (getting the urls, and downloading all images). Than you know exactly when to start downloading the images.

2) You start the next AsyncTask from the onPostExecute() of the first AsyncTask.

Jeffrey Klardie
  • 3,020
  • 1
  • 18
  • 23
  • 1
    From design perspective it's better to have a Manager class to handle the download, I use your second option when deadline is really tight, but it just gets me in trouble on next update :) – Ahmad Dwaik 'Warlock' Dec 05 '13 at 14:03
  • That depends on the size of the project. But yes, creating an image manager that handles the background work itself would be the perfect solution. – Jeffrey Klardie Dec 05 '13 at 22:30
0

You won't be able to do your next piece of work in //line no 2 without defeating the purpose of AsyncTask. If you're doing network activity, you need to be doing it asynchronously, so that's not an option.

Instead, in onPostExecute() you can call another method in your activity that does what you would have done in //line no 2. This is safe to do, because onPostExecute() happens on the UI thread.

But depending on your design, it might make more sense to do all the //line no 2 stuff in your original AysncTask in onPostExecute, so you only have one task doing all of the work.

Tenfour04
  • 83,111
  • 11
  • 94
  • 154
0

I use a custom interface to do stuff after execution.

public Interface OnDataReceived
{
    public void onReceive( Object result);
}

and on MyASyncTask

public class MyAsyncTask extends ASyncTask<Object,Object,Object>
{
    OnDataReceived receiver;
    public MyAsyncTask( OnDataReceived receiver )
    {
        this.receiver  = receiver;
    }
...
    protected void onPostExecute( Object result)
    {
        receiver.onreceive( result );
    }
}

and let my main class implement OnDataReceived

public class Main implements OnDataReceived 
{
....
    public void getit(View v)
    {
        new MyAsyncTask(this).execute("http://10.0.2.2/geturls.php");
    }
    @override
    public void onReceive( Object result)
    {
        // do whatever
    }
}

EDIT

Even for more control you can add onFailed and rename your interface to OnResponse

public Interface OnResponse
{
    public void onReceive( Object result);
    public void onFailed( Object errcode);
}
Ahmad Dwaik 'Warlock'
  • 5,953
  • 5
  • 34
  • 56
0

Use a Handler. In the method onPostExecute of your AsyncTask you can send a message informing the Handler to start another AsyncTask.

Something like this:

@Override
    protected void onPostExecute(Void res) {            

        MyHandlerHandler handler = new MyHandlerHandler();
        Message msg = new Message();
        msg.what = MyHandler.TASK_FINISHED;
        handler.sendMessage(msg);
    }

And in your Handler class:

public class MyHandlerHandler extends Handler {
            public static final int TASK_FINISHED = 2;
    @Override
    public void handleMessage(Message msg) {
        switch (msg.what) {

        case TASK_FINISHED:
            new MyAsyncTask().execute();
            break;
        }
    }
}
leandrocastelli
  • 536
  • 3
  • 16
0

instead of putting line 2 in getIt, put it in onPostExecute like below :

public void getit(View v)

    {
        new getdata().execute("http://10.0.2.2/geturls.php");
    }



   @Override
    protected void onPostExecute() {
      // line 2 
   }
Rudi
  • 4,304
  • 4
  • 34
  • 44