简体   繁体   English

Android 进度对话框强制显示

[英]Android progress dialog force showing

I have this lines of code:我有这几行代码:

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();

But what happens is that phase 2) happens before 1).. which is wrong.但是发生的情况是阶段 2) 发生在 1).. 之前,这是错误的。 First UI freezes then dialog appears..第一个 UI 冻结然后出现对话框..

phase 2) is some code that interacts with DB, might also change some textViews..but since this might take a while i decided to show that progress dialog so that user would know that really important stuff is going on.阶段 2) 是一些与 DB 交互的代码,也可能会更改一些 textViews..但是由于这可能需要一段时间,我决定显示进度对话框,以便用户知道真正重要的事情正在发生。 I cant use Async for these operations since UI code & db code is mengled, it will only complicate my life由于 UI 代码和 db 代码混杂,我无法将 Async 用于这些操作,它只会使我的生活复杂化

How can i force dialog to show at request ??.. to me it seams that code presented just adds it in a "To do list when i have some free time & i dont have time now" stack..我如何强制对话框在请求时显示??.. 对我来说,它显示的代码只是将它添加到“当我有空闲时间而我现在没有时间时的待办事项列表”堆栈中..

You are doing your work on the ui thread.您正在 ui 线程上进行工作。 You should use a separate thread for this to keep the UI (progress bar) responsive.您应该为此使用一个单独的线程来保持 UI(进度条)响应。 Have a look at AsynchTask .看看AsynchTask

Do not use UiThread for background operations it lead to freeze of screen.You have to use separate thread like Asyc Task.不要将 UiThread 用于后台操作,它会导致屏幕冻结。您必须使用单独的线程,如 Asyc Task。

do like below喜欢下面

in

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();
                }
            });
     }
}

For most of cases if you want to simply have 2 methods, ShowLoading() and HideLoading() just use this在大多数情况下,如果您只想拥有 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();
}

Now I declare and explain the followings variables I used on my code sample, one of them is global, yes, if you don't like to use global vars, or you want to have 2 loading dialogs at a time (wtf...) look for another solution.现在我声明并解释我在代码示例中使用的以下变量,其中一个是全局变量,是的,如果您不喜欢使用全局变量,或者您希望一次有 2 个加载对话框(wtf... ) 寻找另一种解决方案。 This is just the simplest way, most friendly and free of weird code with nested methods, new classes and inheritance everywhere for such a simple thing:对于这么简单的事情,这只是最简单、最友好且没有带有嵌套方法、新类和继承的奇怪代码的最简单方法:

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;  

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

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