源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

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

  • 时间:2020-08-03 00:56 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要: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);

[code]downloadManager.enqueue[/code]是加入下载请求到下载管理类中,并且会返回一个下载的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);
    }
   }
  }
 };

这里的[code]intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);[/code]的[code]DownloadManager.EXTRA_DOWNLOAD_ID[/code]是DownloadManager类里的参数,使用下面方法注册广播
/** register download success broadcast **/
registerReceiver(completeReceiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));

用到的IntentFilter是下载完成的Filter 然后会通知这个广播,并且返回的intent里面包含了[code]DownloadManager.EXTRA_DOWNLOAD_ID[/code]的参数。 关于DownloadManager的其他用法可以查看api文档 这里再介绍下DownloadManager.Query的用法。 显而易见Query是内部类。有[code]query.setFilterById[/code]和[code]query.setFilterByStatus[/code]两个方法, query.setFilterById就是把downloadManager.enqueue返回的id放进去作为查询的条件;
[url=http://www.1sucai.cn/Special/381.htm]Android基本组件用法总结[/url]》、《[url=http://www.1sucai.cn/Special/410.htm]Android开发入门与进阶教程[/url]》、《[url=http://www.1sucai.cn/Special/371.htm]Android布局layout技巧总结[/url]》、《[url=http://www.1sucai.cn/Special/375.htm]Android视图View技巧总结[/url]》、《[url=http://www.1sucai.cn/Special/367.htm]Android编程之activity操作技巧总结[/url]》、《[url=http://www.1sucai.cn/Special/423.htm]Android资源操作技巧汇总[/url]》及《[url=http://www.1sucai.cn/Special/124.htm]Android控件用法总结[/url]》 希望本文所述对大家Android程序设计有所帮助。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部