[英]Showing a progressdialog in android while a large method is executed
I have a long running method that is called during onCreate, this method populates textviews so interacts with the UI, and updates maybe 70 labels (about 3-20 seconds depending on device). 我在onCreate期间调用了一个运行时间很长的方法,该方法填充文本视图,以便与UI交互,并可能更新70个标签(大约3-20秒,具体取决于设备)。
I want to display a progressdialog as this method executes. 我想在此方法执行时显示进度对话框。
Ideally I want to fire my method on the UI thread once the Activity has been displayed and the progress is displayed, this I cannot do, the Activity won't paint until the method has finished. 理想情况下,一旦显示了活动并显示了进度,我想在UI线程上触发我的方法,这是我做不到的,在方法完成之前,活动不会绘制。
I hoped to find an event which was fired after the activity was displayed, and I found the one below, but it still leaves the screen black until the method has finished. 我希望找到一个在显示活动之后触发的事件,但我在下面找到了一个事件,但是在方法完成之前,它仍然使屏幕变黑。
@Override public void onPostCreate(Bundle savedInstanceState) @Override public void onPostCreate(Bundle savedInstanceState)
I am normally a WP7 developer, and in .NET you add an event handler for onLoadComplete which is fired after the ui is displayed, but before the user has a chance to interact withthe UI, how do I do this in Android JAVA? 我通常是WP7开发人员,并且在.NET中添加了onLoadComplete的事件处理程序,该事件处理程序在显示ui之后触发,但是在用户有机会与UI进行交互之前,如何在Android JAVA中执行此操作?
Thanks 谢谢
Put a ProgressBar in the View. 在视图中放置一个ProgressBar。
Then in the onCreate() or onResume() method do this: 然后在onCreate()或onResume()方法中执行以下操作:
new Thread() {
public void run() {
yourLargeMethod();
}
}.start();
Now you can do this inside your method to update the progressBar 现在,您可以在方法内部执行此操作以更新progressBar
public void yourLargeMethod() {
// doSomething
...
runOnUiThread(new Runnable() {
public void run() {
// update the progress from the thread
progressBar.setProgress(x); // x is your progress, 0 <= x <= progressBar.getMax()
}
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.