android webview downloadManager文件下载管理

一。downloadmanager类说明:html

从Android 2.3开始新增了一个下载管理类,在SDK的文档中咱们查找android.app.DownloadManager能够看到。下载管理类能够长期处理多个HTTP下载任务,客户端只须要给出请求的Uri和存放目标文件的位置便可,下载管理使用了一个AIDL服务器因此能够放心的在后台执行,同时实例化的方法须要使用getSystemService(Context.DOWNLOAD_SERVICE) ,Android123再次提醒使用API Level为9的用户能够轻松的经过新增的这个API实现Android平台上的文件下载操做。android

DownloadManager类提供了如下几种方法来处理,数据库

 

Java代码    long  enqueue(DownloadManager.Request request)   //存入队列一个新的下载项 
 
ParcelFileDescriptor  openDownloadedFile(long id)  //打开一个下载后的文件用于读取,参数中的long型id是一个provider中的一条记录。 
 
Cursor  query(DownloadManager.Query query)  //查询一个下载,返回一个Cursor 
 
int  remove(long... ids)  //取消下载同时移除这些条从下载管理中。 服务器

  咱们能够看到提供的方法都比较简单,给咱们操做的最终封装成为一个provider数据库的方式进行添加、查询和移除,可是对于查询和添加任务的细节,咱们要看看DownloadManager.Request类和DownloadManager.Query 类了。cookie

  1、DownloadManager.Request类的成员和定义
Java代码  DownloadManager.Request  addRequestHeader(String header, String value)  // 添加一个Http请求报头,对于这两个参数,Android开发网给你们举个小例子,好比说User-Agent值能够为Android123或 Windows XP等等了,主要是给服务器提供标识。 
DownloadManager.Request  setAllowedNetworkTypes(int flags)  //设置容许使用的网络类型,这一步Android 2.3作的很好,目前有两种定义分别为NETWORK_MOBILE和NETWORK_WIFI咱们能够选择使用移动网络或Wifi方式来下载。 
DownloadManager.Request  setAllowedOverRoaming(boolean allowed)  //对于下载,考虑到流量费用,这里是否容许使用漫游。 
DownloadManager.Request  setDescription(CharSequence description)  //设置一个描述信息,主要是最终显示的notification提示,能够随便写个本身区别 
DownloadManager.Request  setDestinationInExternalFilesDir(Context context, String dirType, String subPath)  //设置目标存储在外部目录,通常位置能够用 getExternalFilesDir()方法获取。 
DownloadManager.Request  setDestinationInExternalPublicDir(String dirType, String subPath)  //设置外部存储的公共目录,通常经过getExternalStoragePublicDirectory()方法获取。 
DownloadManager.Request  setDestinationUri(Uri uri)  //设置须要下载目标的Uri,能够是http、ftp等等了。 
DownloadManager.Request  setMimeType(String mimeType)  //设置mime类型,这里看服务器配置,通常国家化的都为utf-8编码。 
DownloadManager.Request  setShowRunningNotification(boolean show)  //是否显示下载进度的提示 
DownloadManager.Request  setTitle(CharSequence title)  //设置notification的标题 
DownloadManager.Request  setVisibleInDownloadsUi(boolean isVisible)  //设置下载管理类在处理过程当中的界面是否显示 网络

  固然了Google还提供了一个简单的方法来实例化本类,这个构造方法为DownloadManager.Request(Uri uri) ,咱们直接填写一个Uri便可,上面的设置使用默认状况。session

2、DownloadManager.Query类app

  对于当前下载内容的状态,咱们可使用DownloadManager.Query类来获取,本类比较简单,仅仅提供了两个方法。
Java代码  DownloadManager.Query  setFilterById(long... ids)  //根据id来过滤查找。 
DownloadManager.Query  setFilterByStatus(int flags) //根据任务的状态来查找。 ide

  详细的状态在android.app.DownloadManager类中有定义,目前Android 2.3中的定义为:编码

Java代码  int STATUS_FAILED 失败 
int STATUS_PAUSED 暂停 
int STATUS_PENDING 等待将开始 
int STATUS_RUNNING 正在处理中 
int STATUS_SUCCESSFUL 已经下载成功 

最后Android开发网提醒你们要说的是由于DownloadManager类提供的query方法返回一个Cursor对象,这些状态保存在这个游标的COLUMN_STATUS 字段中。

  1. 下载的状态完成均是以广播的形式通知你们,目前API Level为9定义了下面三种Intent的action
Java代码  ACTION_DOWNLOAD_COMPLETE下载完成的动做。 
ACTION_NOTIFICATION_CLICKED 当用户单击notification中下载管理的某项时触发。 
ACTION_VIEW_DOWNLOADS 查看下载项 

  2. 对于一个还没有完成的项,在Cursor中咱们查找COLUMN_REASON字段,可能有如下定义:
Java代码  int ERROR_CANNOT_RESUME 不可以继续,因为一些其余缘由。 
int ERROR_DEVICE_NOT_FOUND 外部存储设备没有找到,好比SD卡没有插入。 
int ERROR_FILE_ALREADY_EXISTS 要下载的文件已经存在了,Android123提示下载管理类是不会覆盖已经存在的文件,因此若是须要从新下载,请先删除之前的文件。 
 
int ERROR_FILE_ERROR 可能因为SD卡缘由致使了文件错误。 
int ERROR_HTTP_DATA_ERROR 在Http传输过程当中出现了问题。 
int ERROR_INSUFFICIENT_SPACE 因为SD卡空间不足形成的  
 
int ERROR_TOO_MANY_REDIRECTS 这个Http有太多的重定向,致使没法正常下载 
int ERROR_UNHANDLED_HTTP_CODE 没法获取http出错的缘由,好比说远程服务器没有响应。 
int ERROR_UNKNOWN 未知的错误类型. 

3. 有关暂停的一些状态,一样COLUMN_REASON字段的值多是如下定义
Java代码  int PAUSED_QUEUED_FOR_WIFI 因为移动网络数据问题,等待WiFi链接能用后再从新进入下载队列。 
int PAUSED_UNKNOWN 未知缘由致使了任务下载的暂停. 
int PAUSED_WAITING_FOR_NETWORK 可能因为没有网络链接而没法下载,等待有可用的网络链接恢复。. 
int PAUSED_WAITING_TO_RETRY 因为重重缘由致使下载暂停,等待重试。 

  有关Android 2.3中新增的下载管理DownloadManager的介绍基本上已经彻底讲完,若是你对Cursor、Provider这些基础概念了解的话,能够看到这个下载管理类能够帮咱们减小不少没必要要的代码编写

 


二。用downloadManager 下载:


    public void onDownloadStart(String url, String userAgent,
            String contentDisposition, String mimetype, long contentLength) {

        AppUtils.LogD(mimetype);

        // download file
        DownloadManager downloadManager = ((DownloadManager) activity
                .getSystemService(Activity.DOWNLOAD_SERVICE));
        Request request = new Request(Uri.parse(url));


        // set request header, 如session等 www.2cto.com
            request.addRequestHeader("Accept",
                    "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
            request.addRequestHeader("Accept-Language", "en-us,en;q=0.5");
            request.addRequestHeader("Accept-Encoding", "gzip, deflate");
            request.addRequestHeader("Accept-Charset",
                    "ISO-8859-1,utf-8;q=0.7,*;q=0.7");
            request.addRequestHeader("Cache-Control", "max-age=0");

            String host = "";
            try {
                host = new URL(url).getHost();
            } catch (MalformedURLException e) {
                e.printStackTrace();
            }

            String cookieStr = CookieManager.getInstance().getCookie(host);
            if (!AppUtils.isEmpty(cookieStr)) {
                request.addRequestHeader("Cookie", cookieStr + "; AcSe=0");
            }

            // generate filename dynamically
            String fileName = contentDisposition.replaceFirst(
                    "attachment; filename=", "");
            request.setDestinationInExternalPublicDir(
                    Environment.DIRECTORY_DOWNLOADS, fileName);

        downloadManager.enqueue(request);
    }

 


三。判断download finished:


        BroadcastReceiver receiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                String action = intent.getAction();
                if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) {
                    long downloadId = intent.getLongExtra(
                            DownloadManager.EXTRA_DOWNLOAD_ID, 0);
                    Query query = new Query();
                    query.setFilterById(enqueue);
                    Cursor c = dm.query(query);
                    if (c.moveToFirst()) {
                        int columnIndex = c
                                .getColumnIndex(DownloadManager.COLUMN_STATUS);
                        if (DownloadManager.STATUS_SUCCESSFUL == c
                                .getInt(columnIndex)) {
 
                            ImageView view = (ImageView) findViewById(R.id.imageView1);
                            String uriString = c
                                    .getString(c
                                            .getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));
                            view.setImageURI(Uri.parse(uriString));
                        }
                    }
                }
            }
        };
 
        registerReceiver(receiver, new IntentFilter(
                DownloadManager.ACTION_DOWNLOAD_COMPLETE));

四。获取系统download history list:

            Intent i = new Intent(DownloadManager.ACTION_VIEW_DOWNLOADS);
            startActivity(i);

 


五。 得到刚下载的文件 和 打开该文件:

    protected String getDownloadedFileName() {

        String fileName = "";

        DownloadManager downloadManager = (DownloadManager) activity
                .getSystemService(Activity.DOWNLOAD_SERVICE);
        DownloadManager.Query query = new DownloadManager.Query();
        query.setFilterByStatus(DownloadManager.STATUS_SUCCESSFUL);

        Cursor c = downloadManager.query(query);
        if (c.moveToFirst()) {
            fileName = c.getString(c
                    .getColumnIndex(DownloadManager.COLUMN_TITLE));
        }

        return fileName;
    }

 

    public void openFileFromSDCard(String fileStr, String mimeType) {
        Intent intent = new Intent();
        intent.setAction(android.content.Intent.ACTION_VIEW);
        File file = new File(fileStr);
        intent.setDataAndType(Uri.fromFile(file), mimeType);
        activity.startActivity(intent);
    }

 

 

摘自 fhy_2008的专栏

相关文章
相关标签/搜索