/**
 * 文件名 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. 基于Qt QGraphicsView的多点触摸绘图

    本应用于基于QGraphicsView框架,实现多点触摸. 工程仅仅演示了多点触摸绘图,源自我前段时间一款基于Qt的绘图软件. 工程结构: kmp.h 定义了枚举 slide.h/cpp 定义了派生于 ...

  2. frame,bounds,center-三者的含义

    frame与bounds的区别比较 frame,bounds,center-三者的含义 偶然觉的,这三个属性有时候定位的时候,需要用.于是就来搞清楚,到底frame,bounds,center 这三个 ...

  3. iOS开发之——巧用反射机制

    1.应用场景——自定义UITabBarController的TabBar视图 (1)隐藏TabBar视图 一般我们选择自定义TabBar视图有两种方式.1是将tabBar视图隐藏;2是将TabBar视 ...

  4. 转:Github上最受关注的前端大牛,快来膜拜吧!

    原文来自于:http://code.csdn.net/news/2820990 本文列出了Github上最受关注的10位前端大牛.看看他们负责的项目和提交的代码,你是不是能从中学到些什么? 1. Pa ...

  5. 45 Useful JavaScript Tips, Tricks and Best Practices(有用的JavaScript技巧,技巧和最佳实践)

    As you know, JavaScript is the number one programming language in the world, the language of the web ...

  6. 对ExtJS4应用 性能优化的几点建议

    ExtJS由于UI设计过去强悍,导致性能问题一直被大家诟病,不过到ExtJS4.1之后,性能问题相比以前的版本已有所改善,下面是官方文档给出的优化建议,李坏在此做个小小的总结,仅供大家参考. (1)合 ...

  7. 图论(生成树):HDU 5631Rikka with Graph

    Rikka with Graph Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

  8. iTerm2 + oh my zsh代替mac自带的bash shell

    使用Solarized dark配色方案 需要字体menlo for powerline oh-my-zsh主题使用agnoster,这个主题默认的路径是全路径,当路径很长的时候,就会占很长的空间,可 ...

  9. Summary Ranges —— LeetCode

    Given a sorted integer array without duplicates, return the summary of its ranges. For example, give ...

  10. 第一个ios程序

    1.ios的理解: Operating System,简称OS,操作系统,ios是苹果操作系统. 2.Xcode开发环境: 苹果公司开发的编程软件,是开发人员建立OS X 和 iOS 应用程序的最快捷 ...