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.
You are welcome, happy new year.