Reputation: 5380
I have some specific logic in which there is a need to wait 5 seconds before dismissing spinner and showing dialog message. Everything works but dialog message not shown. If i do the same thing without delay it works. My code is:
public static void showMessageNotSentDialog(Activity inActivity)
{
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(inActivity);
alertDialogBuilder.setTitle(R.string.error);
alertDialogBuilder.setMessage(R.string.error_sending_message);
alertDialogBuilder.setCancelable(false);
alertDialogBuilder.setPositiveButton(R.string.ok,new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}
public static void showDelayedErrorMessage(final Activity inActivity)
{
Runnable task = new Runnable() {
public void run() {
com.test.classes.Spinner.hideSpinner();
showMessageNotSentDialog(inActivity);
}
worker.schedule(task, com.test.classes.Spinner.TEXT_SPINNER_HIDEOUT_SEC, TimeUnit.SECONDS);
}
Trying to show dialog from UI thread:
public static void showDelayedErrorMessage(final Activity inActivity)
{
Runnable task = new Runnable() {
public void run() {
com.test.classes.Spinner.hideSpinner();
Runnable messageTask = new Runnable() {
public void run() {
showMessageNotSentDialog(inActivity);
}
};
inActivity.runOnUiThread(messageTask);
}
};
worker.schedule(task, com.test.classes.Spinner.TEXT_SPINNER_HIDEOUT_SEC, TimeUnit.SECONDS);
}
Upvotes: 1
Views: 121
Reputation: 929
use count down timer and no need to play with threads
new CountDownTimer(5000,5000) {
@Override
public void onTick(long arg0) {
}
@Override
public void onFinish() {
}
}.start();
onfinish executes after 5 seconds
Upvotes: 1
Reputation: 10245
You should use a Handler, which can update the gui thread. Basically you could just post execute on the handler with delay, here is an example:
Handler guiHandler = new Handler();
Runnable showDialog = new Runnable(){
public void run(){
//put here the dialog creation
}
}
postDelayed (showDialog ,5000); // Post for 5 seconds
Note that the creation of the Handler must be within the GUI thread, otherwise it won't work. and remember that the GUI can be manipulated by the main thread only.
Upvotes: 1