码迷,mamicode.com
首页 > 其他好文 > 详细

使用downloadmanager调用系统的下载

时间:2014-07-14 20:50:45      阅读:257      评论:0      收藏:0      [点我收藏+]

标签:android   des   http   java   使用   文件   

/**
 * 文件名 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调用系统的下载,布布扣,bubuko.com

使用downloadmanager调用系统的下载

标签:android   des   http   java   使用   文件   

原文地址:http://www.cnblogs.com/dongweiq/p/3842639.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!