使用下载管理器类,从的WebView下载文件

编程入门 行业动态 更新时间:2024-10-23 01:50:16
本文介绍了使用下载管理器类,从的WebView下载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我这个小code这里的时候,点击的WebView链接是一个链接到一个文件,在这种情况下.MP4。这code将进入默认的Web浏览器,并请求应用程序,可以查看此文件类型。

I have this little code here when click a link on webview that is a link to a file, in this case .mp4. this code will go to default web browser and request app that can view this file type.

myWebView.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); } });

我要的是当我点击该文件链接,它创建要求气象下载或查看文件的对话框。如果点击下载,我想使用下载管理器类来处理它,并下载该文件的背景和警报完成时。如果单击视图,我想创建意图索要应用程序,可以不用去的Web浏览器查看此文件。

What i want is when i click that file link, it create a dialog that ask weather to download or view the file. If click download, i want to use DownloadManager class to handle it and download that file in the background and alert when completed. And if click view, i want to create intent that ask for app that can view this file without going to the web browser.

private class MyWebViewClient extends WebViewClient { @Override public boolean shouldOverrideUrlLoading(WebView view, final String url) { if (url.endsWith(".mp4")) { AlertDialog.Builder builder = new AlertDialog.Builder(context); builder.setMessage(R.string.dialog_title) .setCancelable(false) .setPositiveButton(R.string.dialog_download, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url)); request.setDescription("Some descrition"); request.setTitle("Some title"); // in order for this if to run, you must use the android 3.2 to compile your app request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "name-of-the-file.ext"); // get download service and enqueue file DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE); manager.enqueue(request); } }) .setNegativeButton(R.string.dialog_play, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { WebView myWebView = (WebView) findViewById(R.id.webview); myWebView.setDownloadListener(new DownloadListener() { public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) { Intent intent = new Intent(); intent.setAction(Intent.ACTION_VIEW); intent.setDataAndType(Uri.parse(url), "video/mp4"); startActivity(intent); } }); } }); AlertDialog alert = builder.create(); alert.show(); } return false; } }

现在我得到这个code,提示用户下载或播放,用户点击的MP4文件。但是当我点击播放或下载这是工作,直到我点击链接的第二次,有什么不对的code以上,如果任何人都可以解决此请。谢谢。

Now i got this code that prompt user to download or play mp4 file that the user clicked. But when i click Play or Download it is work until i click the link the second time, what's wrong with the code above if anyone can correct this please. thanks.

我很新至Android开发和Java中,如果任何人都可以指导我通过这个它会帮助我学得更快。而且也对不​​起我的英语水平...

I'm very new to Android developing and java, if anyone could guide me through this it will help me learn faster. And also sorry for my English...

推荐答案

该问题的解决方案依赖于拦截你的We​​bView试图加载的URL。创建 WebViewClient 并覆盖shouldOverrideUrlLoading方式:

The solution to your problem relies on intercepting the URL that your webview is trying to load. Create a WebViewClient and overwrite the shouldOverrideUrlLoading method:

public boolean shouldOverrideUrlLoading(WebView view, String url) { if (url.endsWith(".mp3")) { //or whatever other extension //Prompt user for action (save or view) } return false; }

然后,根据用户选择什么,启动一个AsyncTask下载处理下载文件,或启动的意图进行查看。

Then, depending on what the user chooses, start an AsyncTask for downloading to handle downloading the file, or launch the intent for viewing.

更多推荐

使用下载管理器类,从的WebView下载文件

本文发布于:2023-11-28 13:14:52,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1642560.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:管理器   文件   WebView

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!