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 !!

有帮助吗?

解决方案

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.

其他提示

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

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

        }

on the your asynctask

@Override
protected void onPostExecute() {



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

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

    }

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

}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top