Android: how to wait for AsyncTask to complete in MainThread?

I know that the first thing you are going to do is ... why the hell in the world that you used in AsyncTask.

So here is my problem: I am working on some Android application (API 7 for Android version 2.1 or higher), and I am testing the emulator, and everything was fine, so I tested on HTC Sensation and it says NetworkOnMainThreadExeption!

I uploaded a few photos and then painted on a map.

Therefore, to solve this problem, everyone (Internet connection) in this case uploads the images that I must use for AsyncTask to work.

So, I need a method to find out when all the pictures are taken, so I can start drawing.

I tried so much, and I had no idea whatsoever. I have one solution with a handler, but if you are working on a slower network, I get nullpointer (because the pictures do not load).

So please help me.

EDIT:

here is the idea:

Bitmap bubbleIcon ; onCreate(){ ... // i am making call for Async new ImgDown().execute(url); //and then i calling functions and classes to draw with that picture bubbleIcon ! DrawOnMap(bubbleIcon); } //THIS IS ASYNC AND FOR EX. SUPPOSE I NEED TO DOWNLOAD THE PIC FIRST class ImgDown extends AsyncTask<String, Void, Bitmap> { private String url; public ImgDown() { } @Override protected Bitmap doInBackground(String... params) { url = params[0]; try { return getBitmapFromURL(url); } catch (Exception err) { } return null; } @Override protected void onPostExecute(Bitmap result) { bubbleIcon = result; bubbleIcon = Bitmap .createScaledBitmap(bubbleIcon, 70, 70, true); } public Bitmap getBitmapFromURL(String src) { try { Log.e("src", src); URL url = new URL(src); HttpURLConnection connection = (HttpURLConnection) url .openConnection(); connection.setDoInput(true); connection.connect(); InputStream input = connection.getInputStream(); // /tuka decode na slika vo pomalecuk kvalitet! BitmapFactory.Options options = new BitmapFactory.Options(); options.inSampleSize = 3; Bitmap myBitmap = BitmapFactory .decodeStream(new FlushedInputStream(input)); Log.e("Bitmap", "returned"); return myBitmap; } catch (IOException e) { e.printStackTrace(); Log.e("getBitmapFromURL", e.getMessage()); return null; } } class FlushedInputStream extends FilterInputStream { public FlushedInputStream(InputStream inputStream) { super(inputStream); } public long skip(long n) throws IOException { long totalBytesSkipped = 0L; while (totalBytesSkipped < n) { long bytesSkipped = in.skip(n - totalBytesSkipped); if (bytesSkipped == 0L) { int byteValue = read(); if (byteValue < 0) { break; // we reached EOF } else { bytesSkipped = 1; // we read one byte } } totalBytesSkipped += bytesSkipped; } return totalBytesSkipped; } } } 

Hope now clearer.

+11
android handler android-asynctask solution
Oct 26
source share
4 answers
 class OpenWorkTask extends AsyncTask { @Override protected Boolean doInBackground(String... params) { // do something return true; } @Override protected void onPostExecute(Boolean result) { // The results of the above method // Processing the results here myHandler.sendEmptyMessage(0); } } Handler myHandler = new Handler() { @Override public void handleMessage(Message msg) { switch (msg.what) { case 0: // calling to this function from other pleaces // The notice call method of doing things break; default: break; } } }; 
+32
Oct 26 '12 at 2:20
source share

You can write your own delegate to delegate information about completing a task using the principles of OOP:

task_delegate.java

 public interface TaskDelegate { void TaskCompletionResult(String result); } 

main_activity.java

 public class MainActivity extends Activity implements TaskDelegate { //call this method when you need private void startAsynctask() { myAsyncTask = new MyAsyncTask(this); myAsyncTask.execute(); } //your code @Override public void TaskCompletionResult(String result) { GetSomethingByResult(result); } } 

my_asynctask.java

 public class MyAsyncTask extends AsyncTask<Void, Integer, String> { private TaskDelegate delegate; protected MyAsyncTask(TaskDelegate delegate) { this.delegate = delegate; } //your code @Override protected void onPostExecute(String result) { delegate.TaskCompletionResult(result); } } 
+5
Dec 30 '15 at 12:48
source share
 class openWorkTask extends AsyncTask<String, String, Boolean> { @Override protected Boolean doInBackground(String... params) { //do something return true; } @Override protected void onPostExecute(Boolean result) { // The results of the above method // Processing the results here } } 
+2
Oct 26
source share

I would use progress dialogues if I were you. This way, users can see that something happens when ASyncTask loads the image. In PostExecute, call a method from your main code that checks if the images are null. Remember that you cannot update the user interface in the doInBackground method, so any user interface works either in onPreExecute or onPostExecute

 private class DownloadPictures extends AsyncTask<String, Void, String> { ProgressDialog progressDialog; @Override protected String doInBackground(String... params) { //Download your pictures return null; } @Override protected void onPostExecute(String result) { progressDialog.cancel(); //Call your method that checks if the pictures were downloaded } @Override protected void onPreExecute() { progressDialog = new ProgressDialog( YourActivity.this); progressDialog.setMessage("Downloading..."); progressDialog.setCancelable(false); progressDialog.show(); } @Override protected void onProgressUpdate(Void... values) { // Do nothing } } 
0
Oct 26 '12 at 2:11
source share



All Articles