/**
 * 文件名 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调用系统的下载的更多相关文章

  1. Android下载图片/调用系统相机拍照、显示并保存到本地

    package com.example.testhttpget; import java.io.BufferedReader; import java.io.FileNotFoundException ...

  2. 关于Android 7.0更新后调用系统相机及电筒问题

    android升级到7.0后对权限又做了一个更新即不允许出现以file://的形式调用隐式APP,需要用共享文件的形式:content:// URI 因为系统相机是提供的共享 Provider , C ...

  3. Android调用系统相机功能

    在常规应用开发过程中,我们经常会使用到手机的相机功能,通过调用系统相机方便快捷的帮助我们实现拍照功能,本篇我将带领大家实现一下,如何通过调用系统相机实现拍照. 第一种:调用系统相机拍照,通过返回的照片 ...

  4. ImageLoader框架的使用、调用系统相册显示图片并裁剪显示、保存图片的两种方式

    ImageLoader虽然说是一个相对于比较老的一个框架了 ,但是总的来说,还是比较好用的,今天我就总结了一下它的用法.还有调用系统相册并裁剪,以及,通过sharedpreference和文件存储来保 ...

  5. iOS程序中调用系统自带应用(短信,邮件,浏览器,地图,appstore,拨打电话,iTunes,iBooks )

    在网上找到了下在记录下来以后方便用 在程序中调用系统自带的应用,比如我进入程序的时候,希望直接调用safar来打开一个网页,下面是一个简单的使用:

  6. IOS中调用系统的电话、短信、邮件、浏览功能

    iOS开发系列--通讯录.蓝牙.内购.GameCenter.iCloud.Passbook系统服务开发汇总 2015-01-13 09:16 by KenshinCui, 26990 阅读, 35 评 ...

  7. Android调用系统相机、自己定义相机、处理大图片

    Android调用系统相机和自己定义相机实例 本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,而且因为涉及到要把拍到的照片显示出来,该样例也会涉及到Android载入大图片时候的处 ...

  8. C#后台调用浏览器打开下载连接地址的三种方法

    一.从注册表中读取到本地计算机默认浏览器,然后调用下载. private void button1_Click(object sender, EventArgs e)   {       //从注册表 ...

  9. ShareIntentUtil【调用系统自带的分享的工具类】

    版权声明:本文为HaiyuKing原创文章,转载请注明出处! 前言 根据参考资料的文章,整理了调用系统自带分享的工具类(实现了适配7.0FileProvider的功能),需要搭配<Android ...

随机推荐

  1. PHP-HTML重要知识点笔记

    1.用frameset.frame和iframe还实现多窗口 2.在图片上利用映射距离usemap来实现按钮跳转.------第8尾集 3.表单必须要有name和value,因为抓包的时候,可发现必须 ...

  2. 简明解释算法中的大O符号

    伯乐在线导读:2009年1月28日Arec Barrwin在StackOverflow上提问,“有没有关于大O符号(Big O notation)的简单解释?尽量别用那么正式的定义,用尽可能简单的数学 ...

  3. 转:《IIC时序》

    I2C(Inter-Integrated Circuit)总线是一种由PHILIPS公司开发的两线式串行总线,用于连接微控制器及其外围设备.I2C总线产生于在80年代,最初为音频和视频设备开发,如今主 ...

  4. c++ 顺序容器学习 - 容器适配器

    摘要: 对 容器适配器 的疑问. 刚开始接触 容器适配器 时,总感觉怪怪的,认为多此一举,顺手搜了搜,原来我在这一点is not alone: STL容器适配器的用途 其中有个老兄说的好,这里 引用一 ...

  5. SharePoint REST api

    http://msdn.microsoft.com/en-us/magazine/dn198245.aspx Understanding and Using the SharePoint 2013 R ...

  6. unity3d 场景间数据传递

    在游戏项目中,常常会使用到用户信息,获取信息当然可以从数据库中获取.但是对场景多的游戏这样做是不正确的,那么我我们就需要再第一次获取用户信息之后, 同时在其它的场景中共享用户数据,避免对服务器增加负担 ...

  7. 通过预编译头文件来提高C++ Builder的编译速度

    C++ Builder是最快的C++编译器之一,从编译速度来说也可以说是最快的win32C++编译器了.除了速度之外,C++builder的性能也在其它C++编译器的之上,但许多Delphi程序员仍受 ...

  8. 牛逼的bootcss之buttons

    css源码 /*! @license * * Buttons * Copyright 2012-2014 Alex Wolfe and Rob Levin * * Licensed under the ...

  9. Wall(Graham算法)

    Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 27110   Accepted: 9045 Description Once ...

  10. HDOJ 2027 统计元音

    Problem Description 统计每个元音字母在字符串中出现的次数. Input 输入数据首先包括一个整数n,表示测试实例的个数,然后是n行长度不超过100的字符串. Output 对于每个 ...