I need to write static method that shows ProgressDialog, runs worker in thread and after worker finished ProgreesDialog must dismiss and my method return what worker did. This is my code:
public static TOut execute(final Context c, final String title, final String message, final IProgressWorker worker, final TIn param)
{
final ValueHolder result = new ValueHolder();
final ProgressDialog progress=new ProgressDialog(c,R.layout.progress_impl_layout);
Thread main = new Thread(new Runnable(){
@Override
public void run() {
Looper.prepare();//creating new Looper to show progressDialog immediately
final Looper looper=Looper.myLooper();
AsyncTask async = new AsyncTask(){
@Override
protected void onPostExecute(Boolean result) {
super.onPostExecute(result);
isDone=true;
looper.quit();//quit Looper.loop
progress.dismiss();
}
@Override
protected void onPreExecute() {
super.onPreExecute();
progress.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progress.setTitle(title);
progress.setMessage(message);
progress.show();
}
@Override
protected Boolean doInBackground(Integer... params) {
IProgressImpl progressImpl=new MyProgressImpl(progress);
worker.execute(progressImpl, param);
return true;
}
};
async.execute(0);
if(!isDone){
Looper.loop();//show progressDialog immediately
}
}
});
main.start();
try {
main.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result.value;
}
Everything works, but ProgressDialog does not dismissing. I suspect that looper.quit() do something bad, but I need to stop Looper.loop(). I call loop() because i need to show ProgressDialog immediately.