Android中UI线程操作期间显示ProgressDialog
正如我在另一个问题此处提出的那样,似乎 PackageManager.getInstalledPackages() 没有不能很好地使用线程。正如 CommonsWare 所说此处:
也许需要在主应用程序上调用 PackageManager 线程,由于某种原因
因此将其放在线程上会出现不良行为,在我的活动中进入和退出几次会使显示的应用程序列表有时带有项目,有时为空。让 UI 线程中的所有内容都像梦想一样工作,每次都能正常加载。问题是,用户期望得到某种反馈,而我需要提供反馈。当我开始活动时,屏幕保持黑屏 3-4-5-6 秒(取决于安装的设备和应用程序)。我如何提供某种反馈?我正在考虑一个 ProgressDialog,但我不知道如何启动它。谢谢。
As I've asken on another question HERE it seems that the PackageManager.getInstalledPackages() doesn't play nice with Threading. As CommonsWare stated HERE:
Perhaps the PackageManager needs to be invoked on the main application
thread, for some reason
So having it on a thread gets undesired behavior, entering and exiting a few times in my Activity makes the list of displayed apps sometimes with items, sometimes empty. Having everything in the UI Thread works like a dream, loads fine everytime. The thing is, the user expects some sort of feedback and I need to provide one. As I start the activity, the screen remains black for 3-4-5-6 seconds (depending on the device and apps installed). How can I provide some sort of feedback ? I am thinking of a ProgressDialog but I don't know how can I start it. Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
正如我们所发现的,与调用 PackageManager.getInstalledPackages()(必须在 UI 线程上完成)相比,处理应用程序的循环需要一段时间(可以在单独的线程中完成)。
As discovered, the loop to work through the applications takes awhile (which can be done in a separate thread), compared to the call to PackageManager.getInstalledPackages() (which has to be done on the UI thread).
使用异步执行后台工作并在加载数据时显示指示器。
在你的 onCreate() 中。调用 new AsyncCommonDataFetcher(this).execute();
Use Async to do background work and show indicator while loading data.
in you onCreate(). call
new AsyncCommonDataFetcher(this).execute();
在活动的 onCreate() 中尝试对 ProgressDialog 执行以下操作
,然后在导致延迟的过程结束时将其关闭
Try the following for ProgressDialog in the onCreate() of your activity
And then dismiss it when the process causing the delay is over