Cancel ProgressDialog and stop the thread.

I have a thread that starts several operations once, and I would like to stop it when the user cancels ProgressDialog .

 public void run() { //operation 1 //operation 2 //operation 3 //operation 4 } 

This thread only works once, so I cannot implement a loop to check if it continues the thread.

Here is my ProgressDialog:

 //Wait dialog m_dlgWaiting = ProgressDialog.show(m_ctxContext, m_ctxContext.getText(R.string.app_name), m_ctxContext.getText(R.string.msg_dlg_analyse_pic), true, //indeterminate true, new OnCancelListener() { @Override public void onCancel(DialogInterface dialog) { m_bRunning = false; } }); 

How do I not know how to stop the thread, would the flow of the loop through the loop be correct to make sure it still works, or is there a better way?

 public void run() { int op = 0; while(m_bRunning) { switch(op) { case 0 : //operation 1 break; case 1 : //operation 2 break; case 2 : //operation 3 break; case 3 : //operation 4 break; } op++; } } 

Even with this solution, if there are too many operations in the stream, it can be difficult to complete the sequence of operations. Is there a better way to achieve this?

+4
source share
2 answers

Use callbacks or AsyncTask

http://developer.android.com/reference/android/os/AsyncTask.html

 final AsyncTask<Void, Void, Void> task = new AsyncTask<Void, Void, Void>() { private ProgressDialog dialog; @Override protected void onPreExecute() { this.dialog = new ProgressDialog(context); this.dialog.setMessage("Loading..."); this.dialog.setCancelable(true); this.dialog.setOnCancelListener(new DialogInterface.OnCancelListener() { @Override public void onCancel(DialogInterface dialog) { // cancel AsyncTask cancel(false); } }); this.dialog.show(); } @Override protected Void doInBackground(Void... params) { // do your stuff return null; } @Override protected void onPostExecute(Void result) { //called on ui thread if (this.dialog != null) { this.dialog.dismiss(); } } @Override protected void onCancelled() { //called on ui thread if (this.dialog != null) { this.dialog.dismiss(); } } }; task.execute(); 
+9
source

You can use Asynctask as below

 FetchRSSFeeds fetchRss = new FetchRSSFeeds() fetchRss.execute(); private class FetchRSSFeeds extends AsyncTask<String, Void, Boolean> { private ProgressDialog dialog = new ProgressDialog(HomeActivity.this); /** progress dialog to show user that the backup is processing. */ /** application context. */ protected void onPreExecute() { this.dialog.setMessage(getResources().getString( R.string.Loading_String)); this.dialog.show(); } protected Boolean doInBackground(final String... args) { try { /** * Write your RUN method code here */ if (isCancelled()) { if (dialog.isShowing()) { dialog.dismiss(); } } return true; } catch (Exception e) { Log.e("tag", "error", e); return false; } } @Override protected void onPostExecute(final Boolean success) { } } 

And if you want to cancel the background process, follow below

 if (fetchRss.getStatus() == AsyncTask.Status.RUNNING) { fetchRss.cancel(true); } 
+1
source

All Articles