当前位置: 移动技术网 > 移动技术>移动开发>Android > Android基于自带的DownloadManager实现下载功能示例

Android基于自带的DownloadManager实现下载功能示例

2019年07月24日  | 移动技术网移动技术  | 我要评论

本文实例讲述了android基于自带的downloadmanager实现下载功能。分享给大家供大家参考,具体如下:

downloadmanager.request request = new downloadmanager.request(uri.parse(apk_url));
request.setdestinationinexternalpublicdir(download_folder_name, download_file_name);
request.settitle(getstring(r.string.download_notification_title));
request.setdescription("meilishuo desc");
request.setnotificationvisibility(downloadmanager.request.visibility_visible_notify_completed);
request.setvisibleindownloadsui(false);
// request.allowscanningbymediascanner();
// request.setallowednetworktypes(downloadmanager.request.network_wifi);
// request.setshowrunningnotification(false);
// request.setnotificationvisibility(downloadmanager.request.visibility_hidden);
request.setmimetype("application/cn.trinea.download.file");
downloadid = downloadmanager.enqueue(request);

downloadmanager.enqueue是加入下载请求到下载管理类中,并且会返回一个下载的id。

request.setallowednetworktypes(downloadmanager.request.network_wifi);

大文件只能在wifi下下载

需要注册一个下载完成的广播,自定义这个广播

class completereceiver extends broadcastreceiver {
  @override
  public void onreceive(context context, intent intent) {
   /**
    * get the id of download which have download success, if the id is my id and it's status is successful,
    * then install it
    **/
   long completedownloadid = intent.getlongextra(downloadmanager.extra_download_id, -1);
   if (completedownloadid == downloadid) {
    initdata();
    updateview();
    // if download successful, install apk
    if (downloadmanagerpro.getstatusbyid(downloadid) == downloadmanager.status_successful) {
     string apkfilepath = new stringbuilder(environment.getexternalstoragedirectory().getabsolutepath())
       .append(file.separator).append(download_folder_name).append(file.separator)
       .append(download_file_name).tostring();
     install(context, apkfilepath);
    }
   }
  }
 };

这里的intent.getlongextra(downloadmanager.extra_download_id, -1);downloadmanager.extra_download_iddownloadmanager类里的参数,使用下面方法注册广播

/** register download success broadcast **/
registerreceiver(completereceiver, new intentfilter(downloadmanager.action_download_complete));

用到的intentfilter是下载完成的filter

然后会通知这个广播,并且返回的intent里面包含了downloadmanager.extra_download_id的参数。

关于downloadmanager的其他用法可以查看api文档

这里再介绍下downloadmanager.query的用法。

显而易见query是内部类。有query.setfilterbyidquery.setfilterbystatus两个方法,

query.setfilterbyid就是把downloadmanager.enqueue返回的id放进去作为查询的条件;

复制代码 代码如下:
query.setfilterbystatus(downloadmanager.status_failed|downloadmanager.status_pending|downloadmanager.status_running|downloadmanager.status_successful);

可以进行拼接查询的条件。

cursor cur = downloadmanager.query(query);

这里用的query查询downloads的数据库,但是只可以查询本应用下载的数据

/**
 * 使用downloadmanager.query查询downloads的db,但是在stackoverflow中的解释是
 * you can't access this db from my application. for your own downloads,
 * use downloadmanager.query to check on your download status. data about downloads is not shared to other apps.
 */

所以要是想通过downloadmanager.query查询系统所有的下载记录是不可行的。

但是需求有要怎么办呢?

记得apidemo里有用户联系人使用uri的方式查询联系人contacts,进入root explore观察com.android.providers.downloads包里的db数据库内容时,发现下载的记录里有content://media/external/file/452122

这种内容,所以可以这样获取到下载的文件

复制代码 代码如下:
cursor cursor = getcontentresolver().query(uri.parse("content://media/external/file"), null, null, null, null);

测试下返回的字段

/*string[] downnames = cursor.getcolumnnames();
string str = "";
for(int i=0;i<downnames.length;i++){
 str += downnames[i]+",";
}*/
if(cursor!=null&&cursor.movetolast()){
//       cursor.movetoprevious()
 string displayname = cursor.getstring(cursor.getcolumnindex("_display_name"));
 string name = cursor.getstring(cursor.getcolumnindex("name"));
 string title = cursor.getstring(cursor.getcolumnindex("title"));
 string description = cursor.getstring(cursor.getcolumnindex("description"));
 string data = cursor.getstring(cursor.getcolumnindex("_data"));
 string bucket = cursor.getstring(cursor.getcolumnindex("bucket_display_name"));
 downloadtip.settext(displayname+","+name+","+title+","+description+","+data+","+bucket);
}

根据自己需求提取字段。

更多关于android相关内容感兴趣的读者可查看本站专题:《android基本组件用法总结》、《android开发入门与进阶教程》、《android布局layout技巧总结》、《android视图view技巧总结》、《android编程之activity操作技巧总结》、《android资源操作技巧汇总》及《android控件用法总结

希望本文所述对大家android程序设计有所帮助。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网