How To Check Download Manager File Downloaded and Show File Name Xamarin.Forms Android

SimonGhost 256 Reputation points
2020-12-28T21:44:03.437+00:00

Hello Im Downloading Files Using DownloadManager and its Working Good But how to Check The File is Downloaded Done by his ID Then Get File Name To Show it On Toast...
My Code:

    var manager = DownloadManager.FromContext(Forms.Context);
    var request = new DownloadManager.Request(Android.Net.Uri.Parse(URL));
    request.SetNotificationVisibility(DownloadVisibility.VisibleNotifyCompleted);
    request.SetDestinationInExternalPublicDir(Location, FileName);
    request.SetTitle(FNameFulExt.Replace(".mp4",""));
    request.SetDescription("File is Downloading...");
    long downloadId = manager.Enqueue(request);

Some similar questions say you should use a broadcast receiver, but how can I check (downloadId ) for my files then get File Name ?
also this my broadcast receiver file:

[BroadcastReceiver]
[IntentFilter(new string[] { DownloadManager.ActionDownloadComplete })]
public class DownloadBrodacast : Android.Content.BroadcastReceiver
{
    public override void OnReceive(Context context, Intent intent)
    {
       //What should I do here
    }
}

and Thanks, everyone 😊

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,325 questions
0 comments No comments
{count} vote

Accepted answer
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 71,921 Reputation points Microsoft Vendor
    2020-12-29T09:03:29.807+00:00

    Hello,​

    Welcome to our Microsoft Q&A platform!

    Agree with alessandrocaliaro, For achievement in xamarin android, you can use following code.

       [BroadcastReceiver]  
           [IntentFilter(new string[] { DownloadManager.ActionDownloadComplete })]  
           public class DownloadBrodacast : Android.Content.BroadcastReceiver  
           {  
               public override void OnReceive(Context context, Intent intent)  
               {  
                   //What should I do here  
         
                   String action = intent.Action;  
                   if (DownloadManager.ActionDownloadComplete.Equals(action) && intent.Extras != null)  
                   {  
                       Bundle extras = intent.Extras;  
                       DownloadManager.Query q = new DownloadManager.Query();  
                       long downloadId = extras.GetLong(DownloadManager.ExtraDownloadId);  
                       q.SetFilterById(downloadId);  
                       var c = ((DownloadManager)context.GetSystemService(Context.DownloadService)).InvokeQuery(q);  
                       if (c.MoveToFirst())  
                       {  
                           int status = c.GetInt(c.GetColumnIndex(DownloadManager.ColumnStatus));  
                           if (status ==(int) DownloadStatus.Successful  )  
                           {  
         
                               String downloadFilePath = (c.GetString(c.GetColumnIndex(DownloadManager.ColumnUri))).Replace("file://", "");  
                               String downloadTitle = c.GetString(c.GetColumnIndex(DownloadManager.ColumnTitle));  
                               c.Close();  
                               Toast.MakeText(Android.App.Application.Context, "DownloadPath"+ downloadFilePath, ToastLength.Short).Show();  
                               Toast.MakeText(Android.App.Application.Context, "DownloadTitle" + downloadTitle, ToastLength.Short).Show();  
         
                                
                           }  
                           else if (status ==(int) DownloadStatus.Failed)  
                           {  
         
                             var code=  c.GetInt(c.GetColumnIndex(DownloadManager.ColumnReason));  
                               Toast.MakeText(Android.App.Application.Context,"donwload filed: " + code, ToastLength.Short).Show();  
                           }  
                       }  
                   }  
               }  
           }  
    

    You can RegisterReceiver in the OnCreate method of MainActivity.cs.

       DownloadBrodacast heartRateRec = new DownloadBrodacast();  
         RegisterReceiver(heartRateRec, new IntentFilter(DownloadManager.ActionDownloadComplete));  
    

    Best Regards,

    Leon Lu


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Alessandro Caliaro 4,181 Reputation points
    2020-12-28T21:53:29.013+00:00

    4399386

    This is a java sample that you can use. DownlodManager has a db that contains all info you need