0

I'm using some code which I've found on YouTube as well as Stackoverflow to download a file from a website. This has been working fine, but now I would like to open the PDF which has been downloaded in a WebView. I will figure out how to do that, but first I need to know how to execute a function which is in a Fragment once the download has finished. I alread tried using a BroadcastReceiver but I guess I did it wrong :(

My Code:

private void downloadZusammenfassung(View v){
    DownloadManager downloadManager;
    downloadManager = (DownloadManager)getActivity().getSystemService(Context.DOWNLOAD_SERVICE);
    Button button = (Button)v;
    String zusammenfassungLink = "https://dan6erbond.github.io/I1A/Documents/Zusammenfassungen/" + button.getText() + ".pdf";
    Log.i("TAG", zusammenfassungLink);
    Uri uri = Uri.parse(zusammenfassungLink);
    DownloadManager.Request request = new DownloadManager.Request(uri);
    request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
    Long reference = downloadManager.enqueue(request);
    BroadcastReceiver onComplete = new BroadcastReceiver() {
        public void onReceive(Context ctxt, Intent intent) {
            openZusammenfassung();
        }
    };
    Intent registerReceiver (onComplete, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE);
}

public void openZusammenfassung(){
    Log.i("TAG", "bla");
}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Dan6erbond
  • 9
  • 1
  • 2
  • your `BroadcastReceiver onComplete` will never fire it's `onReceive()` if you do not register it - like you're not doing in the posted code. You need to register it to a certain `IntentFilter`. However, you also need to make a few more changes... – Shark Jan 09 '18 at 17:05
  • The broadcast about the `DownloadManager.ACTION_DOWNLOAD_COMPLETE` won't be fired by your application which also means that the BroadcastReceiver you'll be using needs to be declared in the `AndroidManfiest.xml` and have the `exported=true` so it can hear other processes' broadcasts as well. – Shark Jan 09 '18 at 17:07
  • Here's a link to a very similar question that has an answer to what you're looking for. https://stackoverflow.com/questions/18789246/broadcastreceiver-not-receiving-download-complete-action – Shark Jan 09 '18 at 17:07
  • Does that mean that the BroadcastReceiver needs to be in a seperate Java File? – Dan6erbond Jan 09 '18 at 17:11
  • Not necessarily, but yeah it will make your life a bit easier. If you still want to use methods from your fragment, there's something else you can do. – Shark Jan 09 '18 at 17:14
  • have the DownloadComplete broadcast listener to be managed by the Android OS by declaring it in the AndroidManifest, then have another `BroadcastReceiver` in your fragment - you will register your Fragment's BroadcastReceiver by using `LocalBroadcastManager.getInstance(getContext()).registerRecevier(myFragmentReceiver);` – Shark Jan 09 '18 at 17:15
  • and then when the `DownloadComplete` broadcast listener hears the DOWNLOAD_COMPLETE intent/action, it can fire a local in-app broadcast to your other receiver by using `LocalBroadcastManager.getInstance(someContext).sendBroadcast(myDownloadDoneIntent);` and then the Fragment's BroadcastReceiver will hear it as well. – Shark Jan 09 '18 at 17:17
  • Basically, the BroadcastReceiver in a separate Java file will hear the OS's broadcast and simply process it a bit and forward only the necessary parts to **your** BroadcastReceiver so you can do your magic when it hears something. But do have in mind that you'll have to register/unregister the Fragment's BroadcastReceiver manually in all lifecycle callbacks. – Shark Jan 09 '18 at 17:18
  • Instead of posting blobs of code in comments, please feel free to edit your own question with your up-to-date code so that other viewers as well can figure out what you're talking about (and jump in with some additional help) – Shark Jan 10 '18 at 11:25

0 Answers0