android - 下载开始并显示应用程序后如何关闭 WebBrowser?

标签 android android-studio android-intent webview android-webview

我正在使用 intent 将我的 URL 从我的 android 应用程序传递到 WebBrowser 以下载文件。但是一旦下载开始,它就会停留在 WebBrowser 中。所以我想在下载开始后关闭 WebBrowser 并显示应用程序。

这是我用来传递我的 URL 的代码:

js hide: false console: true babel:

} else if (tabId == R.id.tab_b) {
    webView.loadUrl("file:///android_asset/D.html");
    webView.setDownloadListener(new DownloadListener() {
        public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) {
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setData(Uri.parse(url));
            Toast.makeText(getApplicationContext(), "Downloading File",   To notify the Client that the file is being downloaded
            Toast.LENGTH_LONG).show();
            startActivity(intent);
        }
    });
}

最后,如果可能的话,我可以在不打开 WebBrowser 的情况下下载我的应用程序吗?

最佳答案

强制下载:

 webView.setDownloadListener(new DownloadListener() {
        @Override
        public void onDownloadStart(String aUrl, String userAgent, String contentDisposition, String mimetype, long contentLength) {


                    DownloadManager.Request request = new DownloadManager.Request(Uri.parse(aUrl));
                    request.allowScanningByMediaScanner();
                    CookieManager cookieManager = CookieManager.getInstance();
                    String cookie = cookieManager.getCookie(aUrl);
                    request.addRequestHeader("Cookie", cookie);
                    request.addRequestHeader("User-Agent", userAgent);
                    Environment.getExternalStorageDirectory();
                    request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
                    request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, fileName);
                    DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
                    dm.enqueue(request);
                    registerReceiver(onComplete, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));





        }


});

不要忘记添加这些方法以在下载后打开 pdf:

BroadcastReceiver onComplete=new BroadcastReceiver() {
        public void onReceive(Context ctxt, Intent intent) {
// HERE YOU ADD WHAT YOU WANT AFTER DONWLOAD     
 }
            };

也不要忘记在 :

中声明权限
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

关于android - 下载开始并显示应用程序后如何关闭 WebBrowser?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48340004/

相关文章:

java - 无法连接到 MySQL 服务器

android - 当用户按下键盘上的返回键时隐藏键盘

android - 从 IntelliJ IDEA 导入项目到 Android Studio

Android 应用未构建 : Duplicate files copied in APK META-INF/INDEX. 列表

android - 如何获取存储在SD卡中的图片数量

android - 程序在 android 启动画面后不工作

java - Android加载字节数组图像列表

android - java.lang.RuntimeException 无法实例化服务 : java. lang.NullPointerException

java - 如何在 android studio 的 Library 模块中使用 getResources() 函数

android - 在 Android 设备上选择文件夹