Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid 2.2: ProgressDialog Freezing In Second Thread
    primarykey
    data
    text
    <p>I have recently experimented with creating an easy way to open a ProgressDialog up in a second thread, so if the main thread freezes the dialog will keep working.</p> <p>Here is the class: public class ProgressDialogThread extends Thread { public Looper ThreadLooper; public Handler mHandler;</p> <p>public ProgressDialog ThreadDialog; public Context DialogContext; public String DialogTitle; public String DialogMessage;</p> <pre><code>public ProgressDialogThread(Context mContext, String mTitle, String mMessage) { DialogContext = mContext; DialogTitle = mTitle; DialogMessage = mMessage; } public void run() { Looper.prepare(); ThreadLooper = Looper.myLooper(); ThreadDialog = new ProgressDialog(DialogContext); ThreadDialog.setTitle(DialogTitle); ThreadDialog.setMessage(DialogMessage); ThreadDialog.show(); mHandler = new Handler(); Looper.loop(); } public void Update(final String mTitle, final String mMessage) { while(mHandler == null) synchronized(this) { try { wait(10); } catch (InterruptedException e) { Log.d("Exception(ProgressDialogThread.Update)", e.getMessage() == null ? "MISSING MESSAGE" : e.getMessage()); } } mHandler.post(new Runnable(){ @Override public void run() { ThreadDialog.setTitle(mTitle); ThreadDialog.setMessage(mMessage); }}); } public void Dismiss() { while(ThreadDialog == null || mHandler == null) synchronized(this) { try { wait(10); } catch (InterruptedException e) { Log.d("Exception(ProgressDialogThread.Dismiss)", e.getMessage() == null ? "MISSING MESSAGE" : e.getMessage()); } } mHandler.post(new Runnable(){ @Override public void run() { ThreadDialog.dismiss(); }}); } public void Continue() { while(ThreadLooper == null || mHandler == null) synchronized(this) { try { wait(10); } catch (InterruptedException e) { Log.d("Exception(ProgressDialogThread.Continue)", e.getMessage() == null ? "MISSING MESSAGE" : e.getMessage()); } } mHandler.post(new Runnable(){ @Override public void run() { ThreadLooper.quit(); }}); } </code></pre> <p>However it sometimes work perfectly but other times the application simply freezes and crashes eventually.</p> <p>Here is an example of use:</p> <pre><code>ProgressDialogThread thread = new ProgressDialogThread(this, "Loading", "Please wait..."); thread.start(); // Do Stuff thread.Dismiss(); thread.Continue(); </code></pre> <p>It generates a lot of warning and even some crashes sometimes:</p> <p>eg. Handler: Sending message to dead thread....</p> <p>and exceptions like ANR in ...... Reason: keyDispatchingTimedOut</p> <p>Thanks for any help, Alex.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload