0

Uploading information using AsyncTask in android and notifying user once the task is completed


  • What am i trying to Do:: I am trying to upload information to server from android

  • What have i tried:: I have done the image uploading part, I have used Async-task for this & the functionality works

What i am trying to do::

  • I want to show a Toast message once the file uploading is done as "File Uploaded"
  • How can i achieve this ?

MainActivity.java

public class DataAcceptActivity extends Activity {

    <----------------------Code-------------->
    public class MainTest extends AsyncTask<String, Integer, String> {

        @Override
        protected void onPreExecute() {
            pDialog = new ProgressDialog(DataAcceptActivity.this);
            pDialog.setMessage("Loading..");
            pDialog.setIndeterminate(true);
            pDialog.setCancelable(false);
            pDialog.show();
        }

        @Override
        protected String doInBackground(String... params) {

            postImageData();

            return null;
        }

        @Override
        protected void onPostExecute(String result) {
            // TODO Auto-generated method stub

            super.onPostExecute(result);
            // data=jobj.toString();
            pDialog.dismiss();

        }

    }


    public class ImageAdapter extends BaseAdapter{

        <_----------------- code _-------------->

}

Thanks !!

smriti3
  • 871
  • 2
  • 15
  • 35
  • Similar topics already in SO http://stackoverflow.com/questions/2837676/how-to-raise-a-toast-in-asynctask-i-am-prompted-to-used-the-looper http://stackoverflow.com/questions/4591878/updating-progress-dialog-in-activity-from-asynctask – Mojo Risin Dec 19 '13 at 10:55

6 Answers6

1

You can show the toast message in the onPostExecute() method of your async task.

nikvs
  • 1,090
  • 5
  • 9
1

You can simply display a toast in onPostExecute

 @Override
        protected void onPostExecute(String result) {
            // TODO Auto-generated method stub

            super.onPostExecute(result);
            Toast toast = Toast.makeText(context, text, duration);
             toast.show();
            pDialog.dismiss();

        }
Madhur Ahuja
  • 22,211
  • 14
  • 71
  • 124
1

In AsyncTask you have to notified in onPostExecute method:

So use this:

@Override
    protected void onPostExecute(String result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);
        Toast toast = Toast.makeText(YourActivity.this, "Message Here", duration).show();
        pDialog.dismiss();

    }

It also depends on your Response that which type of message do you want to display.

If you are not using AsyncTask then you can also use Handler.

Piyush
  • 18,895
  • 5
  • 32
  • 63
1

on the your asynctask

@Override
protected void onPostExecute() {



    Toast toast = Toast.makeText(getApplicationContextt, "File Uploaded", Toast.LENGTH_LONG).show();

}
Nambi
  • 11,944
  • 3
  • 37
  • 49
1
public class MainTest extends AsyncTask<Void, Void, Boolean> {

        @Override
        protected void onPreExecute() {
            pDialog = new ProgressDialog(DataAcceptActivity.this);
            pDialog.setMessage("Loading..");
            pDialog.setIndeterminate(true);
            pDialog.setCancelable(false);
            pDialog.show();
        }

        @Override
        protected Boolean doInBackground(Void... params) {

            postImageData();

            return true;
        }

        @Override
        protected void onPostExecute(Boolean result) {
            // TODO Auto-generated method stub

            super.onPostExecute(result);
            // data=jobj.toString();
             if(result)
         {
          Toast.makeText(_context , "Image uploaded.." , Toast.LENGTH_LONG).show();
     }
    /* else
     {
        Toast.makeText(_context , "Image is not uploaded.." , Toast.LENGTH_LONG).show();
     }*/
        if(pDialog.isShowing())pDialog.dismiss();
       }

    }
Sanket Shah
  • 4,352
  • 3
  • 21
  • 41
1

In your code addding some changes in onPostExecuteMethod

 public class MainTest extends AsyncTask<String, Integer, String> {
    @Override
    protected void onPreExecute() {
        pDialog = new ProgressDialog(DataAcceptActivity.this);
        pDialog.setMessage("Loading..");
        pDialog.setIndeterminate(true);
        pDialog.setCancelable(false);
        pDialog.show();
    }

    @Override
    protected String doInBackground(String... params) {

        postImageData();

        return null;
    }

    @Override
    protected void onPostExecute(String result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);
       if(pDialog.isShowing()){
        // data=jobj.toString();
         pDialog.dismiss();
     }
       Toast toast = Toast.makeText(context, text, duration);
         toast.show();
    }

}
Sanket990
  • 665
  • 1
  • 10
  • 22