进度对话框未显示
当我的应用程序第一次启动时,它需要进行一些设置。我想在发生这种情况时显示一个进度对话框,这样用户就不会仅仅面临无响应的黑屏。我尝试使用以下代码执行此操作,但对话框从未显示:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
请尝试这个并报告回来
Please try this and report back
您的代码在 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.