Android 安卓下载意图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/525204/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android Download Intent
提问by Isaac Waller
I was wondering what is the intent for downloading URLs? In the browser, it will download stuff with a little notification icon. I was wondering if I can use that intent (and what it is).
我想知道下载 URL 的意图是什么?在浏览器中,它将下载带有小通知图标的内容。我想知道我是否可以使用该意图(以及它是什么)。
回答by Alex Jasmin
Applications can download files with the download manager just like the browser and gmail. This is available starting with Gingerbread.
应用程序可以像浏览器和 gmail 一样使用下载管理器下载文件。这从 Gingerbread 开始可用。
Your app needs the INTERNETpermission to initiate a download. To save the file in the default Download directory it also needs the WRITE_EXTERNAL_STORAGEpermission.
您的应用程序需要INTERNET权限才能启动下载。要将文件保存在默认下载目录中,它还需要WRITE_EXTERNAL_STORAGE权限。
Here's how you can download an URI:
以下是下载 URI 的方法:
DownloadManager.Request r = new DownloadManager.Request(uri);
// This put the download in the same Download dir the browser uses
r.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "fileName");
// When downloading music and videos they will be listed in the player
// (Seems to be available since Honeycomb only)
r.allowScanningByMediaScanner();
// Notify user when download is completed
// (Seems to be available since Honeycomb only)
r.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
// Start download
DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
dm.enqueue(r);
There are a bunch of other options for customizing the notification, querying the download state and setting the download location.
还有许多其他选项可用于自定义通知、查询下载状态和设置下载位置。
This blog postshows how you could use the download manager on previous versions of Android via hidden APIs.
这篇博文展示了如何通过隐藏的 API 在以前版本的 Android 上使用下载管理器。
回答by dparnas
While I don't believe there is a download Intent in the browser, you can probably use a normal ACTION_VIEW
Intent and have the browser decide if it should download or view the url based on the content-type.
虽然我不相信浏览器中有下载 Intent,但您可能可以使用普通的ACTION_VIEW
Intent 并让浏览器根据内容类型决定是否应该下载或查看 url。
So from your code trigger
所以从你的代码触发器
new Intent(Intent.ACTION_VIEW, Uri.parse(url))
and hope this triggers a download in the browser.
并希望这会触发浏览器中的下载。
回答by hacken
What are you trying to do? If your app wants to download a file you can use the UrlConnection code. If you want to download a package then ACTION_PACKAGE_INSTALL
should do what you want.
你想做什么?如果您的应用程序想要下载文件,您可以使用 UrlConnection 代码。如果你想下载一个包,那么ACTION_PACKAGE_INSTALL
应该做你想做的。