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为演示项目 ...
随机推荐
- linux 命令——29 chgrp(转)
在 lunix系统里,文件或目录的权限的掌控以拥有者及所诉群组来管理.可以使用chgrp指令取变更文件与目录所属群组,这种方式采用群组名称或群组识别 码都可以.Chgrp命令就是change grou ...
- Kibana功能一览
Overview标签 总共32个请求,最大响应时间:4.7秒 Usage标签 可以看到HTTP请求的发起时间分布 Performance and Quality 6个请求里,响应时间在100毫秒以下的 ...
- JAVA设计模式初探之桥接模式
生活中的一个例子: 拿汽车在路上行驶的来说.既有小汽车又有公共汽车,它们都不但能在市区中的公路上行驶,也能在高速公路上行驶.这你会发现,对于交通工具(汽车)有不同的类型,它们所行驶的环境(路)也 ...
- Memory Usage Performance Guidelines
https://developer.apple.com/library/content/documentation/Performance/Conceptual/ManagingMemory/Arti ...
- flutter 踩坑总结
导入第三方库踩坑小结: (编译器:VsCode) ( 打算在学习中,使用flutter重新自己的项目,遇到比较特殊的坑,就先记录一下,持续更新中) 1.把第三方库 写入pubspec.yaml文件中 ...
- 18课 Vue第一节
Q1: url-loader必须搭载file-loader?Q2: 图片的打包问题,如果直接写在img标签里用src引用图片,该如何打包?Q3: 如何根据不同的页面html模板打包与之对应的css/j ...
- 多线程:InterlockedIncrement
1.InterlockedIncrement保护多线程中操作的整数. #include <stdio.h> #include <windows.h> volatile long ...
- 在React中使用Redux数据流
问题:数据流是什么呢?为什么要用数据流? 答案:1.数据流是我们的行为与相应的抽象 2.使用数据流帮助我们明确了行为的对应的响应 问题: React与数据流的关系 1.React是纯 V 层的前端框架 ...
- Vuex的简单了解
vuex的官网了解:https://vuex.vuejs.org/zh/guide/ 一.什么是vuex? Vuex 是一个专为 Vue.js 应用程序开发的状态管理模式.它采用集中式存储管理应用的所 ...
- 三十三、MySQL 导入数据
MySQL 导入数据 本章节我们为大家介绍几种简单的 MySQL 导出的数据的命令. 1.mysql 命令导入 使用 mysql 命令导入语法格式为: mysql -u用户名 -p密码 < 要导 ...