“Good programmers write solid code, while great programmers reuse the code of good programmers”
— W. Jason Gilmore, the author of “Beginning PHP and MySQL”
In Android Platform, it supports some ways to download files from URLs via HttpURLConnection or HttpClient, or DownloadProvider.When you write an android programm with Eclipse, try to import:
import android.provider.Downloads
The Eclipse gives a red error prompting and complains:
The import android.provider.Downloads cannot be resolved
Oops…
The download manager is not part of the public SDK, and there are no relational API description in Android developers Reference.Comment in sources frameworks/base/core/java/android/provider/Downloads.java, says: “For 1.0 the download manager can’t deal with abuse from untrusted apps, so this API is hidden.” Refer to the document description from: packages/providers/DownloadProvider/docs/index.html. we Know that Browser / Gmail / Market / Updater depend on the download manager. Yes, show some screenshots in the market:
We maybe can’t compile with Eclipse, but we can write an Android.mk makefile, use mmm to do it.
DownloadProvider is very easy to use.First, declare permission in AndroidManifest.xml:
<uses-permission
android:name="android.permission.INTERNET"
>
</uses-permission>
<uses-permission
android:name="android.permission.ACCESS_DOWNLOAD_MANAGER"
>
</uses-permission>
And then just need to fill some field values:
ContentValues values = new ContentValues();
String url = "http://blog.lytsing.org/wp-content/uploads/2010/06/android_downloadprovider_market.jpg";
values.put(Downloads.URI, url);
values.put(Downloads.MIMETYPE, "image/jpeg");
values.put(Downloads.FILENAME_HINT, getFullFilename("android_downloadprovider_market.jpg"));
values.put(Downloads.TITLE, "screenshot");
values.put(Downloads.DESCRIPTION, "screenshot file for DownloadProvider Demo");
values.put(Downloads.VISIBILITY, Downloads.VISIBILITY_VISIBLE);
values.put(Downloads.NOTIFICATION_CLASS, "org.lytsting.android.downloads.DownloadReceiver");
values.put(Downloads.NOTIFICATION_PACKAGE, "org.lytsting.android.downloads");
getContentResolver().insert(Downloads.CONTENT_URI, values);
private String getFullFilename(String filename) {
return Environment.getExternalStorageDirectory().toString() + "/download/" + filename);
}
Notes
Downloads.FILENAME_HINT, for a demo, here, I put the file into SD Card, the download directory, without checking any exception.
Downloads.NOTIFICATION_CLASS, you want to write a DownloadReceiver class, which extends BroadcastReceiver, and handle the message Downloads.DOWNLOAD_COMPLETED_ACTION or Downloads.NOTIFICATION_CLICKED_ACTION. like this:
public class DownloadReceiver extends BroadcastReceiver {
static final String TAG = "DownloadReceiver";
@Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG, intent.getAction());
if (intent.getAction().equals(Downloads.NOTIFICATION_CLICKED_ACTION)) {
Intent activityIntent = new Intent(Intent.ACTION_VIEW);
activityIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
activityIntent.setClass(context, Downloads.class); // Downloads Activity
try {
context.startActivity(activityIntent);
} catch (ActivityNotFoundException ex) {
Log.d(TAG, "no activity for Downloads.NOTIFICATION_CLICKED_ACTION" + ex);
}
} else if (intent.getAction().equals(Downloads.DOWNLOAD_COMPLETED_ACTION)) {
// balabala
}
}
}
By here, we need to modify the AndroidManifest.xml, add:
<receiver
android:name=".DownloadReceiver"
android:permission="android.permission.SEND_DOWNLOAD_COMPLETED_INTENTS"
android:exported="true" >
<intent-filter>
<action
android:name="android.intent.action.DOWNLOAD_COMPLETED">
</action>
</intent-filter>
<intent-filter>
<action
android:name="android.intent.action.DOWNLOAD_NOTIFICATION_CLICKED">
</action>
<category
android:name="android.intent.category.DEFAULT">
</category>
<data
android:scheme="content">
</data>
</intent-filter>
</receiver>
Maybe, you don’t like show the the Download state in status bar, just change Downloads.VISIBILITY_VISIBLE to Downloads.VISIBILITY_HIDDEN
and even, maybe you would like to show the download progress in the Activity, little case.
Cursor c = getContentResolver().query(
Downloads.CONTENT_URI, new String[] {
Downloads._ID,
Downloads.CURRENT_BYTES,
Downloads.TOTAL_BYTES,
Downloads.STATUS },
" " + Downloads.MIMETYPE + " = 'image/jpeg'", null,
Downloads._ID);
if (c == null) {
return;
}
// Columns match projection in query above
final int idColumn = 0;
final int currentBytesColumn = 1;
final int totalBytesColumn = 2;
final int statusColumn = 3;
c.moveToFirst();
long totalBytes = c.getLong(totalBytesColumn);
long currentBytes = c.getLong(currentBytesColumn);
c.close();
Add a ProgressBar and a Handler to display and refresh the progress, I suppose you should know how to do. You also can write a class extends ContentObserver to observer the download status.
The last step, delete the history data:
getContentResolver().delete(Downloads.CONTENT_URI,
"(" + Downloads.TITLE + " = 'screenshot')", null);
This code snippet should be write into a function in really code.
Read the code packages/apps/Browser for a more complete example.
Update:Since Android 2.2, the Downloads’s api has changed, they put some variables input a sub class which named Impl, so you should check it in the source code.
相关推荐
1. If you used MLO installer to install the program use "Add or Remove Programs" from your Windows Control Panel to completely uninstall MLO. 2. If you used zip archive to install MLO, simply delete...
For detailed information on how to configure a test server environment using a variety of operating systems and web servers, see "Local server setup" (http://drupal.org/node/157602) in the Drupal ...
Applications can use the install attribute in the shortcut element to specify their desire to be installed. Installed applications are not removed when the Java Web Start cache is cleared, but can be ...
漫画作品与时间旅行题材
Spring Boot特点: 1、创建一个单独的Spring应用程序; 2、嵌入式Tomcat,无需部署WAR文件; 3、简化Maven配置; 4、自动配置Spring; 5、提供生产就绪功能,如指标,健康检查和外部配置; 6、绝对没有代码生成和XML的配置要求;第一章 绪 论 1 1.1背景及意义 1 1.2国内外研究概况 2 1.3 研究的内容 2 第二章 关键技术的研究 3 2.1 相关技术 3 2.2 Java技术 3 2.3 ECLIPSE 开发环境 4 2.4 Tomcat介绍 4 2.5 Spring Boot框架 5 第三章 系统分析 5 3.1 系统设计目标 6 3.2 系统可行性分析 6 3.3 系统功能分析和描述 7 3.4系统UML用例分析 8 3.4.1管理员用例 9 3.4.2用户用例 9 3.5系统流程分析 10 3.5.1添加信息流程 11 3.5.2操作流程 12 3.5.3删除信息流程 13 第四章 系统设计 14 4.1 系统体系结构 15 4.2 数据库设计原则 16 4.3 数据表 17 第五章 系统实现 18 5.1用户功能模块 18 5.2
内容概要:本文作为PyTorch的入门指南,首先介绍了PyTorch相较于TensorFlow的优势——动态计算图、自动微分和丰富API。接着讲解了环境搭建、PyTorch核心组件如张量(Tensor)、autograd模块以及神经网络的定义方式(如nn.Module),并且给出了详细的神经网络训练流程,包括前向传播、计算损失值、进行反向传播以计算梯度,最终调整权重参数。此外还简要提及了一些拓展资源以便进一步探索这个深度学习工具。 适用人群:初次接触深度学习技术的新学者和技术爱好者,有一定程序基础并希望通过PyTorch深入理解机器学习算法实现的人。 使用场景及目标:该文档有助于建立使用者对于深度学习及其具体实践有更加直观的理解,在完成本教程之后,读者应当能够在个人设备上正确部署Python环境,并依据指示独立创建自己的简易深度学习项目。 其他说明:文中所提及的所有示例均可被完整重现,同时官方提供的资料链接也可以方便有兴趣的人士对感兴趣之处继续挖掘,这不仅加深了对PyTorch本身的熟悉程度,也为未来的研究或者工程项目打下了良好的理论基础和实践经验。
古镇美食自驾游:舌尖上的历史韵味
1.版本:matlab2014/2019a/2024a 2.附赠案例数据可直接运行matlab程序。 3.代码特点:参数化编程、参数可方便更改、代码编程思路清晰、注释明细。 4.适用对象:计算机,电子信息工程、数学等专业的大学生课程设计、期末大作业和毕业设计。
漫画作品与神话传说融合
1.版本:matlab2014/2019a/2024a 2.附赠案例数据可直接运行matlab程序。 3.代码特点:参数化编程、参数可方便更改、代码编程思路清晰、注释明细。 4.适用对象:计算机,电子信息工程、数学等专业的大学生课程设计、期末大作业和毕业设计。
ADC推理软件AI程序
漫画作品与科幻元素融合
1.版本:matlab2014/2019a/2024a 2.附赠案例数据可直接运行matlab程序。 3.代码特点:参数化编程、参数可方便更改、代码编程思路清晰、注释明细。 4.适用对象:计算机,电子信息工程、数学等专业的大学生课程设计、期末大作业和毕业设计。
1.版本:matlab2014/2019a/2024a 2.附赠案例数据可直接运行matlab程序。 3.代码特点:参数化编程、参数可方便更改、代码编程思路清晰、注释明细。 4.适用对象:计算机,电子信息工程、数学等专业的大学生课程设计、期末大作业和毕业设计。
1.版本:matlab2014/2019a/2024a 2.附赠案例数据可直接运行matlab程序。 3.代码特点:参数化编程、参数可方便更改、代码编程思路清晰、注释明细。 4.适用对象:计算机,电子信息工程、数学等专业的大学生课程设计、期末大作业和毕业设计。
1.版本:matlab2014/2019a/2024a 2.附赠案例数据可直接运行matlab程序。 3.代码特点:参数化编程、参数可方便更改、代码编程思路清晰、注释明细。 4.适用对象:计算机,电子信息工程、数学等专业的大学生课程设计、期末大作业和毕业设计。
java-springboot+vue景区民宿预约系统实现源码(完整前后端+mysql+说明文档+LunW+PPT).zip
在智慧城市建设的大潮中,智慧园区作为其中的璀璨明珠,正以其独特的魅力引领着产业园区的新一轮变革。想象一下,一个集绿色、高端、智能、创新于一体的未来园区,它不仅融合了科技研发、商业居住、办公文创等多种功能,更通过深度应用信息技术,实现了从传统到智慧的华丽转身。 智慧园区通过“四化”建设——即园区运营精细化、园区体验智能化、园区服务专业化和园区设施信息化,彻底颠覆了传统园区的管理模式。在这里,基础设施的数据收集与分析让管理变得更加主动和高效,从温湿度监控到烟雾报警,从消防水箱液位监测到消防栓防盗水装置,每一处细节都彰显着智能的力量。而远程抄表、空调和变配电的智能化管控,更是在节能降耗的同时,极大地提升了园区的运维效率。更令人兴奋的是,通过智慧监控、人流统计和自动访客系统等高科技手段,园区的安全防范能力得到了质的飞跃,让每一位入驻企业和个人都能享受到“拎包入住”般的便捷与安心。 更令人瞩目的是,智慧园区还构建了集信息服务、企业服务、物业服务于一体的综合服务体系。无论是通过园区门户进行信息查询、投诉反馈,还是享受便捷的电商服务、法律咨询和融资支持,亦或是利用云ERP和云OA系统提升企业的管理水平和运营效率,智慧园区都以其全面、专业、高效的服务,为企业的发展插上了腾飞的翅膀。而这一切的背后,是大数据、云计算、人工智能等前沿技术的深度融合与应用,它们如同智慧的大脑,让园区的管理和服务变得更加聪明、更加贴心。走进智慧园区,就像踏入了一个充满无限可能的未来世界,这里不仅有科技的魅力,更有生活的温度,让人不禁对未来充满了无限的憧憬与期待。
边境自驾游异国风情深度体验
在智慧城市建设的大潮中,智慧园区作为其中的璀璨明珠,正以其独特的魅力引领着产业园区的新一轮变革。想象一下,一个集绿色、高端、智能、创新于一体的未来园区,它不仅融合了科技研发、商业居住、办公文创等多种功能,更通过深度应用信息技术,实现了从传统到智慧的华丽转身。 智慧园区通过“四化”建设——即园区运营精细化、园区体验智能化、园区服务专业化和园区设施信息化,彻底颠覆了传统园区的管理模式。在这里,基础设施的数据收集与分析让管理变得更加主动和高效,从温湿度监控到烟雾报警,从消防水箱液位监测到消防栓防盗水装置,每一处细节都彰显着智能的力量。而远程抄表、空调和变配电的智能化管控,更是在节能降耗的同时,极大地提升了园区的运维效率。更令人兴奋的是,通过智慧监控、人流统计和自动访客系统等高科技手段,园区的安全防范能力得到了质的飞跃,让每一位入驻企业和个人都能享受到“拎包入住”般的便捷与安心。 更令人瞩目的是,智慧园区还构建了集信息服务、企业服务、物业服务于一体的综合服务体系。无论是通过园区门户进行信息查询、投诉反馈,还是享受便捷的电商服务、法律咨询和融资支持,亦或是利用云ERP和云OA系统提升企业的管理水平和运营效率,智慧园区都以其全面、专业、高效的服务,为企业的发展插上了腾飞的翅膀。而这一切的背后,是大数据、云计算、人工智能等前沿技术的深度融合与应用,它们如同智慧的大脑,让园区的管理和服务变得更加聪明、更加贴心。走进智慧园区,就像踏入了一个充满无限可能的未来世界,这里不仅有科技的魅力,更有生活的温度,让人不禁对未来充满了无限的憧憬与期待。