Android Download Manager从HTML获取链接并开始下载

时间:2016-02-18 09:26:47

标签: php android html android-download-manager

有没有办法让Android下载管理器立即获取链接并立即开始下载。

示例HTML页面

<html>
<head>
<title>Thank You</title>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
</head>

<body>
<a href="http://domain.com/file.dat" title="Order" download>Download File</a>   
</body>
</html>

我正在使用的当前Android下载管理器

                        public void onResponse(String response) {
                            // Toast.makeText(MainActivity.this,response,Toast.LENGTH_LONG).show();
                             downloadManager = (DownloadManager)getSystemService(DOWNLOAD_SERVICE);
                             Uri Download_Uri = Uri.parse(url); // put here your URL that you get 
                             DownloadManager.Request request = new DownloadManager.Request(Download_Uri);
                             request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE);
                             //Set whether this download may proceed over a roaming connection.
                             request.setAllowedOverRoaming(false);
                             //Set the title of this download, to be displayed in notifications (if enabled).
                             String link = url.toString();

                             String fileName = link.substring(link.lastIndexOf('/') + 1);

                             request.setTitle(fileName);
                             //Set a description of this download, to be displayed in notifications (if enabled)
                             request.setDescription("Android Data download using DownloadManager.");
                             //Set the local destination for the downloaded file to a path within the application's external files directory
                             //request.setDestnInExtFilesDir(this, Environment.DIRECTORY_DOWNLOADS,"");
                             downloadReference = downloadManager.enqueue(request);
                             Intent intent = new Intent();
                             intent.setAction(DownloadManager.ACTION_VIEW_DOWNLOADS);
                             startActivity(intent);
                         }

请尽可能帮助我修改代码,对不起我是新手。

非常感谢你!

1 个答案:

答案 0 :(得分:0)

使用JSOUP,您可以使用它来获取属性,文本,

public void hyperlinkUpdate(HyperlinkEvent e) {
    if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
        String description = e.getDescription();
        ...
    }
}

添加到gradle中的依赖项,

Document doc = Jsoup.connect("http://example.com")// html page link
Element link = doc.select("a").first();

String text = doc.body().text(); // "An example link"
String linkHref = link.attr("href"); // "http://example.com/"
String linkText = link.text(); // "example"

更多例如,http://www.androidbegin.com/tutorial/android-basic-jsoup-tutorial/