protected override void OnCreate(Bundle savedInstanceState) { base.OnCreate(savedInstanceState); // Set our view from the "main" layout resource SetContentView(Resource.Layout.Main); InitDownloadManager(); // Get our button from the layout resource, // and attach an event to it Button button = FindViewById <Button> (Resource.Id.myButton); var foo = new Downloader(); button.Click += delegate { // If already downloading, abort it. if (foo.IsDownloading()) { foo.AbortDownloading(); button.Text = "Download aborted."; return; } button.Text = "Start downloading ..."; foo.InitializeDownload(); foo.File.PropertyChanged += (sender, e) => { System.Diagnostics.Debug.WriteLine("[Property changed] " + e.PropertyName + " -> " + sender.GetType().GetProperty(e.PropertyName).GetValue(sender, null).ToString()); // Update UI text-fields var downloadFile = ((IDownloadFile)sender); switch (e.PropertyName) { case nameof(IDownloadFile.Status): FindViewById <TextView>(Resource.Id.value_status).Text = downloadFile.Status.ToString(); break; case nameof(IDownloadFile.StatusDetails): FindViewById <TextView>(Resource.Id.value_statusdetails).Text = downloadFile.StatusDetails; break; case nameof(IDownloadFile.TotalBytesExpected): FindViewById <TextView>(Resource.Id.value_totalbytesexpected).Text = downloadFile.TotalBytesExpected.ToString(); break; case nameof(IDownloadFile.TotalBytesWritten): FindViewById <TextView>(Resource.Id.value_totalbyteswritten).Text = downloadFile.TotalBytesWritten.ToString(); break; } // Update UI if download-status changed. if (e.PropertyName == "Status") { switch (((IDownloadFile)sender).Status) { case DownloadFileStatus.COMPLETED: case DownloadFileStatus.FAILED: case DownloadFileStatus.CANCELED: button.Text = "Downloading finished."; // Get the path this file was saved to. When you didn't set a custom path, this will be some temporary directory. var nativeDownloadManager = (DownloadManager)ApplicationContext.GetSystemService(DownloadService); System.Diagnostics.Debug.WriteLine(nativeDownloadManager.GetUriForDownloadedFile(((DownloadFileImplementation)sender).Id)); // If you don't want your download to be listed in the native "Download" app after the download is finished //nativeDownloadManager.Remove(((DownloadFileImplementation)sender).Id); break; } } // Update UI while donwloading. if (e.PropertyName == "TotalBytesWritten" || e.PropertyName == "TotalBytesExpected") { var bytesExpected = ((IDownloadFile)sender).TotalBytesExpected; var bytesWritten = ((IDownloadFile)sender).TotalBytesWritten; if (bytesExpected > 0) { var percentage = Math.Round(bytesWritten / bytesExpected * 100); button.Text = "Downloading (" + percentage + "%)"; } } }; foo.StartDownloading(FindViewById <Switch>(Resource.Id.switch1).Checked); }; }
public void Download() { var folder = Windows.ApplicationModel.Package.Current.InstalledLocation; path = Path.Combine(folder.Path, "data_10mb.test"); // If already downloading, abort it. if (foo.IsDownloading()) { foo.AbortDownloading(); downloadBtn.Content = "Download aborted."; return; } downloadBtn.Content = "Start downloading ..."; foo.InitializeDownload(); foo.File.PropertyChanged += (sender, e) => { System.Diagnostics.Debug.WriteLine("[Property changed] " + e.PropertyName + " -> " + sender.GetType().GetProperty(e.PropertyName).GetValue(sender, null).ToString()); // Update UI text-fields var downloadFile = ((IDownloadFile)sender); switch (e.PropertyName) { case nameof(IDownloadFile.Status): Statustext.Text = downloadFile.Status.ToString(); break; case nameof(IDownloadFile.TotalBytesExpected): BytesExpected.Text = downloadFile.TotalBytesExpected.ToString(); break; case nameof(IDownloadFile.TotalBytesWritten): BytesReceived.Text = downloadFile.TotalBytesWritten.ToString(); break; } // Update UI if download-status changed. if (e.PropertyName == "Status") { switch (((IDownloadFile)sender).Status) { case DownloadFileStatus.COMPLETED: case DownloadFileStatus.FAILED: case DownloadFileStatus.CANCELED: downloadBtn.Content = "Downloading finished."; ((DownloadFileImplementation)downloadFile).DownloadOperation.ResultFile.DeleteAsync(); break; } } // Update UI while donwloading. if (e.PropertyName == "TotalBytesWritten" || e.PropertyName == "TotalBytesExpected") { var bytesExpected = ((IDownloadFile)sender).TotalBytesExpected; var bytesWritten = ((IDownloadFile)sender).TotalBytesWritten; if (bytesExpected > 0) { var percentage = Math.Round(bytesWritten / bytesExpected * 100); downloadBtn.Content = "Downloading (" + percentage + "%)"; } } }; foo.StartDownloading(CellularNetworkAllowed.IsChecked.HasValue && CellularNetworkAllowed.IsChecked.Value); }
protected override void OnCreate(Bundle savedInstanceState) { base.OnCreate(savedInstanceState); // Set our view from the "main" layout resource SetContentView(Resource.Layout.Main); InitDownloadManager(); // Get our button from the layout resource, // and attach an event to it Button button = FindViewById <Button> (Resource.Id.myButton); var foo = new Downloader(); button.Click += delegate { // If already downloading, abort it. if (foo.IsDownloading()) { foo.AbortDownloading(); button.Text = "Download aborted."; return; } button.Text = "Start downloading ..."; foo.InitializeDownload(); foo.File.PropertyChanged += (sender, e) => { System.Diagnostics.Debug.WriteLine("[Property changed] " + e.PropertyName + " -> " + sender.GetType().GetProperty(e.PropertyName).GetValue(sender, null).ToString()); // Update UI text-fields var downloadFile = ((IDownloadFile)sender); switch (e.PropertyName) { case nameof(IDownloadFile.Status): FindViewById <TextView>(Resource.Id.value_status).Text = downloadFile.Status.ToString(); break; case nameof(IDownloadFile.StatusDetails): FindViewById <TextView>(Resource.Id.value_statusdetails).Text = downloadFile.StatusDetails; break; case nameof(IDownloadFile.TotalBytesExpected): FindViewById <TextView>(Resource.Id.value_totalbytesexpected).Text = downloadFile.TotalBytesExpected.ToString(); break; case nameof(IDownloadFile.TotalBytesWritten): FindViewById <TextView>(Resource.Id.value_totalbyteswritten).Text = downloadFile.TotalBytesWritten.ToString(); break; } // Update UI if download-status changed. if (e.PropertyName == "Status") { switch (((IDownloadFile)sender).Status) { case DownloadFileStatus.COMPLETED: case DownloadFileStatus.FAILED: case DownloadFileStatus.CANCELED: button.Text = "Downloading finished."; // Get the path this file was saved to. When you didn't set a custom path, this will be some temporary directory. var nativeDownloadManager = (DownloadManager)ApplicationContext.GetSystemService(DownloadService); System.Diagnostics.Debug.WriteLine(nativeDownloadManager.GetUriForDownloadedFile(((DownloadFileImplementation)sender).Id)); break; } } // Update UI while donwloading. if (e.PropertyName == "TotalBytesWritten" || e.PropertyName == "TotalBytesExpected") { var bytesExpected = ((IDownloadFile)sender).TotalBytesExpected; var bytesWritten = ((IDownloadFile)sender).TotalBytesWritten; if (bytesExpected > 0) { var percentage = Math.Round(bytesWritten / bytesExpected * 100); button.Text = "Downloading (" + percentage + "%)"; } } }; try { foo.StartDownloading(FindViewById <Switch>(Resource.Id.switch1).Checked); } catch (Java.Lang.IllegalArgumentException) { foo.File = null; button.Text = "Download crashed."; try { //Open the specific App Info page: Intent intent = new Intent(Android.Provider.Settings.ActionApplicationDetailsSettings); intent.SetData(Android.Net.Uri.Parse("package:com.android.providers.downloads")); StartActivity(intent); } catch (ActivityNotFoundException) { //Open the generic Apps page: Intent intent = new Intent(Android.Provider.Settings.ActionManageApplicationsSettings); StartActivity(intent); } } }; }
public override void ViewDidLoad() { base.ViewDidLoad(); // If you want to take full control of the saved file, see `ExtendedUrlSessionDownloadDelegate` //CrossDownloadManager.Current.PathNameForDownloadedFile = new Func<IDownloadFile, string> (file => { // string fileName = (new NSUrl (file.Url, false)).LastPathComponent; // return Path.Combine (Environment.GetFolderPath (Environment.SpecialFolder.MyDocuments), fileName); //}); var foo = new Downloader(); // Perform any additional setup after loading the view, typically from a nib. Button.AccessibilityIdentifier = "myButton"; Button.TouchUpInside += delegate { // If already downloading, abort it. if (foo.IsDownloading()) { foo.AbortDownloading(); Button.SetTitle("Download aborted.", UIControlState.Normal); return; } Button.SetTitle("Start downloading ...", UIControlState.Normal); foo.InitializeDownload(); foo.File.PropertyChanged += (sender, e) => { // Update UI text-fields var downloadFile = ((IDownloadFile)sender); switch (e.PropertyName) { case nameof(IDownloadFile.Status): InvokeOnMainThread(() => { StatusTextField.Text = downloadFile.Status.ToString(); }); break; case nameof(IDownloadFile.StatusDetails): InvokeOnMainThread(() => { StatusDetailsTextField.Text = downloadFile.StatusDetails; }); break; case nameof(IDownloadFile.TotalBytesExpected): InvokeOnMainThread(() => { TotalBytesExpectedTextField.Text = downloadFile.TotalBytesExpected.ToString(); }); break; case nameof(IDownloadFile.TotalBytesWritten): InvokeOnMainThread(() => { TotalBytesWrittenTextField.Text = downloadFile.TotalBytesWritten.ToString(); }); break; } // Update UI if download-status changed. if (e.PropertyName == "Status") { switch (((IDownloadFile)sender).Status) { case DownloadFileStatus.COMPLETED: case DownloadFileStatus.FAILED: case DownloadFileStatus.CANCELED: InvokeOnMainThread(() => { Button.SetTitle("Downloading finished.", UIControlState.Normal); }); break; } } // Update UI while donwloading. if (e.PropertyName == "TotalBytesWritten" || e.PropertyName == "TotalBytesExpected") { var bytesExpected = ((IDownloadFile)sender).TotalBytesExpected; var bytesWritten = ((IDownloadFile)sender).TotalBytesWritten; if (bytesExpected > 0) { var percentage = Math.Round(bytesWritten / bytesExpected * 100); InvokeOnMainThread(() => { Button.SetTitle("Downloading (" + percentage + "%)", UIControlState.Normal); }); } } }; foo.StartDownloading(Switch.On); }; }