DownloadManager的使用
DownloadManager是系统开放给第三方应用使用的类,包含两个静态内部类DownloadManager.Query和DownloadManager.Request。DownloadManager.Request用来请求一个下载,DownloadManager.Query用来查询下载信息。具体使用代码如下:
package com.example.administrator.mystudent.downloadManager; import android.app.Activity;
import android.app.DownloadManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.Cursor;
import android.net.Uri;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast; import com.example.administrator.mystudent.R; import java.util.HashMap;
import java.util.Map; public class DownLoadActivity extends Activity {
private Button starButton;
private Button stopButton;
private Button dirButton;
private TextView infoText; private DownloadManager downloader;
private Uri uri;
private long reference; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_down_load); initView();
} private void initView() {
starButton = (Button) findViewById(R.id.star);
stopButton = (Button) findViewById(R.id.stop);
dirButton = (Button) findViewById(R.id.dir);
infoText= (TextView) findViewById(R.id.myText); starButton.setOnClickListener(new myButtonListener());
stopButton.setOnClickListener(new myButtonListener());
dirButton.setOnClickListener(new myButtonListener());
} class myButtonListener implements View.OnClickListener {
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.star: {
//执行下载任务
downLoadManager();
}
break;
case R.id.stop: {
//停止下载
downloader.remove(reference);
}
break;
case R.id.dir: {
//获取存放地址
Map sDir=getMyFilesDir();
infoText.setText("downid:"+sDir.get("downid")
+"title:"+sDir.get("title")
+"statuss:"+sDir.get("statuss")
+"address:"+sDir.get("address")
+"status:"+sDir.get("status")
);
}
break;
}
}
} /**
* 获取下载的文件存贮路径
* @return 文件路径
*/
private Map getMyFilesDir() {
Map<String, String> map = null;
//创建查询对象
DownloadManager.Query query=new DownloadManager.Query();
//根据任务编号查询下载任务信息
query.setFilterById(reference); Cursor cursor=downloader.query(query);
while (cursor.moveToNext()){
String downId= cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_ID)); //下载文件的id
String title = cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_TITLE)); //下载文件的题目
String address = cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI)); //下载的地址
String statuss = cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS)); //状态
String size= cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR)); //大小
String sizeTotal = cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES)); //总大小 map = new HashMap<String, String>();
map.put("downid", downId);
map.put("title", title);
map.put("statuss", statuss);
map.put("address", address);
map.put("status", sizeTotal+":"+size);
}
cursor.close();
return map;
} /**
* DownloadManager的使用
*/
private void downLoadManager() {
//创建downLoadManager的管理器
downloader = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
//创建一个URI
// uri = Uri.parse("http://biz.10039shop.com/download/leyt/leyt.apk");
uri = Uri.parse("http://p1.ifengimg.com/a/2017_09/75dd25f878e8715_size66_w600_h363.jpg");
//封装一个request对象
DownloadManager.Request request = new DownloadManager.Request(uri);
//设置文件必须在WIFI下下载
request.setAllowedNetworkTypes(request.NETWORK_WIFI); //用于设置下载时时候在状态栏显示通知信息
request.setNotificationVisibility(request.VISIBILITY_VISIBLE);
//设置通知栏标题
request.setTitle("百度下载");
//设置Notification的message信息
request.setDescription("图片正在下载");
//用于设置漫游状态下是否可以下载
request.setAllowedOverRoaming(false);
//设置文件存放目录
request.setDestinationInExternalFilesDir(this, Environment.DIRECTORY_DOWNLOADS, "myDownLoad"); //发送request请求并返回一个下载ID(开始下载了)
reference = downloader.enqueue(request);
Log.e("开始下载", "下载的id是" + reference); //创建一个下载的广播,下载完成之后
IntentFilter intentFilter = new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE); BroadcastReceiver myReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
long referenceTo = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);
if (referenceTo == reference) {
Log.e("结束下载", "下载完成" + referenceTo);
Toast.makeText(getApplicationContext(), "文件下载完成", Toast.LENGTH_SHORT).show();
}
}
};
//注册广播
registerReceiver(myReceiver, intentFilter);
}
}
DownloadManager的使用的更多相关文章
- [Android Pro] 判断Uri对应的ContentProvider所操作的数据库u存在,及DownloadManager的暂停,继续
reference to : http://blog.csdn.net/u012858313/article/details/38821857 项目中遇到一个问题,就是用到DownloadManage ...
- app的自动更新(调用DownloadManager)
具体思路为:调用接口与服务器版本对比,当服务器版本号大于本地的,调用DownloadManager进行下载,之前也试过很多方法,但是兼容性都不是很好,还有一点要注意的是,在这里我并没有设置固定的下载路 ...
- DownloadManager
在androi中,volley适合小文件的获取和大并发,如果支持大文件的下载可以用Android原生的DownloadManager.DownloadManager默认支持多线程下载.断点续传等. 基 ...
- Android 使用 DownloadManager 管理系统下载任务的方法,android管理系统
从Android 2.3(API level 9)开始Android用系统服务(Service)的方式提供了Download Manager来优化处理长时间的下载操作.Download Manager ...
- DownloadManager补漏
原始完成于:2014-10-24 20:01:03 DownloadManager是一个处理HTTP下载请求的系统服务: 1. 基本用法 1 private void download() { 2 R ...
- DownloadManager 的使用
一.基本概念 1.DownloadManager是Android 2.3A (API level 9) 引入的,基于http协议,用于处理长时间下载. 2.DownloadManager对于断点 ...
- Android--调用系统的DownLoadManager去下载文件
代码里面有详细的注释: /** * 该方法是调用了系统的下载管理器 */ public void downLoadApk(Context context,String url){ /** * 在这里返 ...
- 使用downloadmanager调用系统的下载
/** * 文件名 UpdateDownload.java * 包含类名列表 com.issmobile.numlibrary.tool * 版本信息 版本号 * 创建日期 2014年7月14日 ...
- DownloadManager 下载管理类
演示 简介 从Android 2.3开始新增了一个下载管理类,在SDK的文档中我们查找android.app.DownloadManager可以看到.下载管理类可以长期处理多个HTTP下载任务,客户端 ...
- 安卓开发之非常好用的AndroidOne框架DownloadManager
AndroidOne框架是采用MVC模式,集成了Android主流开源技术及组件,是一款极速且简单高效开发框架,整个项目包含两个部分AndroidOne,oneCore AndroidOne为演示项目 ...
随机推荐
- codeforces 600D Area of Two Circles' Intersection
分相离,内含,想交三种情况讨论一下. 主要是精度和数据范围的问题,首先数据用long double,能用整型判断就不要用浮点型. 题目中所给的坐标,半径是整型的,出现卡浮点判断的情况还是比较少的. 最 ...
- 理顺react,flux,redux这些概念的关系
作者:北溟小鱼hk链接:https://www.zhihu.com/question/47686258/answer/107209140来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转 ...
- 2017年9月11日 梁勇 java教材 编程练习题 第二章 2.15 键盘 读取两个点的坐标值(小数),控制台输出两点间距离。
package com.swift; import java.util.Scanner; public class PToP { public static void main(String[] ar ...
- 第四篇、Swift_Podfile文件配置格式
# Uncomment this line to define a global platform for your project platform :ios, '9.0' # Comment th ...
- iOS 闭包传值 和 代理传值
let vc = ViewController() let navc = UINavigationController(rootViewController: vc) window = UIWindo ...
- LAMP 搭建练习
目录 LAMP 搭建 1:CentOS 7, lamp (module): http + php + phpMyAdmin + wordpress 192.168.1.7 配置虚拟主机 xcache ...
- tcl之内容
- php与js的crc32(支持中文)
代码: <?php function myCrc32($string, $crc = 0) { $table = "00000000 77073096 EE0E612C 990951B ...
- Roads in the North POJ - 2631
Roads in the North POJ - 2631 Building and maintaining roads among communities in the far North is a ...
- 并查集:HDU4496-D-City(倒用并查集)
D-City Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others) Total Submis ...