使用downloadmanager调用系统的下载
/**
* 文件名 UpdateDownload.java
* 包含类名列表 com.issmobile.numlibrary.tool
* 版本信息 版本号
* 创建日期 2014年7月14日
* 版权声明
*/
package com.issmobile.numlibrary.tool;
import com.iss.utils.LogUtil;
import android.annotation.SuppressLint;
import android.app.DownloadManager;
import android.app.DownloadManager.Query;
import android.app.DownloadManager.Request;
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.widget.Toast;
/**
* 类名
* @author 王洪贺<br/>
* 调用系统下载功能下载程序安装包
* 创建日期 2014年7月14日
*/
public class UpdateDownload {
static final String TAG = "UpdateDownload";
/**上下文*/
Context mContext;
/**下载管理器*/
DownloadManager manager;
/**下载完成监听器*/
DownloadCompleteReceiver receiver;
public UpdateDownload(Context context) {
this.mContext = context;
}
@SuppressLint("NewApi")
public void DownloadFile(String url) {
url = "http://dl.google.com/android/ADT-12.0.0.ZIP";
//获取下载服务
manager = (DownloadManager) mContext.getSystemService(mContext.DOWNLOAD_SERVICE);
receiver = new DownloadCompleteReceiver();
//创建下载请求
Request down = new DownloadManager.Request(Uri.parse(url));
//设置允许使用的网络类型,这里是移动网络和wifi都可以
down.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_MOBILE
| DownloadManager.Request.NETWORK_WIFI);
//禁止发出通知,既后台下载
// down.setShowRunningNotification(false);
//不显示下载界面
// down.setVisibleInDownloadsUi(false);
//设置下载后文件存放的位置
down.setDestinationInExternalFilesDir(mContext, null, "numlibrary.apk");
//将下载请求放入队列
manager.enqueue(down);
}
/**
*注册监听器
*/
public void registReceiver() {
mContext.registerReceiver(receiver, new IntentFilter(
DownloadManager.ACTION_DOWNLOAD_COMPLETE));
}
/**
*取消监听器
*/
public void unRegistReceiver() {
if (receiver != null)
mContext.unregisterReceiver(receiver);
}
/**
* 安装APK文件
*/
private void installApk(String path) {
Intent i = new Intent(Intent.ACTION_VIEW);
//TODO 得到文件的路径
i.setDataAndType(Uri.parse("file://" + path), "application/vnd.android.package-archive");
mContext.startActivity(i);
}
//接受下载完成后的intent
class DownloadCompleteReceiver extends BroadcastReceiver {
private DownloadManager downloadManager;
@SuppressLint("NewApi")
@Override
public void onReceive(Context context, Intent intent) {
// if (intent.getAction().equals(DownloadManager.ACTION_DOWNLOAD_COMPLETE)) {
// long downId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);
// Log.v(TAG, " download complete! id : " + downId);
// //TODO 传入文件的保存路径并打开进行安装
// installApk("");
// Toast.makeText(context, intent.getAction() + "id : " + downId, Toast.LENGTH_SHORT)
// .show();
// }
String action = intent.getAction();
if (action.equals(DownloadManager.ACTION_DOWNLOAD_COMPLETE)) {
Toast.makeText(context, "下载完成了....", Toast.LENGTH_LONG).show();
//TODO 判断这个id与之前的id是否相等,如果相等说明是之前的那个要下载的文件
long id = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, 0);
Query query = new Query();
query.setFilterById(id);
downloadManager = (DownloadManager) context
.getSystemService(Context.DOWNLOAD_SERVICE);
Cursor cursor = downloadManager.query(query);
int columnCount = cursor.getColumnCount();
//TODO 这里把所有的列都打印一下,有什么需求,就怎么处理,文件的本地路径就是path
String path = null;
while (cursor.moveToNext()) {
for (int j = 0; j < columnCount; j++) {
String columnName = cursor.getColumnName(j);
String string = cursor.getString(j);
if (columnName.equals("local_uri")) {
path = string;
}
if (string != null) {
LogUtil.d(TAG, columnName + ": " + string);
} else {
LogUtil.d(TAG, columnName + ": null");
}
}
}
cursor.close();
//如果sdcard不可用时下载下来的文件,那么这里将是一个内容提供者的路径,这里打印出来,有什么需求就怎么样处理 if(path.startsWith("content:")) {
cursor = context.getContentResolver()
.query(Uri.parse(path), null, null, null, null);
columnCount = cursor.getColumnCount();
while (cursor.moveToNext()) {
for (int j = 0; j < columnCount; j++) {
String columnName = cursor.getColumnName(j);
String string = cursor.getString(j);
if (string != null) {
LogUtil.d(TAG, columnName + ": " + string);
} else {
LogUtil.d(TAG, columnName + ": null");
}
}
}
cursor.close();
} else if (action.equals(DownloadManager.ACTION_NOTIFICATION_CLICKED)) {
Toast.makeText(mContext, "....", Toast.LENGTH_LONG).show();
}
}
}
}
使用downloadmanager调用系统的下载的更多相关文章
- Android下载图片/调用系统相机拍照、显示并保存到本地
package com.example.testhttpget; import java.io.BufferedReader; import java.io.FileNotFoundException ...
- 关于Android 7.0更新后调用系统相机及电筒问题
android升级到7.0后对权限又做了一个更新即不允许出现以file://的形式调用隐式APP,需要用共享文件的形式:content:// URI 因为系统相机是提供的共享 Provider , C ...
- Android调用系统相机功能
在常规应用开发过程中,我们经常会使用到手机的相机功能,通过调用系统相机方便快捷的帮助我们实现拍照功能,本篇我将带领大家实现一下,如何通过调用系统相机实现拍照. 第一种:调用系统相机拍照,通过返回的照片 ...
- ImageLoader框架的使用、调用系统相册显示图片并裁剪显示、保存图片的两种方式
ImageLoader虽然说是一个相对于比较老的一个框架了 ,但是总的来说,还是比较好用的,今天我就总结了一下它的用法.还有调用系统相册并裁剪,以及,通过sharedpreference和文件存储来保 ...
- iOS程序中调用系统自带应用(短信,邮件,浏览器,地图,appstore,拨打电话,iTunes,iBooks )
在网上找到了下在记录下来以后方便用 在程序中调用系统自带的应用,比如我进入程序的时候,希望直接调用safar来打开一个网页,下面是一个简单的使用:
- IOS中调用系统的电话、短信、邮件、浏览功能
iOS开发系列--通讯录.蓝牙.内购.GameCenter.iCloud.Passbook系统服务开发汇总 2015-01-13 09:16 by KenshinCui, 26990 阅读, 35 评 ...
- Android调用系统相机、自己定义相机、处理大图片
Android调用系统相机和自己定义相机实例 本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,而且因为涉及到要把拍到的照片显示出来,该样例也会涉及到Android载入大图片时候的处 ...
- C#后台调用浏览器打开下载连接地址的三种方法
一.从注册表中读取到本地计算机默认浏览器,然后调用下载. private void button1_Click(object sender, EventArgs e) { //从注册表 ...
- ShareIntentUtil【调用系统自带的分享的工具类】
版权声明:本文为HaiyuKing原创文章,转载请注明出处! 前言 根据参考资料的文章,整理了调用系统自带分享的工具类(实现了适配7.0FileProvider的功能),需要搭配<Android ...
随机推荐
- 技术大牛面试 http://www.itmian4.com/forum.php?mod=viewthread&tid=3824
不久前,byvoid面阿里星计划的面试结果截图泄漏,引起无数IT屌丝的羡慕敬仰.看看这些牛人,NOI金牌,开源社区名人,三年级开始写Basic...在跪拜之余我们不禁要想,和这些牛人比,作为绝大部分技 ...
- CSS远程加载字体
CSS 远程加载字体的方法,做网站CSS的都知道,用户浏览网站时,网页上的字体是加载本地的.换言之,如果网站使用了用户电脑所没有安装的字体,那显示字体就会被默认字体所代替了,自然效果就大受影响了. 上 ...
- 得到某个进程所有线程ID和入口地址
#include <windows.h> #include <tlhelp32.h> #include "iostream" using namespace ...
- NET Core 静态文件及JS包管理器(npm, Bower)的使用
NET Core 静态文件及JS包管理器(npm, Bower)的使用 文章目录 在 ASP.NET Core 中添加静态文件 使用npm管理JavaScript包 使用Bower管理JavaScri ...
- jquery如何判断div是否隐藏--useful
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- UNIX Filesystems - Evolution Design and Implementation.pdf
UNIX Filesystems - Evolution Design and Implementation.pdf
- Catenyms
poj2337:http://poj.org/problem?id=2337 题意:给定一些单词,如果一个单词的尾字母与另一个的首字母相同则可以连接.问是否可以每个单词用一次,将所有单词连接,可以则输 ...
- Mysql 数字类型转换函数
.将Int 转为varchar经常用 concat函数,比如concat(,' .将varchar 转为Int 用 cast(a as signed) a为varchar类型的字符串 总结:类型转换和 ...
- COJ 2110 Day7-例3
Day7-例3 难度级别:C: 运行时间限制:5000ms: 运行空间限制:256000KB: 代码长度限制:2000000B 试题描述 输入 输入的第一行包含整数n和k,其中n(2 ≤ n ≤100 ...
- BZOJ1631: [Usaco2007 Feb]Cow Party
1631: [Usaco2007 Feb]Cow Party Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 459 Solved: 338[Submit ...