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

Android基于自带的DownloadManager实现下载功能示例,第1张

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

本文实例讲述了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);

测试下返回的字段

/*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程序设计有所帮助。

总结

以上是内存溢出为你收集整理的Android基于自带的DownloadManager实现下载功能示例全部内容,希望文章能够帮你解决Android基于自带的DownloadManager实现下载功能示例所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1144813.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-31
下一篇2022-05-31

发表评论

登录后才能评论

评论列表(0条)

    保存