Show Dialog After Thread is Finish
I want show dialog after finish Thread. In thread I am changing TextView's Value like as 0 to 100... When TextView Value is reach 100 then i want to show dialog..
What i do for it. Thanks in advance... Code Snippet:
final Thread thread = new Thread(new Runnable()
{
@Override
public void run()
{
synchronized (this)
{
try
{
for(int i=0 ; i<speed; i++)
{
final int value=i+1;
wait(3000/speed);
Test.this.runOnUiThread(new Runnable() {@Override public void run()
{
accText.setText(String.valueOf(value));
}});
}
}
catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
});
thread.start();
this is asyntask code snippet...
class setTextBackgroundTask extends AsyncTask<String , Integer, Void>
{
@Override
protected vo开发者_如何学编程id onPreExecute()
{
}
@Override
protected Void doInBackground(String... params)
{
Thread th = new Thread();
int value;
for(int i=0 ; i<speed; i++)
{
value=i+1;
publishProgress(value);
try {
th.sleep(3000/speed);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return null;
}
@Override
protected void onProgressUpdate(Integer... values)
{
accText.setText(String.valueOf(values[0]));
System.out.println("Value=="+values[0]);
super.onProgressUpdate(values);
}
@Override
protected void onPostExecute(Void result)
{
showShareDialog();
}
}
This document explains how you can create dialogs.
Please note that you will have to do any dialog creation code on the UI thread.
You already have code that runs something on the UI thread, just do that outside the loop,but with the dialog creation code inside.
You should use AsyncTask: subclass AsyncTask
, override doInBackground()
to execute your time consuming action on another thread, and then override onPostExecute()
to show your dialog.
Note that you cannot change UI elements from a non-UI (background) thread. AsyncTask
takes care of that for you: it calls doInBackground()
on a new thread and then calls onPostExecute()
on the UI thread as soon as the background task is complete.
精彩评论