xiaoguozi's Blog
Pay it forword - 我并不觉的自豪,我所尝试的事情都失败了······习惯原本生活的人不容易改变,就算现状很糟,他们也很难改变,在过程中,他们还是放弃了······他们一放弃,大家就都是输家······让爱传出去,很困难,也无法预料,人们需要更细心的观察别人,要随时注意才能保护别人,因为他们未必知道自己要什么·····
  DownloadManager是Android为开发者提供的一个后台应用组件,它通过Http层进行文件的下载任务.
    1:使用
         首先要在AndroidManifest.xml中申请访问DownloadManager的权限
          <permission android:name="android.permission.ACCESS_DOWNLOAD_MANAGER"/>
         添加一个下载任务:
         ContentValues values = new ContentValues();
         values.put(Downloads.URI, url);//指定下载地址
        values.put(Downloads.COOKIE_DATA, cookie);//如果下载Server需要cookie,设置cookie
        values.put(Downloads.VISIBILITY,Downloads.VISIBILITY_HIDDEN);//设置下载提示是否在屏幕顶部显示
        values.put(Downloads.NOTIFICATION_PACKAGE, getPackageName());//设置下载完成之后回调的包名
        values.put(Downloads.NOTIFICATION_CLASS, DownloadCompleteReceiver.class.getName());//设置下载完成之后负责接收的Receiver,这个类要继承 BroadcastReceiver     
        values.put(Downloads.DESTINATION,save_path);//设置下载到的路径,这个需要在Receiver里自行处理
        values.put(Downloads.TITLE,title);//设置下载任务的名称
        this.getContentResolver().insert(Downloads.CONTENT_URI, values);//将其插入到DownloadManager的数据库中,数据库会触发修改事件,启动下载任务


   2:如何为DownloadManager设置代理,比如Wap
             values.put(Downloads.PROXY_HOST,"10.0.0.172");
            values.put(Downloads.PROXY_PORT,"80");


  3:如何在下载过程中监听下载任务
     可以通过监听数据库来实现
    DownloadsChangeObserver mDownloadObserver=new DownloadsChangeObserver(Downloads.CONTENT_URI);
    private class DownloadsChangeObserver extends ContentObserver {
        public DownloadsChangeObserver(Uri uri) {
            super(new Handler());
        }
        @Override
        public void onChange(boolean selfChange) {
            //查询需要监听的字段
           //比如要监听实时下载进度,查看当前下载状态:是否已经断开,或者下载失败等等
           StringBuilder wherequery = new StringBuilder(Downloads.TITLE);
            wherequery.append("=");
            wherequery.append("'");
            wherequery.append(mTitle);
            wherequery.append("'");

           mDownloadCursor =mContext.getContentResolver().query(Downloads.CONTENT_URI, new String[] {Downloads.TITLE, Downloads.STATUS, Downloads.CURRENT_BYTES,}, wherequery.toString(), null,orderBy);        
            int mSizeColunmId=mDownloadCursor.getColumnIndexOrThrow(Downloads.CURRENT_BYTES);   
           mDownloadCursor.moveToFirst();    
            int size=mDownloadCursor.getInt(mSizeColunmId);
       }

  4:如何删除下载记录
    private void deleteHistory(String title)//删除掉指定名称的下载记录
    { 
        StringBuilder whereDelete = new StringBuilder(Downloads.TITLE);
        whereDelete.append("=");
        whereDelete.append("'");
        whereDelete.append(str);
        whereDelete.append("'");
        this.getContentResolver().delete(Downloads.CONTENT_URI,whereDelete.toString(), null);
    }   

https://github.com/commonsguy/cw-android/tree/master/Internet/Download
http://hi-android.info/src/com/android/providers/downloads/DownloadProvider.java.html
http://www.androidadb.com/source/platform/frameworks/base/core/java/android/app/DownloadManager.java.html

http://apps-for-android.googlecode.com/svn/trunk/Samples/
posted on 2011-10-29 18:31 小果子 阅读(1083) 评论(0)  编辑 收藏 引用

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理