我正在使用意图将我的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
的情况下下载我的应用程序吗?
答案 0 :(得分:0)
强行下载:
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
}
};
同样不要忘记在:
中声明perrmission <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />