我怎样才能显示比从UI线程的线程上一个对话框 [英] How can I show a Dialog box from a thread other than the UI thread

查看:162
本文介绍了我怎样才能显示比从UI线程的线程上一个对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用需要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 the Activity) and then pass it to your thread. Now from the thread you call handler.post(runnable) to enqueue code to be executed on the UIThread.

  • Define a BroadcastReceiver in your Activity and from you thread send an Intent with the necessary information in the Bundle

  • Use an AsyncTask and the methods publishProgress(), onProgressUpdate() or onPostExecute() to inform the Activity of the progress or when the taask has finished

  • Use runOnUiThread.

It depends on your needs. For short-running asynchronous operations, AsyncTask is a good choice.

这篇关于我怎样才能显示比从UI线程的线程上一个对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆