Widespread Augmented Reality

Widespread Augmented Reality
Click on the image to get the Android Augmented Reality Heads up Display

Thursday, April 9, 2015

Android Async Task and Multiple Parms

I am building a pop up window to display text that is available on the main UI, but the pop up also needs to display an image that is downloaded from the server. Therefore the entire method that builds the pop up window needs to be on a background thread, for building the window text on the main UI and then making a network image call results in the image not being in sync with the text description.

In order to build the pop up window in the background, I need to pass parms and a URL into the Async Task.
To do this I essentially created a Popup variable like so:
 
public static class PopupParms {

             private String burl; 

             private int type;  

             private int position; 

             private Bitmap bm; 
 
}

This class is inside the larger class that contains the async task.


Then I instantiate on the main UI with:
PopupParms pp = new PopupParms() and assign values like:
pp.burl = a_url;
pp.position = row_number;
pp.type = row_type;
etc...
Now I call the AsyncTask with:
new buildPopUpWithURL().execute(pp);
The AsyncTask class will look like this:
public class buildPopupWithURL extends AsyncTask 

     '<'PopupParms, Integer, PopupParms'>' { 


  @Override
  protected void onPreExecute() { 

    }

  @Override

  protected PopupParms doInBackground(PopupParms... parms) { 

    
    HttpGet reuest = new HttpGet(parms[0].burl); 

    . 

    . 

    . 

    Bitmap bm = BitmapFactory.decodeStream(inputstream, null, null); 

    PopupParms pp = new PopupParms(); 

    pp.bm = bm; // broken out for illustration 

    pp.position = parms[0].position; // broken out for illustration 

    pp.type = parms[0].type; // broken out for illustration  

    return pp; 

  } 

  @Override 

  protected void onPostExecute(PopupParms p) { 

    // broken out for illustration 

    PopupParms pms = new PopupParms();  

    pms.type = p.type; 

    pms.position = p.position; 

    pms.bm = p.bm; 

    // A build method on the main UI
    buildPopup( pms.position, pms.type, pms.bm); 



  }

 

In conclusion, I created a custom class variable that contains the multiple fields that need to be passed into an AsyncTask. After I pass that class variable into the AsyncTask, I parse out and read the member fields of that class variable.
High level representation of passing multiple parameters into an AsyncTask:

You can see this live with SpiderOnFire's Widespread Augmented Reality app.

No comments:

Post a Comment