如何从通知意图重新连接到 Android 上正在运行的 AsyncTask?
我正在通过 AsyncTask 在我的应用程序中下载文件。下载正在进行时,我在通知栏中创建通知并在我的活动上显示进度栏。
现在,如果用户存在我的应用程序,下载可能仍在进行中。通知栏的意图允许用户返回我的应用程序,但如果发生这种情况,我的应用程序将显示为其初始状态。
如何返回到用户离开之前我的应用程序的状态?我的 AsyncTask 更新 ProgressBar 了吗?
非常感谢!
丹尼斯
I'm downloading files in my app through an AsyncTask. While a download is in progress, I create a notification in the Notification Bar and display a ProgressBar on my Activity.
Now, if the user exists my app the download might be still in progress. The intent from the Notification Bar allows the user to return to my app, but if that happens my app is shown in its initial state.
How can I return to the state my app had before the user left? With my AsyncTask updating the ProgressBar?
Thanks a lot!
Dennis
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要下载/上传文件,您应该使用 IntentService。它的作用是继续运行,直到操作完成,然后自行完成。更好的是,您可以使用 WakefulIntentService。
现在,当您的下载正在进行时,您可以将进度存储在您的服务中,并且当点击它时会触发一个 Intent,该 Intent 将重新创建 Activity,并将 ProgressBar 初始化为当前下载进度。您必须设置正确的 Intent flag 以免如果此活动已存在,则重新创建该活动。
更好的是,您可以使用自定义视图在通知栏本身中显示进度。取决于您想要什么行为。
For downloading/uploading files you should use an IntentService. What it does is that it keeps on running until the operation is complete and then finishes itself. Better still you can use WakefulIntentService.
Now while your download is in progress you can keep the progress stored in your service and when its tapped fire an Intent which will re-create the activity with the ProgressBar initialized to the current download progress. You will have to set the proper Intent flag so as not to re-create this activity if it's already present.
Better still, you can show the progress in your notification bar itself using a custom view. Depends on what behavior you want.