简体   繁体   English

进度对话框不会出现-Android

[英]Progress Dialog Box Won't appear - Android

I want to make a Progress Dialog Box in my app to use when sending some information. 我想在我的应用程序中创建一个“进度”对话框,以便在发送某些信息时使用。 But the code I wrote won't work. 但是我写的代码行不通。 It the method send() executes but the dialog box never appears because it dismisses very quickly 它执行send()方法,但对话框不会出现,因为它很快就会消失

Here is my code : 这是我的代码:

    ProgressDialog myProgressDialog = ProgressDialog.show(Tents.this,
                                "Please wait...", "Sending...", true);
    send();
    myProgressDialog.dismiss();
    goFour();

How do I make the Dialog Box Last a little longer? 如何使对话框的使用时间更长一点?

You are probably getting a progress dialog, but having it immediately dismiss as it has nothing to wait for. 您可能会收到一个进度对话框,但由于没有什么可等待的,因此请立即将其关闭。

I'll pretend you want this in OnCreate for my example: 我将在您的示例中假装在OnCreate中使用此功能:

 @Override
 public void onCreate(Bundle savedInstanceState){
     super.onCreate(savedInstanceState);
     setContentView(R.layout.main);

     ProgressDialog pd = ProgressDialog.show(this, "Please wait...", "Sending...");
     new Thread(new Runnable(){
         public void run() {
             send();
             pd.dismiss();
         }
     }).start();

     gofour();
  }

EDIT: If it still goes away immediately, make sure send(); 编辑:如果仍然立即消失,请确保send(); does something that actually takes some time. 做某事实际上需要一些时间。 ;) ;)

First of all - you should not do send() in the same thread as show() and dismiss() - because you are effectively blocking UI thread during sending. 首先-您不应该在与show()和dismiss()相同的线程中执行send()-因为您在发送过程中会有效地阻塞UI线程。 The dialog will actually never show - because in order to show it after show() is called, you need to give the control back to the main looper in UI thread and simply finish handling whatever event you are handling. 该对话框实际上永远不会显示-因为要在调用show()之后显示该对话框,您需要将该控件交还给UI线程中的主循环程序,并只需完成处理您要处理的任何事件即可。 Otherwise the UI thread will never have a chance to draw your dialog. 否则,UI线程将永远没有机会绘制对话框。

The best idea is to start running send() in AsyncTask and call dismiss() in onPostExecute() (see http://developer.android.com/reference/android/os/AsyncTask.html to get idea how to run async task). 最好的想法是开始在AsyncTask中运行send()并在onPostExecute()中调用dismiss()(请参阅http://developer.android.com/reference/android/os/AsyncTask.html来了解如何运行异步任务)。

The UI thread is used to start send() , this will not work and progress dialog will not be shown . UI线程用于启动send(),它将不起作用,并且不会显示进度对话框。 Call send in another thread or AsynTask doBackground and on completion dismiss the dialog. 在另一个线程或AsynTask doBackground中调用send,并在完成时关闭该对话框。

如果您的发送动作完成得如此之快以致于对话框无法正确显示,我建议您改为通过requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS)在活动右上角使用不确定的进度条,然后再使用setProgressBarIndeterminateVisibility(true/false)

"I guess my real question would then be how do I make it so that it lasts a little longer?" “我想我真正的问题是如何使它持续更长的时间?” My answer would be WHY???!!! 我的答案将是为什么?

I think you would be better showing an alert dialog to confirm your send function has completed, it would be annoying for the user having to wait for no reason! 我认为您最好显示一个警报对话框,以确认您的发送功能已完成,这对于用户无缘无故地等待来说会很烦人!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM