下载完成后如何在片段活动中调用函数(使用DownloadManager)?

时间:2018-01-09 17:00:35

标签: android android-fragments

我使用了我在YouTube上发现的一些代码以及Stackoverflow从网站下载文件。这一直很好,但现在我想打开已在WebView中下载的PDF。我将弄清楚如何做到这一点,但首先我需要知道如何在下载完成后执行片段中的函数。我尝试使用BroadcastReceiver,但我想我做错了:(

我的代码:

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");
}

0 个答案:

没有答案