0

I need to download a large file . I need to use DownloadManger class for api 9 or higher version . But I wish my download to continue even if User pauses the Activity or presses Back ... or user kills the Activity. Once he starts the downloading process it should stop only after finishing that respective download. If meanwhile network connectivity breaks the downloading should resume from where it stopped,i.e, should not start from the beginning .

4

1 回答 1

0

仅依赖Activity于这个用例并不是一个好主意。有两种选择:

选项1

  1. Service除了你的Activity. 在您的 Activity 中onPause(),启动Service和在您onResume()的 Activity 中,停止Service.
  2. Service本身也应该注册ACTION_DOWNLOAD_COMPLETE广播。
  3. 如果您在 Activity 处于后台(即在您ServiceActivity. 您还应该注册ACTION_NOTIFICATION_CLICKED.

选项 2

  1. 只需创建一个 manifest-registeredBroadcastReceiver来监听ACTION_DOWNLOAD_COMPLETE广播。
  2. onReceive()下载完成后,您可以做任何您想做的事情。同样,如果您的 Activity 已经在前台,您可以直接更新您的 UI。否则,用户单击Notification,您可以在ACTION_NOTIFICATION_CLICKED.
于 2012-07-23T09:16:36.843 回答