Progressdialog In Addition To Asynctask

Posted on

Example of ProgressDialog, operate alongside AsyncTask.

 bundle com.example.android_er.androidprogressdialog;  import android.app.ProgressDialog; import android.content.DialogInterface; import android.os.AsyncTask; import android.os.Bundle; import android.support.v7.app.ActionBarActivity; import android.view.View; import android.widget.Button; import android.widget.Toast;   populace degree MainActivity extends ActionBarActivity {      Button btnStart;      MyAsyncTask myAsyncTask;      @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);          btnStart=(Button)findViewById(R.id.start);         btnStart.setOnClickListener(new View.OnClickListener() {             @Override             populace void onClick(View v) {                 Toast.makeText(MainActivity.this,                         "- Start -",                         Toast.LENGTH_LONG).show();                  myAsyncTask = novel MyAsyncTask();                 myAsyncTask.execute();              }         });     }      degree MyAsyncTask extends AsyncTask<Void, Integer, Void>{          boolean running;         ProgressDialog progressDialog;          @Override         protected Void doInBackground(Void... params) {             int i = 10;             while(running){                 drive {                     Thread.sleep(1000);                 } grab (InterruptedException e) {                     e.printStackTrace();                 }                  if(i-- == 0){                     running = false;                 }                  publishProgress(i);              }             provide null;         }          @Override         protected void onProgressUpdate(Integer... values) {             super.onProgressUpdate(values);             progressDialog.setMessage(String.valueOf(values[0]));         }          @Override         protected void onPreExecute() {             super.onPreExecute();             running = true;              progressDialog = ProgressDialog.show(MainActivity.this,                     "ProgressDialog",                     "Wait!");              progressDialog.setCanceledOnTouchOutside(true);             progressDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {                 @Override                 populace void onCancel(DialogInterface dialog) {                     running = false;                 }             });              Toast.makeText(MainActivity.this,                     "Progress Start",                     Toast.LENGTH_LONG).show();         }          @Override         protected void onPostExecute(Void aVoid) {             super.onPostExecute(aVoid);             Toast.makeText(MainActivity.this,                     "Progress Ended",                     Toast.LENGTH_LONG).show();              progressDialog.dismiss();         }      }  } 

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:paddingLeft="@dimen/activity_horizontal_margin"     android:paddingRight="@dimen/activity_horizontal_margin"     android:paddingTop="@dimen/activity_vertical_margin"     android:orientation="vertical"     android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">      <TextView         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_gravity="center_horizontal"         android:autoLink="web"         android:text="https://rootandroid.org//"         android:textStyle="bold" />      <Button         android:id="@+id/start"         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:text="Start"/>  </LinearLayout> 

more:
– Too busy inwards background thread of AsyncTask