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 .

Abhilasha
  • 929
  • 1
  • 17
  • 37
Vipin Sahu
  • 1,441
  • 1
  • 18
  • 29
  • What is your question? What have you tried and what problems have you come across? Some code please? – curioustechizen Jul 23 '12 at 08:48
  • see the file is being downloaded while the i have register an intent for action download completed if i pause the activity by press any event key then I need to unregistered the receiver in onpause and reregister it on resume if mean while my download take place when n where i have unregistered the receiver how do I came to know it is completed – Vipin Sahu Jul 23 '12 at 08:59
  • Ah, I understand the question now. Detailed answer below. – curioustechizen Jul 23 '12 at 09:08
  • Patience my friend. It takes time to type out the answers :) – curioustechizen Jul 23 '12 at 09:17
  • dear @curioustechizen I unfortunately down voted your answer please reedit your answer so that i can upvote my your answer ......... – Vipin Sahu Jul 23 '12 at 11:34

1 Answers1

0

It is not a good idea to depend only on an Activity for this use case. There are two options:

Option 1

  1. Use a Service in addition to your Activity. In your onPause(), start the Service and in your onResume() of your Activity, stop the Service.
  2. The Service itself should also register for the ACTION_DOWNLOAD_COMPLETE broadcast.
  3. If you receive the broadcast when your Activity is in the background, i.e., in your Service, you can post a notification or toast or whatever that the user clicks to get back to your Activity. You should also register for ACTION_NOTIFICATION_CLICKED.

Option 2

  1. Simply create a manifest-registered BroadcastReceiver to listen for the ACTION_DOWNLOAD_COMPLETE broadcast.
  2. In the onReceive() you can do whatever you want when the download is complete. Again, if you Activity is already in the foreground you can directly update your UI. Else, user clicks the Notification and you can handle that in ACTION_NOTIFICATION_CLICKED.
Community
  • 1
  • 1
curioustechizen
  • 10,572
  • 10
  • 61
  • 110