使用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 ...
随机推荐
- js获取当前时间戳与日期比较
如何用javascript获取当前时间戳: 复制代码 代码示例: 方法1: var timestamp = date.parse(new date()); 结果:1280977330000 方法2: ...
- Python Tutorial 学习(九)--Classes
## 9. Classes 类 Compared with other programming languages, Python's class mechanism adds classes wit ...
- elasticsearch 性能优化
#系统默认的最大打开文件数的限制 vi /etc/security/limits.conf * - nproc 50240 * - ...
- C++类型转换运算符
C++中提供4中类型转运算符,分别是:static_cast.dynamic_cast.reinterpret_cast和const_cast; 语法格式如下: 类型转换运算符 < type_i ...
- BZOJ 1020 安全的航线flight
Description 在设计航线的时候,安全是一个很重要的问题.首先,最重要的是应采取一切措施确保飞行不会发生任何事故,但同时也需要做好最坏的打算,一旦事故发生,就要确保乘客有尽量高的生还几率.当飞 ...
- [BZOJ 1070] [SCOI2007] 修车 【费用流】
题目链接:BZOJ - 1070 题目分析 首先想到拆点,把每个技术人员拆成 n 个点,从某个技术人员拆出的第 i 个点,向某辆车连边,表示这是这个技术人员修的倒数第 i 辆车.那么这一次修车对整个答 ...
- cf B. Number Busters
http://codeforces.com/contest/382/problem/B 题意:给你Aa,b,w,x,c,然后每经过1秒,c=c-1; 如果b>=x,b=b-x;否则 a=a-1 ...
- C语言宏定义使用技巧
写好C语言,漂亮的宏定义很重要,使用宏定义可以防止出错,提高可移植性,可读性,方便性 等等.下面列举一些成熟软件中常用得宏定义...... 1.防止一个头文件被重复包含 #ifndef COMDEF_ ...
- Struts 2 标签
注:要使用Strust 2标签需<%@ taglib prefix="s" uri="/struts-tags" %> 表单标签: .form标签 ...
- netstat详解
Netstat 命令用于显示各种网络相关信息,如网络连接,路由表,接口状态 (Interface Statistics),masquerade 连接,多播成员 (Multicast Membershi ...