[英]How can I stop webChromeClient from opening files in the Android Browser
I am developing an Android App using WebView which loads a locally stored HTML5/JQuery Mobile app in the assets folder. 我正在使用WebView开发一个Android应用程序,该程序将在资产文件夹中加载本地存储的HTML5 / JQuery Mobile应用程序。 The App gets content from a REST service on a domain I own.
该应用程序从我拥有的域上的REST服务获取内容。 The content is a mixture of HTML content and downloadable files.
内容是HTML内容和可下载文件的混合。
My problem lies when a user clicks on a button to download a file, a Browser window launches and the file is downloaded, which is great. 我的问题在于,当用户单击按钮下载文件时,将启动“浏览器”窗口并下载文件,这很棒。 However my App then loses focus and I have a blank browser on screen.
但是,我的应用程序随后失去了焦点,并且屏幕上没有空白的浏览器。 I have done a bit of Googling and have read a few answers on Stack Overflow and have adapted my code to set a Download Listener, but this still loads the Browser.
我做了一些谷歌搜索,并阅读了一些有关Stack Overflow的答案,并修改了我的代码以设置下载侦听器,但这仍然会加载浏览器。 Please advise.
请指教。 My code is listed below
我的代码在下面列出
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
WebView webView = (WebView)findViewById(R.id.webView);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setAllowFileAccess(true);
webView.setWebChromeClient(new WebChromeClient());
webView.setDownloadListener(new DownloadListener() {
public void onDownloadStart(String url, String userAgent,
String contentDisposition, String mimetype,
long contentLength) {
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
}
});
webView.loadUrl("file:///android_asset/www/index.html");
}
Have you tried using the Android DownloadManager (please see http://developer.android.com/reference/android/app/DownloadManager.html ) instead of starting a VIEW Intent? 您是否尝试过使用Android DownloadManager(请参阅http://developer.android.com/reference/android/app/DownloadManager.html )而不是启动VIEW Intent? I think that you'd just need to enqueue a download from your DownloadListener.
我认为您只需要从DownloadListener入队下载。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.