如何显示下载完成弹出确定按钮

时间:2018-05-13 13:42:39

标签: android android-studio download download-manager

我在Android Studio中使用webView制作应用程序,我的应用程序包含所有图像,所以我也在我的应用程序中实现了下载按钮,但当有人点击下载按钮时它只是下载图像,我想在下载时显示弹出完成了,像这样的, 下载已完成且低于确定按钮

这是我的下载代码

 Uri source = Uri.parse(url);
                // Make a new request pointing to the .apk url
                DownloadManager.Request request = new DownloadManager.Request(source);
                // appears the same in Notification bar while downloading
                request.setDescription("Description for the DownloadManager Bar");
                request.setTitle("PunjabiDharti.jpg");
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
                    request.allowScanningByMediaScanner();
                    request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
                }
                // save the file in the "Downloads" folder of SDCARD
                request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "PunjabiDharti.jpg");
                // get download service and enqueue file
                DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
                manager.enqueue(request);

如何显示弹出窗口?

1 个答案:

答案 0 :(得分:1)

首先,在 build.gradle

中添加 MaterialDialog 的依赖项
dependencies {
    // ... other dependencies here
    implementation 'com.afollestad.material-dialogs:core:0.9.6.0'
}

创建 BroadcastReceiver处理程序

BroadcastReceiver downloadListener = new BroadcastReceiver(){
    public void onReceive(Context ct, Intent intent){
        new MaterialDialog.Builder(this)
              .title("Download Completed")
              .content("Download Successfully Completed")
              .positiveText("OK")
              .show();
    }
};

现在注册接收器

registerReceiver(downloadListener, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));