/**
 * 文件名 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. operation 多线程

    2.Cocoa Operation 优点:不需要关心线程管理,数据同步的事情.Cocoa Operation 相关的类是 NSOperation ,NSOperationQueue.NSOperati ...

  2. HTML5 display:inline、block、inline-block的区别--备用

    display:block就是将元素显示为块级元素. block元素的特点是: 总是在新行上开始: 高度,行高以及顶和底边距都可控制: 宽度缺省是它的容器的100%,除非设定一个宽度 <div& ...

  3. JS论坛地址备忘

    论坛: 百度JS吧 CSDN论坛JS版 AS天地会JS区 编程论坛JS版 开源中国JS分享 开源中国JS提问区 德问 JS堂 CSS-JS前端论坛 蓝色理想 ITEYE php100 phpspeak ...

  4. Json传递后台数据的问题

    在后台我有两个类: public Class Person { private String name; private Address address;//一个自定义的类 //getter和sett ...

  5. 工作那些事(二)应聘时填写个人信息ABCD

    先看看都有那些: 公司A: 填写来访人员登记表(在前台的那种),内容包括: 姓名.时间.电话.职位. 公司B: 填写来访人员登记表(在前台的那种),内容包括: 姓名.时间.电话.身份证号码().事由( ...

  6. HDOJ(HDU) 2192 MagicBuilding(用Java的Map做了下)

    Problem Description As the increase of population, the living space for people is becoming smaller a ...

  7. HDOJ 2015 偶数求和

    Problem Description 有一个长度为n(n<=100)的数列,该数列定义为从2开始的递增有序偶数,现在要求你按照顺序每m个数求出一个平均值,如果最后不足m个,则以实际数量求平均值 ...

  8. HDOJ 3466 Proud Merchants

    Problem Description Recently, iSea went to an ancient country. For such a long time, it was the most ...

  9. Android公共库——图片缓存 网络缓存 下拉及底部更多ListView 公共类

    Android公共库——图片缓存 网络缓存 下拉及底部更多ListView 公共类 转载自http://www.trinea.cn/android/android-common-lib/ 介绍总结的一 ...

  10. cf702D Road to Post Office

    D. Road to Post Office time limit per test 1 second memory limit per test 256 megabytes input standa ...