Forum Xamarin.Android

Checking that DownloadManager has finished a download using BroadcastReceiver

So I want my App to be able to download a PDF-File and open it as soon as it finsihed the download.
The code is called in an onClickMethod of a ListElement

I use the following code to download the file:

ISharedPreferences prefs = PreferenceManager.GetDefaultSharedPreferences(this); var token = prefs.GetString("token", ""); DownloadManager dm; long myDownloadReference; dm = (DownloadManager)GetSystemService(DownloadService); String url = "" + token + "&id=" + documents[position].docId; DownloadManager.Request request = new DownloadManager.Request(Android.Net.Uri.Parse(url)); request.SetDescription(documents[position].docId).SetTitle("PDF - Download"); request.SetDestinationInExternalFilesDir(this, Android.OS.Environment.DirectoryDownloads, documents[position].docId+".pdf"); request.SetVisibleInDownloadsUi(true); myDownloadReference= dm.Enqueue(request);

and this code to open the pdf file:

Java.IO.File file = new Java.IO.File("/sdcard/Android/data/mWidas2.mWidas2/files/" + Android.OS.Environment.DirectoryDownloads +"/" + documents[position].docId + ".pdf"); file.SetReadable(true); Android.Net.Uri uri = Android.Net.Uri.FromFile(file); Intent intent = new Intent(Intent.ActionView); intent.SetDataAndType(uri, "application/pdf"); intent.SetFlags(ActivityFlags.ClearWhenTaskReset | ActivityFlags.NewTask); StartActivity(intent);

I have to access the documents[position].docId String in order to open the correct file. Unfortunately all solutions I found (which are not that many for Xamarin) want me to create a new class for the BroadcastReceiver which doesn't let me use the needed String.

Some sample code with a possible solution for my problem would be greatly appreciated.
Thanks in advance!

Sign In or Register to comment.