开发者

Can't get publishProgress to work properly. Help?

I have an AsyncTask that take开发者_Go百科s urls and gets the content length for all urls that are passed through. I'd like to have the progress bar update in the titlebar and have an alertbox display when it is done getting the final value. But I'm having trouble understanding how to make this all function together. The code I have now moves the progress bar but will not have the alertbox show. I don't have a custom title bar, instead I'm calling the requestWindowFeature(FEATURE_PROGRESS). Any help would be appreciated.

This is my Async Class

    private class MyClass extends AsyncTask<URL, Integer, Float>
    {
         protected Float doInBackground(URL... urls)
         {
              try
              {
                for(int i = 0; i<urls.length; i++)
                {
                    url = urls[i];
                    urlConnection = (HttpURLConnection) url.openConnection();
                    urlConnection.setRequestMethod("GET");
                    urlConnection.setDoOutput(true);
                    urlConnection.connect();
                    size = urlConnection.getContentLength();
                    total += size; 
                    while(progress<total)
                    {
                        progress++;
                        publishProgress(progress);
                    }

                }
            }
            catch(MalformedURLException e)
            {
                e.printStackTrace();
            }
            catch(IOException iox)
            {
                iox.printStackTrace();
            }
            finally
            {
                urlConnection.disconnect();
            }
               return total;
    }

       protected void onProgressUpdate(Integer... values)
       {
        setProgressBarVisibility(true);
        setProgress(values[0]);     
   }

Edit: I understand that publishProgress passes data to onProgressUpdate from the doInBackground method. But with this code above, all the urls content length are being added to "total". And I think the above code is correct. My implementation of this is: Push a button from the main class. That button passes all the urls to the AsyncTask. While the urls get the length and add to total (in background) a progress bar appears in the title bar until the background work is done. Then an alert dialog prompts the user for a choice.

What is happening though is the progress bar reaches the end and the alert dialog doesn't show up. Before I started adding this progress bar, the alert dialog showed up. Now it doesn't. How can I go about getting the progress bar to increment properly with the url content length....dismiss the progress bar.....then load the alert dialog in onPostExecute?

    protected void onPostExecute(final Float result)
    {       

        setProgressBarVisibility(false);

        AlertDialog.Builder alertbox = new AlertDialog.Builder(Vacation.this);
        alertbox.setMessage(TEXT STUFF);
        alertbox.setPositiveButton("Yes", new DialogInterface.OnClickListener() 
        {
            public void onClick(DialogInterface arg0, int arg1) 
            {

            }
        });
        alertbox.setNegativeButton("No", new DialogInterface.OnClickListener()
        {
            public void onClick(DialogInterface arg0, int arg1)
            {

            }
        });
        alertbox.show();    

    } 


sat is right but u can do that in onPostExecute() methode which is having the UI thread access always remember only doInBackground() does not have UI thread access rest all 3 methode has. if you need more than be specific and show the code where you are finding difficulty.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜