后台任务中的活动或服务?

发布于 2024-10-08 08:38:54 字数 423 浏览 0 评论 0原文

这是我的设置;我有一个新闻应用程序。这个应用程序从本地存储加载一些列表视图(我为此滥用共享首选项,但如果这是一个好的做法将是另一个问题)。我希望能够顺利更新这个本地存储。

我的第一个方法是让一个按钮触发一个服务,这成功了。但我也希望该服务在活动完成后刷新该活动。我没能做到这一点。 我目前的方法似乎有效,但在 3G 上不太顺利。活动 A 显示“正在加载”框,然后启动活动 B 并自行销毁。 Activity B 还显示一个加载框,进行下载,然后销毁自身并使用新内容重新启动 Activity A。 然而,最后一种方法会使在 3G 模式下从 A 过渡到 B 时屏幕变黑几秒钟。

为了视觉流畅,我希望连续显示一个加载弹出窗口。完成后,应该刷新该活动。

有谁知道我怎样才能实现这一目标?

我希望我的解释足够清楚。否则,我很乐意与您分享一些代码。

预先非常感谢

Here's my set-up; I have a news app. This app loads a few listviews from local storage (I'm abusing sharedpreferences for that, but if that's good practice would be another question). I want to be able to update this local storage smoothly.

My first approach was to make a button trigger a service, which did the trick. But I also wanted the service to refresh the activity when it was done. I wasn't able to do that.
My current approach seems to work, but not as smoothly on 3G. Activity A shows a 'Loading' box, then launches activity B and destroys itself. Activity B also shows a loading box, does the downloading, then destroys itself and relaunches activity A with the new content.
However, this last approach makes the screen go black for a few seconds in transition from A to B when on 3G.

For the sake of visual smoothness, I'd like one loading popup to display continuously. When done, the activity should be refreshed.

Does anyone know how I can achieve this?

I hope my explanation was clear enough. Otherwise, I'd be happy to share some code with you.

Thanks a lot in advance

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

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

发布评论

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

评论(1

一向肩并 2024-10-15 08:38:54

查看您在评论中链接到的代码,您在活动的 onCreate 方法中做了太多工作。 onCreate 应保持轻量级,以便尽快向用户显示 UI。您可能希望将大部分 Activity 设置移至 onResume 方法。此外,任何阻塞 UI 线程的事情(比如获取和解析数据)都应该在线程上完成。看看使用 AsyncTask (http://developer.android .com/reference/android/os/AsyncTask.html)在您的应用程序中用于此目的。

Looking at the code you linked to in your comment, you are doing too much work in the onCreate method of your activity. onCreate should be kept lightweight in order to display UI to the user as quick as possible. You probably want to move the bulk of your Activity setup to the onResume method. Furthermore, anything that blocks the UI thread (like fetching and parsing data) should be done on a thread. Look at using AsyncTask (http://developer.android.com/reference/android/os/AsyncTask.html) for this purpose in your application.

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