我怎样才能显示比从UI线程的线程上一个对话框 [英] How can I show a Dialog box from a thread other than the UI thread
问题描述
我的应用需要GPS的阅读,所以在主线程我开始一个线程,读取的GPS,但我无法显示一个对话框,上面写着请等待。我也绑用处理器,但也不能工作。什么是最好的,从第二个线程控制请稍候对话框?谢谢!
公共无效showWaitDialog(){ prgDialog =新ProgressDialog(背景);
prgDialog.setTitle(请稍等。);
prgDialog.setMessage(请稍等。);
prgDialog.setCancelable(假);
prgDialog.show();
}
您可以:
-
在您的UI线程定义一个
处理程序
(例如在活动
),然后把它传递给你的线程。现在,从你调用线程handler.post(可运行)
来排队code要在UIThread执行。 -
定义
广播接收器
在活动
和您线程发送意图
在必要的信息捆绑
-
使用的
的AsyncTask
和方法publishProgress
(),onProgressUpdate( )
或onPostExecute()
通知活动
的进展或当taask有完成 -
使用
runOnUiThread
。
这要看你的需求。对于短期运行的异步操作,的AsyncTask
是一个不错的选择。
My app requires gps reading, so on the main thread I start a Thread that reads the GPS but I'm having trouble showing a dialog that says "Please wait". I tied also using a Handler, but that didn't work either. What's the best to control the "Please wait" dialog from the 2nd Thread? Thanks!
public void showWaitDialog() {
prgDialog = new ProgressDialog(context);
prgDialog.setTitle("Please wait.");
prgDialog.setMessage("Please wait.");
prgDialog.setCancelable(false);
prgDialog.show();
}
You can:
Define an
Handler
in your UI Thread (e.g. in theActivity
) and then pass it to your thread. Now from the thread you callhandler.post(runnable)
to enqueue code to be executed on the UIThread.Define a
BroadcastReceiver
in yourActivity
and from you thread send anIntent
with the necessary information in theBundle
Use an
AsyncTask
and the methodspublishProgress
(),onProgressUpdate()
oronPostExecute()
to inform theActivity
of the progress or when the taask has finishedUse
runOnUiThread
.
It depends on your needs. For short-running asynchronous operations, AsyncTask
is a good choice.
这篇关于我怎样才能显示比从UI线程的线程上一个对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!