简体   繁体   English

如何阻止webChromeClient在Android浏览器中打开文件

[英]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.

相关问题 android应用程序正在打开浏览器应用程序而不是webView,我该如何停止呢? - android application is opening a browser application instead of a webView, how do I stop that? Cordova 6.2.3没有WebChromeClient,如何覆盖onJsPrompt()? - Cordova 6.2.3 has no WebChromeClient,how can i override the onJsPrompt()? 打开活动时如何停止“跳跃效果”? - How can I stop the 'jumping effect' when opening an activity? 如何下载文件而不是在浏览器中打开? - How to make files download instead of opening in browser? 当任何浏览器在android上保存文件时,是否发送了广播? 如何在Android的浏览器中拦截保存/下载的文件? - When any browser saves a file on android, is there a broadcast sent? How can I intercept saved/downloaded files in a browser on Android? 如果浏览器已停止,如何停止该JSP? - How can I stop this JSP if the browser has stopped? 我如何在Android中停止线程 - How I can stop thread in Android 我如何劫持已打开的浏览器会话并将其附加到Webdriver,而不是自己打开浏览器 - How can I hijack an opened browser session and attach it to the Webdriver than opening a browser by itself 如何阻止多个意图按顺序打开? - How to stop multiple intents from opening sequentially? Android - 如何从外部存储读取文本文件 - Android - How can I read text files from external storage
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM