• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Updating a ProgressDialog with ByteArrayBuffer

kadmos

Newbie
Anyone have advice on how to structure a publishProgress() call within an AsyncTask to update a ProgressDialog bar while downloading a file using a ByteArrayBuffer? every example i can find uses publishProgress(""+(int)((total*100)/lenghtOfFile)) where lengthOfFile holds the value of URLConnection.getContentLength() but this does not work (or rather, i dont know how to make it work) when using a ByteArrayBuffer for the file download.


thanks
 
Are you able to see the number of bytes loaded in logcat?

Basically I'm not sure if you are having trouble finding the number, or having trouble showing that number to the user.


if you see here:

URLConnection | Android Developers

they use a "place holder" method called readStream(in);


In your version that method, you should be able to count the number of bytes read.

To publish that number to the dialog, use a Handler.


in your activity:
Code:
// using 5 as example, use any int here
private static final int PROG_ID = 5;   

private final Handler mHandler = new Handler() 
{
    @Override
    public void handleMessage(Message msg)
    {
        switch (msg.what) 
        {
            case PROG_ID:
            handleUpdateProgress( msg.arg1 );
            break;  
        }
    }
}; 

protected void handleUpdateProgress (int progressPercent)
{
    myProgressDialog.setProgress(progressPercent);
}
in your ASyncTask send the percent:

Code:
mHandler.obtainMessage(PROG_ID, percent, -1, null).sendToTarget();
(This is off the top of my head so might not work perfectly but should help you get the idea).
 
Are you able to see the number of bytes loaded in logcat?

Basically I'm not sure if you are having trouble finding the number, or having trouble showing that number to the user.


if you see here:

URLConnection | Android Developers

they use a "place holder" method called readStream(in);


In your version that method, you should be able to count the number of bytes read.

To publish that number to the dialog, use a Handler.


in your activity:
Code:
// using 5 as example, use any int here
private static final int PROG_ID = 5;   

private final Handler mHandler = new Handler() 
{
    @Override
    public void handleMessage(Message msg)
    {
        switch (msg.what) 
        {
            case PROG_ID:
            handleUpdateProgress( msg.arg1 );
            break;  
        }
    }
}; 

protected void handleUpdateProgress (int progressPercent)
{
    myProgressDialog.setProgress(progressPercent);
}
in your ASyncTask send the percent:

Code:
mHandler.obtainMessage(PROG_ID, percent, -1, null).sendToTarget();
(This is off the top of my head so might not work perfectly but should help you get the idea).

If he is using the AsyncTask class, he really doesn't need to use a Handler--provided the pointer to the ProgressBar object is marked as final. He can simply override the onPostExecute() method of AsyncTask and post directly to the ProgressBar from there.
 
doesn't onPostExecute only get called from the end of the task?

I'll have to admit I dont use AsyncTasks though. So I dont know, but I figured the scope was like any other off-the-ui-thread stuff.
 
doesn't onPostExecute only get called from the end of the task?

I'll have to admit I dont use AsyncTasks though. So I dont know, but I figured the scope was like any other off-the-ui-thread stuff.

Ah you are correct. It is only called after the background thread has finished executing. Sorry, I normally only use Thread/Handler combos too, so I suppose I wasn't really thinking of his exact implementation. Apologies. Though to clarify, you can use Handlers in both a Thread subclass and the doInBackground() method of the AsyncTask class, so either one would fit your needs just fine.
 
appreciate the responses guys...

unfortunately because im pressed for delivery i had to abandon using the ByteArrayBuffer method i was using for the downloads and revert back to using url.openConnection() so that i could use the value of its getContentLenth() method as a parameter for the publishProgress() method used by the progress dialog. the download is slower (which is why i had implemented the other model) but the client is content so long as there is a progress bar.

i will try and implement your solutions later, once i get this first version out the door.

thanks again.
 
Back
Top Bottom