Mxyk
Mxyk

Reputation: 10698

AsyncTask Not Working as Intended

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:

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

Upvotes: 2

Views: 579

Answers (1)

user658042
user658042

Reputation:

You are starting a new thread inside an already seperated thread.

doInBackground() gets executed in a different thread then the UI-methods onPreExecute(), onPostExecute() and onProgressUpdate() 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 the AsyncTask class does for you already.

Regarding progress: onProgressUpdate() gets called every time you execute publishProgress(). So this is correctly implemented, you just have to call publishProgress() multiple times during the upload process to update the progressbar accordingly (e.g. every percent sent).

Upvotes: 4

Related Questions