Issue
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
?
Solution
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" />
Answered By - Nawrez
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.