0

When i tried to run the code, only main toast is running. Progress Dialog and other toast message is not running.This program is simple async example for sleeping process.The main issue is that it is not showing the Progressdialog.

Did i need to add xml file(it contain only a textView and a Button). Please help me to solve this.Thank You

package com.example.asyncexample;

import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.Toast;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;


    public class MainActivity extends Activity {


ProgressDialog progressBar;
int prorgessInc = 1;            // incrementing the progress dialog

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button button = (Button) findViewById(R.id.start_button);
    button.setOnClickListener(startTaskListener);
    }


private OnClickListener startTaskListener =  new OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
    Context context = getApplicationContext();

    progressBar = new ProgressDialog(v.getContext());

    BackgroundTask test = new BackgroundTask(); 

        test.execute(context);

        CharSequence text = "Main Thread is Running";
        int duration = Toast.LENGTH_LONG;
        Toast toast = Toast.makeText(context, text, duration);
        toast.show();
    }
};

private class BackgroundTask extends AsyncTask<Context, Integer, String>{
    protected void OnPreExecute() {
        CharSequence msg = "BackgroundTask is Operating";
        progressBar.setCancelable(true);
        progressBar.setMessage(msg);
        progressBar.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        progressBar.setProgress(0);
        progressBar.setMax(100);
        progressBar.show();

    }

    @Override
    protected String doInBackground(Context... params) {
        //BackgroundTask Is Running
        for(int i =0; i<=100; i+=prorgessInc){
            try {Thread.sleep(100);}
        catch (InterruptedException e) { e.printStackTrace();}

            publishProgress(prorgessInc);

            if(isCancelled()) break;
            }

        return getString(R.string.backgndcompld);
    }

    protected void OnProgressUpdate(Integer...values ) {
            //Update Progress bar
        progressBar.incrementProgressBy(prorgessInc);

    }

    protected void PostExecute(String result){
        //Dissmiss progressbar 
        progressBar.dismiss();

        Context context = getApplicationContext();
        int duration  = Toast.LENGTH_LONG;
        Toast toast2 = Toast.makeText(context, result, duration);
        toast2.show();
    }

}
}       
Basil
  • 11

1 Answers1

1

Right, your problem is in your method naming for the AsyncTask.

You have defined:

  • protected void OnPreExecute()
  • protected void OnProgressUpdate()
  • protected void PostExecute()

However the real methods are:

  • protected void onPreExecute()
  • protected void onProgressUpdate()
  • protected void onPostExecute()

Note the difference in case. If you had used the @Override as below your IDE would likely have shown you that.

I have done some cleaning of that and other parts of your code. Take a look:

public class MainActivity
        extends Activity
        implements View.OnClickListener
{

    private Button button;
    private ProgressDialog progressBar;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_example);
        button = (Button) findViewById(R.id.start_button);
        button.setOnClickListener(this);
    }

    @Override
    public void onClick(View v)
    {
        Log.d("click", "start button clicked, starting task");
        BackgroundTask test = new BackgroundTask();
        test.execute();
    }

    private class BackgroundTask
            extends AsyncTask<Void, Integer, String>
    {

        int PROGRESS_INCREMENT = 1;
        int PROGRESS_MAX = 100;
        String DIALOG_MSG = "AsyncTask is Operating";

        @Override
        protected void onPreExecute()
        {
            progressBar = new ProgressDialog(getApplicationContext());
            progressBar.setCancelable(true);
            progressBar.setMessage(DIALOG_MSG);
            progressBar.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
            progressBar.setProgress(0);
            progressBar.setMax(PROGRESS_MAX);
            progressBar.show();
        }

        @Override
        protected String doInBackground(Void... params)
        {
            for (int i = 0; i <= 100; i += PROGRESS_INCREMENT)
            {
                try
                {
                    Thread.sleep(100);
                }
                catch (InterruptedException e)
                {
                    e.printStackTrace();
                }
                publishProgress(PROGRESS_INCREMENT);
                if (this.isCancelled())
                {
                    break;
                }
            }
            return "[some result text]";
        }

        @Override
        protected void onProgressUpdate(Integer... values)
        {
            progressBar.incrementProgressBy(PROGRESS_INCREMENT);
        }

        @Override
        protected void onPostExecute(String result)
        {
            progressBar.dismiss();
            Toast.makeText(getApplicationContext(),
                            result,
                            Toast.LENGTH_LONG)
                            .show();
        }
    }
}

Notable changes:

  • Activity implements View.OnClickListener so you can move your onClick() method to your Activity eliminating the anonymous inner class.
  • Button moved to a class level scope. Defining it in your listener could be error prone.
  • prepare and display your PrgressDialog from the onPreExecute() method. That's what it's for.
  • Cleaned up your Toast. No need to use all those variables and waste space with useless objects.
  • Instead of Toast messages use the built in Log for debug messages. Toast messages can very quickly begin to spam your screen and because of the display lengths are very inaccurate for knowing when things happen.
indivisible
  • 4,892
  • 4
  • 31
  • 50