我正在使用 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/