Android 安卓进程对话框强制显示

Android 安卓进程对话框强制显示,android,android-asynctask,progressdialog,Android,Android Asynctask,Progressdialog,我有以下几行代码: 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.d

我有以下几行代码:

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交互的代码,也可能会更改一些文本视图。但由于这可能需要一段时间,我决定显示进度对话框,以便用户知道真正重要的事情正在发生。我不能使用异步进行这些操作,因为UI代码和db代码是菜单化的,这只会使我的生活复杂化


如何根据请求强制显示对话框??。。对我来说,显示的代码只是将其添加到“当我有空闲时间&我现在没有时间时”堆栈中。

您正在ui线程上执行您的工作。您应该为此使用单独的线程来保持UI(进度条)的响应性。请看。

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

你喜欢下面吗

onCreate()
{
dialog.show();
新建下载文件任务().excute()
}
类DownloadFilesTask扩展了AsyncTask
{
受保护的长doInBackground(URL…URL){
//后台操作
}
返回null;
}
受保护的void onProgressUpdate(整数…进度){
}
受保护的void onPostExecute(长结果){
runOnUiThread(新的Runnable(){
@凌驾
公开募捐{
//TODO自动生成的方法存根
//在这里更新您的Ui
dialog.dismise();
}
});
}
}

那么我建议先进行重构。这篇文章可能也很有意思:你可以使用AsyncTask,你需要“让你的生活复杂化”,让一些东西功能齐全,不要那么懒@pulancheck1988
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();
                }
            });
     }
}