Faced a rather incomprehensible problem when testing the work of the ProgressDialog. The idea of an example is that in a separate stream the value of the dialogue of progress increases. When ProgressDialog is called when the button is pressed, after its completion, Toast appears, which, despite the short display time, does not disappear at all. Restarting the dialog results in almost instantaneous execution and termination. Moreover, when exiting the application, the error "An unexpected application stop has occurred ... Please try again." How to resolve the situation?
Code overloaded thread 'a
package com.samples.progressdialog; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.util.Log; public class SecondThread extends Thread{ Handler mHandler; final static int STATE_DONE = 0; final static int STATE_RUNNING = 0; int mState; int mTotal; SecondThread (Handler hnd) { mHandler = hnd; } public void run() { mState = STATE_RUNNING; mTotal = 0; while (mState == STATE_RUNNING) { try { Thread.sleep(100); } catch (InterruptedException e) { Log.e("ERROR", "THREAD INTERRUPTED"); } Message msg = mHandler.obtainMessage(); Bundle b = new Bundle(); b.putInt("Total", mTotal); msg.setData(b); mHandler.sendMessage(msg); mTotal++; } } public void setState (int state) { mState = state; } }
Main file code
package com.samples.progressdialog; import android.app.Activity; import android.app.ProgressDialog; import android.app.Dialog; import android.view.View; import android.view.View.OnClickListener; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.widget.Button; import android.widget.Toast; public class ProgressDialogActivity extends Activity { /** Called when the activity is first created. */ static final int IDD_PROGRESS = 0; private SecondThread mSecondThread; private ProgressDialog mProgressDialog; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); final Button callButton = (Button)findViewById(R.id.button); callButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub showDialog(IDD_PROGRESS); } }); } protected Dialog onCreateDialog(int id) { switch(id) { case IDD_PROGRESS : mProgressDialog = new ProgressDialog(ProgressDialogActivity.this); mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); mProgressDialog.setMessage("Идёт загрузка чего-то там ... Пожалуйста ждите "); mSecondThread = new SecondThread(handler); mSecondThread.start(); return mProgressDialog; default: return null; } } final Handler handler = new Handler() { public void handleMessage(Message msg){ int total = msg.getData().getInt("Total"); mProgressDialog.setProgress(total); if (total >=100) { dismissDialog(IDD_PROGRESS); mSecondThread.setState(SecondThread.STATE_DONE); Toast.makeText(getApplicationContext(), "Task is finished", Toast.LENGTH_SHORT).show(); } } }; }