Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
684 views
in Technique[技术] by (71.8m points)

android - Change a dialog's message while using AsyncTask?

I looked at a few other questions regarding a similar issue, and I figured out that I need to use the onProgressUpdate method to change the message of ProgressDialog.

For example, I have code like this that runs in the AsyncTask's doInBackGround (this is just a very small sample):

        byte[] data = getBytesFromFile(image);

        String lineEnd = "
";
        String twoHyphens = "--";
        String boundary = "*****";
        pictures.dia.setProgress(30);
        pictures.dia.setMessage("Data beginning upload sequence...");

        URL connectURL = new URL(this.base_url);
        HttpURLConnection conn = (HttpURLConnection) connectURL.openConnection();
        conn.setDoInput(true);
        conn.setDoOutput(true);
        conn.setUseCaches(false);
        conn.setRequestMethod("POST");

        conn.setRequestProperty("Connection", "Keep-Alive");
        conn.setRequestProperty("Content-Type", "multipart/form-data, boundary=" + boundary);

        DataOutputStream dos = new DataOutputStream(conn.getOutputStream());
        pictures.dia.setProgress(40);
        pictures.dia.setMessage("Output Stream prepared...");

When I originally run thus, I get a Leaky Window error saying that I can't change dia's message outside of AsyncTask.

So my question is, how do I use onProgressUpdate to set the message of dia when dia's progress reaches a certain number? (i.e., when dia's progress = 30, make it say "Data beginning upload sequence...")
onProgressUpdate must obviously always be checking dia's progress (like a listener, I suppose)[if it doesn't already do this, how can I make it do this?]

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You need to implement the onProgressUpdate method of AsyncTask. Create a new class that holds the progress percentage and the message only to be the onProgressUpdate method's only parameter.

In the onProgressUpdate, call dia.setProgress and dia.setMessage.

In your doInBackground method, call publishProgress with an instance of your new class that contains the new percentage and message. That will cause the AsyncTask to call onProgressUpdate on the main thread.

For example:

private static class TaskProgress {
    final int percentage;
    final String message;

    TaskProgress(int percentage, String message) {
        this.percentage = percentage;
        this.message = message;
    }
}

In your AsyncTask (replace the ?s with the correct types for your implementation):

public ProgressAsyncTask extends AsyncTask<?, TaskProgress, ?> {

    public void onProgressUpdate(TaskProgress progress) {
         pictures.dia.setProgress(progress.percentage);
         pictures.dia.setMessage(progress.message);
    }

    public ? doInBackground(?... params) {
        // ... your code       
        publishProgress(new TaskProgress(30, "A new update"));
        // ... your code 
    }

}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...