【问题标题】:Android progress dialog force showingAndroid 进度对话框强制显示
【发布时间】:2012-08-02 07:23:24
【问题描述】:

我有这行代码:

1)    m_ProgressDialog = ProgressDialog.show(m_Context, "", m_Context.getString(R.string.dictionary_loading));
2)   //important code stuff: interact with db, change some textview values (= 2-3 seconds if i'm unlucky)
3)   m_ProgressDialog.dismiss();

但是发生的情况是阶段 2) 发生在 1) 之前……这是错误的。第一个 UI 冻结然后出现对话框..

阶段 2) 是一些与 DB 交互的代码,可能还会更改一些 textViews..但由于这可能需要一段时间,我决定显示进度对话框,以便用户知道正在发生真正重要的事情。我不能将异步用于这些操作,因为 UI 代码和 db 代码被混淆了,它只会让我的生活复杂化

如何强制对话框根据请求显示??.. 对我来说,显示的代码只是将它添加到“当我有一些空闲时间而我现在没有时间”堆栈时将其添加到“待办事项列表”堆栈中..

【问题讨论】:

    标签: android android-asynctask progressdialog


    【解决方案1】:

    你在 ui 线程上做你的工作。您应该为此使用单独的线程来保持 UI(进度条)响应。看看AsynchTask

    【讨论】:

    【解决方案2】:

    不要将 UiThread 用于后台操作,它会导致屏幕冻结。您必须使用单独的线程,如 Asyc Task。

    像下面那样做

    onCreate()
    {
    dialog.show();
    new DownloadFilesTask().excute()
    }
    class DownloadFilesTask extends AsyncTask<Void,Void,Void>
    {
     protected Long doInBackground(URL... urls) {
            //Background operation
             }
             return null;
         }
    
         protected void onProgressUpdate(Integer... progress) {
    
         }
    
         protected void onPostExecute(Long result) {
          runOnUiThread(new Runnable() {
    
                    @Override
                    public void run() {
                        // TODO Auto-generated method stub
                        //Update you Ui here
    dialog.dismiss();
                    }
                });
         }
    }
    

    【讨论】:

      【解决方案3】:

      在大多数情况下,如果您只想拥有 2 个方法,ShowLoading() 和 HideLoading() 只需使用这个

      public static void ShowLoading()
      {
         HideLoading();
          
         myLoadingThread = new Thread(new ThreadStart(LoadingThread));
         myLoadingThread.Start();
      }
      
      private static void LoadingThread()
      {
         Looper.Prepare();
          
         myProgressDialog = new ProgressDialog(myActivity, 
            Resource.Style.AppTheme_Dialog);
                      
         myProgressDialog.SetMessage("Loading..."); // Or a @string...
      
         myProgressDialog.SetIcon(Resource.Drawable.your_loading_icon);
                      
         myProgressDialog.SetProgressStyle(ProgressDialogStyle.Spinner);
         myProgressDialog.SetCancelable(false);
         myProgressDialog.Show();
          
         Looper.Loop();
      }
          
      public static void HideLoading()
      {
         if (myProgressDialog != null)
         {
            myProgressDialog.Dismiss();
            myProgressDialog = null;
         }
          
         if (myLoadingThread != null)
            myLoadingThread.Abort();
      }
      

      现在我声明并解释我在代码示例中使用的以下变量,其中一个是全局变量,是的,如果你不喜欢使用全局变量,或者你想一次有 2 个加载对话框(wtf ...) 寻找另一种解决方案。这只是最简单的方法,最友好且没有奇怪的代码,其中包含嵌套方法、新类和到处继承这样一个简单的事情:

      private Thread myLoadingThread;
      private ProgressDialog myProgressDialog;
      
      // Some people will hate me for this, but just remember
      // to call myActivity = this; on each OnStart() of your app
      // and end with all your headaches 
      public Activity myActivity;  
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-06-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多