2

I'm using intent to pass my URL to WebBrowser from my android app to download files. But once the download starts, it stays in WebBrowser. So I would like to close the WebBrowser and show app once the download starts.

Here is the code I'm using to pass my 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);
        }
    });
}

Finally, if possible, can I download my app without opening the WebBrowser?

Sneh Pandya
  • 8,197
  • 7
  • 35
  • 50
mark22
  • 17
  • 7
  • may be this will hep you . All the very best [https://stackoverflow.com/a/48130603/8234008](https://stackoverflow.com/a/48130603/8234008) – MayankD Jan 19 '18 at 12:41

1 Answers1

1

Force download :

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





        }


});

and don't forget to add those methods to open pdf after download :

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

Also don't forget declaring perrmission in the :

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Nawrez
  • 3,314
  • 8
  • 28
  • 42
  • 1
    Thanks for your warm reply. I used your given code but I cannot resolve onComplete and fileName. It is highlighted in red color. – mark22 Jan 20 '18 at 04:08
  • you're welcome ! onComplete IS ADDED as a second part of the answer , please apply changes from my last edit :) – Nawrez Jan 20 '18 at 07:28