前言
在Android开发中经常会用到将一个文件下载到本地存储卡,这个过程看起来很容易,就是一个网络请求过程而已,但是有的时候我们需要把下载的内容存储到一个可以公共访问的位置,供其他应用共享,这个时候我们可以使用Android官方提供的一个DownloadManage来很方便的实现。事实上DownloadManage自API 9就已经存在了,中间官方只做了一些小的调整。
下载文件
下面通过一个下载图片的小Demo来一步步理解DownloadManager的使用和涉及到的一些内在知识,类的基本关系如下。
Demo结构
接下来开始一个简单的UI实现,放一个按钮并添加点击事件。
1
2
3
4
5
6
7
8
9
10
11
12
13
| <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/download"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/download"
android:layout_centerInParent="true"/>
</RelativeLayout>
|
阅读更多