一台装载机适用于所有活动

发布于 2024-12-06 21:11:04 字数 186 浏览 1 评论 0原文

我正在构建一个 Android 应用程序。它解析存储在数据库中的提要。应用程序的每个活动都可以从数据库获取这些数据。

每个Activity也可以调用Service,并使其刷新数据。完成后我想显示一个加载程序。下载服务时,用户仍然可以自由地在活动之间导航。我的问题是;如何显示在用户导航到的所有活动中运行的加载程序?

多谢 :)

Im building an Android application. It parses a feed which is stored in a DB. Each activity of the app is able to get this data from the DB.

Every activity can also call the Service, and make it refresh the data. When this is done I would like to display a loader. While the Service is downloading, the user is still free to navigate between activities. My question is; how can I display a loader that runs in all activities the user navigates to?

Thanks a lot :)

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

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

发布评论

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

评论(2

时光沙漏 2024-12-13 21:11:05

您可以尝试以下操作:

  • 在所有活动中放置“全局加载程序视图”。
  • 当您启动/返回活动时,触发一个 AsyncTask,该任务将用于处理全局加载器的更新
  • 覆盖 onPreExecute() 以准备全局加载器(例如,如果服务已经下载了一段时间)
  • 覆盖onProgressUpdate()并使用它通过询问服务当前状态(负载百分比或其他)来更新全局加载器的状态
  • In doInBackground 你实现:

    while( service.isLoading() ) {
      发布进度(service.getLoadPercentage())
      // 也许在这里添加一个 Thread.sleep() 。
    }
    
  • 重写onPostExecute以将全局加载器的可见性设置为View.GONE。

如果用户在加载尚未完成时退出 Activity,您可能需要实现一种终止 AsyncTask 的方法。

You could try something like:

  • Place a "global loader view" in all your activities.
  • When you start/return to an activity, fire an AsyncTask which will be used to handle updates of the global loader
  • Override onPreExecute() to prepare the global loader (e.g. setting its state to the current level if the service has been downloading for a while)
  • Override onProgressUpdate() and use it to update the state of the global loader by asking the Service for the current state (load percentage or something)
  • In doInBackground you implement:

    while( service.isLoading() ) {
      publishProgress( service.getLoadPercentage() )
      // Maybe add a Thread.sleep() here.
    }
    
  • Override onPostExecute to set the visibility of the global loader to View.GONE.

You might need to implement a way of killing the AsyncTask if the user switches away from the activity when the loading is not finished yet.

与风相奔跑 2024-12-13 21:11:04

这就是我会尝试做的事情,因为这对我来说似乎是一项不常见的任务:

我会尝试设置一个半透明的 PopupWindow 其中包含进度指示器。每当需要指示加载进度时,我都会触发/关闭此弹出窗口以显示/或不显示...

Here's what I would try to do since it seems to be an uncommon task to me:

I would try to setup a translucent PopupWindow which contains a progress indicator. I would trigger / dismiss this popup to be displayed / or not whenever there's a need to indicate the loading progress...

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