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();
}
};
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 theAsyncTaskclass 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).