Activity 的 onCreate() 上的长操作
我有一个活动,它从远程服务器检索信息并将其显示在 TableLayout 中。 从服务器检索信息的函数有自己的超时时间,超过超时时间就会抛出异常。
现在,当加载活动时,我希望触发该函数,并在该函数工作时显示一个进度对话框,并在该函数完成工作或抛出超时异常时隐藏该进度对话框。
问题:我已将执行上述所有功能的代码放在 onCreate() 函数中。模拟器屏幕上没有显示任何内容,因为 onCreate() 函数尚未完成运行...
我也尝试将代码放入 onStart() 函数...相同的不需要的结果...
我正在尝试避免使用线程,因为该功能需要许多线程无法访问它们的变量...
我怎样才能实现想要的行为?
谢谢。
I have an Activity that retrieves information from a remote server and displays it in a TableLayout.
The function that retrieves the information from the server has its own timeout, and exception is thrown when the timeout gets exceeded.
Now, when the activity is loaded, I want the function to be fired, and a progressDialog to be shown while the function works, and to be hided if the function is done working, or if a timeout exception was thrown.
The problem: I've put the code that do all the functionality described above in the onCreate() function. Nothing is shown on the emulator screen, since the onCreate() function hasn't finished running...
I've also tried to put the code in the onStart() function... same unwanted results...
I'm trying to avoid using of threads, because the functionality needs many variables that the thread will not has access to them...
How can i implement the wanted behavior??
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将 AsyncTask 与 ProgressDialog 边界:
http://it-projects.spb.ru/?p=150&lang=zh
Use AsyncTask with ProgressDialog bounded:
http://it-projects.spb.ru/?p=150&lang=en
onFinished(params...)
)Handler
并获取处理程序以更新onFinished( params...)
onCreate
中创建一个线程并在那里启动它来调用您的 Runnable。Runnable
and put all your load logic in there. Call a function in the activity when finished (lets sayonFinished(params...)
)Handler
and get the handler to update UI inonFinished(params...)
onCreate
and start it there to call your Runnable.