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

所属分类: 软件编程 / Android 阅读数: 27
收藏 0 赞 0 分享

本文实例讲述了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程序设计有所帮助。

更多精彩内容其他人还在看

老生常谈Android HapticFeedback(震动反馈)

下面小编就为大家带来一篇老生常谈Android HapticFeedback(震动反馈)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

详谈OnTouchListener与OnGestureListener的区别

下面小编就为大家带来一篇详谈OnTouchListener与OnGestureListener的区别。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Android仿知乎悬浮功能按钮FloatingActionButton效果

前段时间在看属性动画,恰巧这个按钮的效果可以用属性动画实现,下面通过本文给大家分享adroid仿知乎悬浮功能按钮FloatingActionButton效果,需要的朋友参考下吧
收藏 0 赞 0 分享

解决Android V7后自定义Toolbar、ActionBar左侧有空白问题

这篇文章主要介绍的Android V7后自定义Toolbar、ActionBar左侧有空白问题的解决方法,需要的朋友可以参考下
收藏 0 赞 0 分享

Android常见控件使用详解

这篇文章主要为大家详细介绍了Android常见控件的使用方法,包括ProgressBar进度条控件、AlertDialog对话框控件等,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android实现简洁的APP更新dialog数字进度条

这篇文章主要为大家详细介绍了Android实现简洁的APP更新dialog数字进度条,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android 判断当前语言环境是否是中文环境

本文主要介绍了Android 判断当前语言环境是否是中文环境的方法。具有很好的参考价值。下面跟着小编一起来看下吧
收藏 0 赞 0 分享

详谈Android中Matrix的set、pre、post的区别

下面小编就为大家带来一篇详谈Android中Matrix的set、pre、post的区别。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Android实现登录界面记住密码的存储

这篇文章主要为大家详细介绍了Android SharedPreferrences实现登录界面记住密码的存储,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android 使用SharedPreferrences储存密码登录界面记住密码功能

Android存储方式有很多种,在这里所用的存储方式是SharedPreferrences, 其采用了Map数据结构来存储数据,以键值的方式存储,可以简单的读取与写入,下面通过实例代码给大家讲解下,需要的朋友参考下吧
收藏 0 赞 0 分享
查看更多