How To Show Alert Dialog In A Running Thread?
I'm developing an Android Game.In this game, There are tracks on which trains run. This is running thread. I want to show an alert dialog when there is a collision between. when I'
Solution 1:
This will help you:
runOnUiThread(newRunnable() {
@Overridepublicvoidrun() {
// Your dialog code.
}
});
Solution 2:
You must need to create AlertDialog
inside UI thread else it will never work. If you are in different thread use MessageHandler
or can use runOnUiThread
(using runnable) to create your dialog inside.
Solution 3:
You can use Handlers to do this work.
HandlermHandler=newHandler(Looper.getMainLooper());
mHandler.post(newRunnable() {
@Overridepublicvoidrun() {
// Your UI updates here
}
});
Solution 4:
You can create an handler in Activity Class, and can invoke sendMessage to that handler object. Write code to display alert in handleMessage method of Handler, for Example:
Activity Class
Handler mHandler = newHandler()
{
publicvoidhandleMessage(Message msg)
{
//Display Alert
}
};
//ThreadThread thread= newThread()
{
publicvoidrun()
{
//Logic
MHandler.sendEmptyMessage(0);
}
}
Solution 5:
You have to show your dialog on UI thread like below
runOnUiThread(newRunnable() {
@Overridepublicvoidrun() {
// Your dialog code.
}
});
Post a Comment for "How To Show Alert Dialog In A Running Thread?"