进度对话框未显示

发布于 2024-11-28 09:33:41 字数 1157 浏览 2 评论 0原文

当我的应用程序第一次启动时,它需要进行一些设置。我想在发生这种情况时显示一个进度对话框,这样用户就不会仅仅面临无响应的黑屏。我尝试使用以下代码执行此操作,但对话框从未显示:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.shell);
    final ProgressDialog dialog = ProgressDialog.show(WhereWolfActivity.this, getString(R.string.loadingtitle), getString(R.string.loading), true, false);
    Thread thread = new Thread(new Runnable() {
        @Override
        public void run() {
            if(dbHelper == null)
                dbHelper = new WhereWolfOpenHelper(getApplicationContext());                    
            checkUserDetails();
            String providers = Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
            checkLocationProviders(providers);
            runOnUiThread(new Runnable(){
                @Override
                public void run() {
                    dialog.dismiss();
                }                   
            });     
        }
    });
    thread.start();
}

如果我删除 dialog.dismiss(); 对话框将在线程完成后显示。这表明 UI 线程被阻塞。有什么想法如何解决这个问题吗?谢谢。

When my application first starts it needs to do some set up. I'd like to show a progress dialog while this is happening, so the user isn't just faced with an unresponsive black screen. I am trying to do it with the following code, but the dialog never shows:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.shell);
    final ProgressDialog dialog = ProgressDialog.show(WhereWolfActivity.this, getString(R.string.loadingtitle), getString(R.string.loading), true, false);
    Thread thread = new Thread(new Runnable() {
        @Override
        public void run() {
            if(dbHelper == null)
                dbHelper = new WhereWolfOpenHelper(getApplicationContext());                    
            checkUserDetails();
            String providers = Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
            checkLocationProviders(providers);
            runOnUiThread(new Runnable(){
                @Override
                public void run() {
                    dialog.dismiss();
                }                   
            });     
        }
    });
    thread.start();
}

If I remove dialog.dismiss(); the dialog shows up after the thread is finished. This suggests to me that the UI thread is being blocked. Any ideas how to fix this? Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

心碎无痕… 2024-12-05 09:33:41

请尝试这个并报告回来

ProgressDialog dialog;
Context mContext = this;
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.shell);
    Thread thread = new Thread(new Runnable() {
        @Override
        public void run() {
            runOnUiThread(new Runnable(){
                @Override
                public void run() {
                    dialog = ProgressDialog.show(mContext, getString(R.string.loadingtitle), getString(R.string.loading), true, false);
                }                   
            });
            if(dbHelper == null)
                dbHelper = new WhereWolfOpenHelper(getApplicationContext());                    
            checkUserDetails();
            String providers = Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
            checkLocationProviders(providers);
            runOnUiThread(new Runnable(){
                @Override
                public void run() {
                    dialog.dismiss();
                }                   
            });     
        }
    });
    thread.start();
}

Please try this and report back

ProgressDialog dialog;
Context mContext = this;
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.shell);
    Thread thread = new Thread(new Runnable() {
        @Override
        public void run() {
            runOnUiThread(new Runnable(){
                @Override
                public void run() {
                    dialog = ProgressDialog.show(mContext, getString(R.string.loadingtitle), getString(R.string.loading), true, false);
                }                   
            });
            if(dbHelper == null)
                dbHelper = new WhereWolfOpenHelper(getApplicationContext());                    
            checkUserDetails();
            String providers = Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
            checkLocationProviders(providers);
            runOnUiThread(new Runnable(){
                @Override
                public void run() {
                    dialog.dismiss();
                }                   
            });     
        }
    });
    thread.start();
}
葮薆情 2024-12-05 09:33:41

您的代码在 2.3.3 Samsung Galaxy S i9000 上运行良好,我刚刚检查过(显然没有数据库内容 - 只是 runOnUIThread 之前的 Thread.sleep() )。

由于这个答案不是很有帮助,让我向您指出类 AsyncTask 这是为您当前“手动”执行的任务创建的。

基本上,您创建 AsyncTask 的子类(作为 Activity 的内部类) - 如果您不需要任何参数,只需使用 Void,Void,Void - 并在 onCreate() 中使用 .execute(); 启动它;

在 onPreExecute() 中启动进度对话框,在 doInBackground 中执行后台工作,最后在 onPostExecute() 中关闭对话框。无需摆弄 runOnUIThread。

Your code works fine on a 2.3.3 Samsung Galaxy S i9000, I just checked (without the DB stuff, obviously - just Thread.sleep() before the runOnUIThread).

Since this answer is not very helpful, let me point you to the class AsyncTask which has been created for the task you are currently doing "by hand".

Basically, you create a subclass of AsyncTask (as an inner class of your activity) - if you don't need any parameters, just use Void,Void,Void - and start it in onCreate() with .execute();

Kick off your progress dialog in onPreExecute(), do your background work in doInBackground, and finally dismiss the dialog in onPostExecute(). No fiddling with runOnUIThread.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文