Android Tutorial DownloadManager tutorial

Here i'll post one or two examples showing how to use the DownloadManager library.

DownloadManager simple example.
Here's what must be the most simple way to use DownloadManager.
It shows usage of all three DownloadManager objects:
  • DownloadManager the main library object.
  • DownloadManagerRequest an object used to create a new download request.
  • DownloadManagerQuery an object used to query the DownloadManager for the status of a DownloadRequest.

DownloadManager requires a minimum Android API level of 9 (Gingerbread), the SQL library must also be enabled in order to work with the DownloadManager Query method.

B4X:
Sub Process_Globals

   Dim DOWNLOAD_ADDRESS As String="http://www.b4x.com/android/images/wiki_logo.png"
   Dim DOWNLOAD_FILENAME As String="wiki_logo.png"
   
   '   DownloadId is a unique identifier assigned by the DownloadManager to this download task
   Dim DownloadId As Long
   
End Sub

Sub Globals
   
   Dim DownloadButton As Button
   Dim DownloadManager1 As DownloadManager
   
   Dim ImageView1 As ImageView
End Sub

Sub Activity_Create(FirstTime As Boolean)
   Activity.LoadLayout("Main")
   
   '   RegisterReceiver enables DownloadManager to raise events
   DownloadManager1.RegisterReceiver("DownloadManager1")
   
   '   Utils is a helper code module
   If Utils.IsInitialized=False Then
      Utils.Initialize(DownloadManager1)
   End If
   
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub DownloadButton_Click

   Dim DownloadManagerRequest1 As DownloadManagerRequest
   DownloadManagerRequest1.Initialize(DOWNLOAD_ADDRESS)
   DownloadManagerRequest1.Description="DownloadManager demo"
   '   save the download to external memory
   '   note you must manually update your project's manifest file adding android.permission.WRITE_EXTERNAL_STORAGE
   DownloadManagerRequest1.DestinationUri="file://"&File.Combine(File.DirRootExternal, DOWNLOAD_FILENAME)
   DownloadManagerRequest1.Title=DOWNLOAD_FILENAME
   DownloadManagerRequest1.VisibleInDownloadsUi=True
   
   DownloadId=DownloadManager1.Enqueue(DownloadManagerRequest1)
End Sub

Sub DownloadManager1_DownloadComplete(DownloadId1 As Long)
   '   this does not guarantee that the download has actually successfully downloaded
   '   it means a DownloadMananger DownloadManagerRequest has completed
   '   we need to find that status of that request but only if that request matches the request we started
   
   If DownloadId=DownloadId1 Then
      '   this is the download request we started
      '   query the DownloadManager for info on this request
      Dim DownloadManagerQuery1 As DownloadManagerQuery
      DownloadManagerQuery1.Initialize
      DownloadManagerQuery1.SetFilterById(DownloadId)
      
      '   you must enable the SQL library to work with the Cursor object
      Dim StatusCursor As Cursor
      '   pass our DownloadManagerQuery to the DownloadManager
      StatusCursor=DownloadManager1.Query(DownloadManagerQuery1)
      If StatusCursor.RowCount>0 Then
         StatusCursor.Position=0   
         
         Dim StatusInt As Int
         StatusInt=StatusCursor.getInt(DownloadManager1.COLUMN_STATUS)
         Log("Download Status = "&Utils.GetStatusText(StatusInt))

         If StatusInt=DownloadManager1.STATUS_FAILED OR StatusInt=DownloadManager1.STATUS_PAUSED Then
            Dim ReasonInt As Int
            ReasonInt=StatusCursor.GetInt(DownloadManager1.COLUMN_REASON)
            Log("Status Reason = "&Utils.GetReasonText(ReasonInt))
         End If
         
         If StatusInt=DownloadManager1.STATUS_SUCCESSFUL Then
            ImageView1.Bitmap=LoadBitmap(File.DirRootExternal, DOWNLOAD_FILENAME)
         End If
         
      Else
         '   always check that the Cursor returned from the DownloadManager Query method is not empty
         Log("The DownloadManager has no trace of our request, it could have been cancelled by the user using the Android Downloads app or an unknown error has occurred.")
      End If
      
      '   free system resources
      StatusCursor.Close
      DownloadManager1.UnregisterReceiver
   End If
   
End Sub

DownloadManager requires android.permission.INTERNET and this is automatically added to your manifest when you use the library.
Saving downloads to external storage requires android.permission.WRITE_EXTERNAL_STORAGE, this must be manually added to your manifest using the manifest editor.
Here's the line i added to this example's manifest file:

B4X:
AddPermission(android.permission.WRITE_EXTERNAL_STORAGE)

The example downloads an image to external memory and displays it in an ImageView.
Not very exciting but it shows you basic syntax and usage etc.

I'll create some more advanced examples tomorrow hopefully and post them in this thread.

Martin.
 

Attachments

  • simple_example.zip
    8.6 KB · Views: 2,116
Last edited:

Devv

Active Member
Licensed User
Longtime User
How could i stop the download from an activity ?
i tried stop service but it wont stop the download
 
Top