问题描述
我查看了一些关于类似问题的其他问题,我发现我需要使用 onProgressUpdate
方法来更改 ProgressDialog
的消息.
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
.
例如,我有这样的代码在 AsyncTask
的 doInBackGround
中运行(这只是一个很小的示例):
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...");
当我最初这样运行时,我收到一个 Leaky Window 错误,说我无法在 AsyncTask
之外更改 dia
的消息.
When I originally run thus, I get a Leaky Window error saying that I can't change dia
's message outside of AsyncTask
.
所以我的问题是,当dia
的进度达到一定数量时,如何使用onProgressUpdate
来设置dia
的消息?(即,当 dia
的进度 = 30 时,让它说数据开始上传序列...")onProgressUpdate
显然必须始终检查 dia
的进度(我想就像一个侦听器)[如果它还没有这样做,我怎样才能做到这一点?]
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?]
推荐答案
你需要实现AsyncTask
的onProgressUpdate
方法.创建一个包含进度百分比和消息的新类,该类仅作为 onProgressUpdate
方法的唯一参数.
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.
在onProgressUpdate中,调用dia.setProgress
和dia.setMessage
.
In the onProgressUpdate, call dia.setProgress
and dia.setMessage
.
在 doInBackground 方法中,使用包含新百分比和消息的新类实例调用 publishProgress
.这将导致 AsyncTask
在主线程上调用 onProgressUpdate
.
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.
例如:
private static class TaskProgress {
final int percentage;
final String message;
TaskProgress(int percentage, String message) {
this.percentage = percentage;
this.message = message;
}
}
在您的 AsyncTask 中(将 ?s 替换为您的实现的正确类型):
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
}
}
这篇关于使用 AsyncTask 时更改对话框的消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!