Android的 - 替代异步任务? [英] Android - Alternative to async tasks?

查看:165
本文介绍了Android的 - 替代异步任务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我在我使用异步任务使用REST API一个Django后端数据获取的应用程序。当设备需要与现场进行同步,这将在第一次使用。
所以这个同步任务中获取相当多的数据(2 GB)的。我在很多地方异步任务真的时,应使用过程耗时不超过2-3秒已经阅读。一定要带在我的情况更长的时间。


  1. 那么,什么是我的另类吗?处理程序?有人可以指向一个教程或文章,解释如何?

  2. 在同步过程中,我的设备关闭显示屏,以节省电源。因此,该活动被破坏。同步完成后,现在我展示一个对话框,使用AlertDialog级,通知相同的用户。这将导致一个错误:活动已泄漏的窗口。它是一个严重的问题?有什么可以做,以避免这种情况?使用片段显示对话框?


解决方案

使用一个服务。结果
它的意思为长时间运行的任务,而不是依赖于你的活动周期。结果
这将是对用户特别有用,如果你也是一个通知的下载(尤其是在移动连接相关联,显示了进步为2GB可能需要大量的时间去取那说起 - 请不要获取数据的2GB没有使它真正清楚,你要做的用户或使他们能​​够选择退出这个移动连接或做到这一点,只有当连接通过WiFi数据有限的用户会感谢你的。 )结果
这里有一个关于服务教程

或者你也可以让操作系统采取所有这一切的保养和使用下载管理器来获取文件。它负责设备重新启动的,连接问题等结果
Sample项目,的教程

So I have an app in which I use an async task to fetch in data from a Django backend using rest API. This will be used the first time when the device needs to be synced with the site. So this sync task fetches in quite a lot of data (2 GB). And I have read in many places that async tasks really should be used when the process takes not more than 2-3 seconds. Definitely gonna take longer in my case.

  1. So what is my alternative here? Handlers? Can someone point to a tutorial or article explaining how to?
  2. During the sync process, my device turns off the display to save power. Hence the activity gets destroyed. Now after the sync is complete I show a dialog box, using "AlertDialog" class, informing the user of the same. This causes an error: Activity has leaked a window. Is it a serious issue? What can be done to avoid that? Use fragments to show dialog boxes?

解决方案

Use a Service.
It's meant for long-running tasks and is not tied to your Activity lifecycle.
It would be especially helpful for the user if you'd also associate a notification with the download, showing the progress (as 2GB can take a huge amount of time to fetch, especially on a mobile connection. Speaking of that - please don't fetch 2GB of data on a mobile connection without making it really clear to the user that you're going to do that or enabling them to opt-out of this or do it only when connected over WiFi. Data-limited users will thank you ;)
Here's a tutorial about services

Or you can let the OS take care of all of this and use DownloadManager to fetch the file. It takes care of device restarts, connection issues etc.
Sample project, Tutorial

这篇关于Android的 - 替代异步任务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆