AsyncTask 未按预期工作
这是我的 AsyncTask 类:
private class Task extends AsyncTask<Void, Integer, Void> {
@Override protected void onPreExecute() {
dia = new ProgressDialog(pictures.this);
dia.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
dia.setMessage("Please wait while content is loaded...");
dia.setCancelable(false);
dia.show();
}
@Override protected Void doInBackground(Void... voids) {
Thread thread = new Thread(null, uploader, "MagentoBackground");
thread.start();
//publishProgress(100); keep this commented or no?
return null;
}
@Override public void onProgressUpdate(Integer... prog) {
if (prog == null)
return;
dia.setProgress(prog[0]);
}
@Override protected void onPostExecute(Void voids) {
dia.setMessage("Done");
dia.cancel();
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}
}
我现在担心一个主要问题和一个小问题。以下是主要问题和次要问题,依次为:
此应用程序的目的是拍照并将其上传到网站。一旦用户点击“确定”按钮上传,AsyncTask 对话框就会显示,所以这很好。然而,它只显示对话框一瞬间,已经 100% 完成,然后强制关闭。然而,它肯定不是 100% 完成的!当我查看网站并刷新页面时,它不显示图片。 (在 WiFi 上,图片实际上会立即上传,并且当网站立即刷新时,它会显示新图片。但在 3G 上,这个过程可能需要几分钟 - 这就是为什么我希望用户看到进度照片正在上传)。
我不确定如何使用
onProgressUpdate
。我尝试了几个教程,但没有效果。如果任何人都可以说明我可以针对这种情况做些什么,那将会很有帮助
编辑:
Runnable
代码:
private Runnable uploader = new Runnable() {
@Override
public void run() {
/**JER 3**/
if (teacherInfo != null)
teacher = " - " + teacherInfo ;
else teacher = "" ;
if (schoolInfo != null )
school = " - " + schoolInfo ;
else school = "" ;
/********/
if(Descriptor.desString.equals("")) Descriptor.desString = "No description provided.";
int sessionId = rapi.createSession(experimentInput.getText().toString(),
name.getText().toString() + teacher + school + " - " + time,
Descriptor.desString, "n/a", "Lowell, MA", "");
JSONArray dataJSON = new JSONArray();
JSONArray subData = new JSONArray();
try {
subData.put(curTime); subData.put(Lat); subData.put(Long); subData.put(Descriptor.desString);
dataJSON.put(subData);
}
catch (JSONException e) {
e.printStackTrace();
}
Boolean result = rapi.updateSessionData(sessionId, experimentInput.getText().toString(), dataJSON);
if (result) {
rapi.uploadPictureToSession(picture, experimentInput.getText().toString(),
sessionId, name.getText().toString() + teacher + school + " - " + time,
name.getText().toString() + Descriptor.desString);
}
//if (m_ProgressDialog != null && m_ProgressDialog.isShowing()) m_ProgressDialog.dismiss();
}
};
Here is my AsyncTask class:
private class Task extends AsyncTask<Void, Integer, Void> {
@Override protected void onPreExecute() {
dia = new ProgressDialog(pictures.this);
dia.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
dia.setMessage("Please wait while content is loaded...");
dia.setCancelable(false);
dia.show();
}
@Override protected Void doInBackground(Void... voids) {
Thread thread = new Thread(null, uploader, "MagentoBackground");
thread.start();
//publishProgress(100); keep this commented or no?
return null;
}
@Override public void onProgressUpdate(Integer... prog) {
if (prog == null)
return;
dia.setProgress(prog[0]);
}
@Override protected void onPostExecute(Void voids) {
dia.setMessage("Done");
dia.cancel();
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}
}
There is one major and one minor problem I am concerned with right now. Here are the major and minor problems, consecutively:
This app's intention is to take a picture and upload it to a website. As soon as the user hits the 'OK' button to upload, the AsyncTask dialog shows, so this is fine. However, it only shows the dialog box for a split second, already 100% completed, and then force closes. However, it is most certainly not 100% complete! When I look at the website and refresh the page, it doesn't show the picture. (On WiFi, the picture actually does upload near immediately, and when the website is refreshed right away, it shows the new picture. But on 3G, this process can take minutes - which is why I want the user to see the progress of the photo being uploaded).
I'm not sure how to use the
onProgressUpdate
. I've tried several tutorials, to no avail. If anyone can state what I can do for this situation, it will be helpful
Edit:
Runnable
code:
private Runnable uploader = new Runnable() {
@Override
public void run() {
/**JER 3**/
if (teacherInfo != null)
teacher = " - " + teacherInfo ;
else teacher = "" ;
if (schoolInfo != null )
school = " - " + schoolInfo ;
else school = "" ;
/********/
if(Descriptor.desString.equals("")) Descriptor.desString = "No description provided.";
int sessionId = rapi.createSession(experimentInput.getText().toString(),
name.getText().toString() + teacher + school + " - " + time,
Descriptor.desString, "n/a", "Lowell, MA", "");
JSONArray dataJSON = new JSONArray();
JSONArray subData = new JSONArray();
try {
subData.put(curTime); subData.put(Lat); subData.put(Long); subData.put(Descriptor.desString);
dataJSON.put(subData);
}
catch (JSONException e) {
e.printStackTrace();
}
Boolean result = rapi.updateSessionData(sessionId, experimentInput.getText().toString(), dataJSON);
if (result) {
rapi.uploadPictureToSession(picture, experimentInput.getText().toString(),
sessionId, name.getText().toString() + teacher + school + " - " + time,
name.getText().toString() + Descriptor.desString);
}
//if (m_ProgressDialog != null && m_ProgressDialog.isShowing()) m_ProgressDialog.dismiss();
}
};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在一个已经分离的线程中启动一个新线程。
doInBackground()
在不同的线程中执行,然后 UI 方法onPreExecute()
、onPostExecute()
和onProgressUpdate()< AsyncTask 类的 /code>。您的异步任务在这里所做的就是启动第三个线程。这几乎不需要时间。之后您的任务完成,导致对话框立即关闭。
您可以在
doInBackground()
内进行上传,无需在那里启动新线程。这就是AsyncTask
类已经为您所做的事情。关于进度:每次执行
publishProgress()
时都会调用onProgressUpdate()
。因此,这是正确实现的,您只需在上传过程中多次调用publishProgress()
即可相应更新进度条(例如发送的每个百分比)。You are starting a new thread inside an already seperated thread.
doInBackground()
gets executed in a different thread then the UI-methodsonPreExecute()
,onPostExecute()
andonProgressUpdate()
of the AsyncTask class. All that your async task does here is start a third thread. This takes almost no time. After that your task is finished, resulting in the dialog closing immediately.You can do your uploading inside
doInBackground()
, you don't need to start a new thread there. That's what theAsyncTask
class does for you already.Regarding progress:
onProgressUpdate()
gets called every time you executepublishProgress()
. So this is correctly implemented, you just have to callpublishProgress()
multiple times during the upload process to update the progressbar accordingly (e.g. every percent sent).