怎么在Android中利用DownloadManager实现一个文件下载功能

  介绍

本篇文章为大家展示了怎么在Android中利用DownloadManager实现一个文件下载功能,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。

<强> Android中DownloadManager实现文件下载

创建下载链接

DownloadManager.Request  request =, new  DownloadManager.Request (Uri.parse (url),

设置允许下载的网络环境

request.setAllowedNetworkTypes (DownloadManager.Request.NETWORK_WIFI);

WIFI网络:DownloadManager.Request。NETWORK_WIFI

移动网络:DownloadManager.Request。NETWORK_MOBILE

通知显示下载进度

//,在通知显示下载进度   request.setNotificationVisibility (DownloadManager.Request.VISIBILITY_VISIBLE);//,设置标题   request.setTitle(“更新“);//,设置描述   request.setDescription(“正在下载更新文件…“);

设置保存路径

private  static  final  String  DIR =,“AutoUpdate";   private  static  final  String  APK =,“MyHome.apk";   private  static  final  String  PATH =, Environment.getExternalStorageDirectory () .getAbsolutePath (), +,“/? +, DIR  +,“/? +, APK;      request.setDestinationInExternalPublicDir (DIR, APK);

下载

下载会返回一个进程ID

DownloadManager  DownloadManager =, (DownloadManager), context.getSystemService (Context.DOWNLOAD_SERVICE);   long  id =, downloadManager.enqueue(请求);

取消下载

通过ID可以需要下载

downloadManager.remove (ID);

下载完成的监听

下载完成,系统会发出广播,通过注册广播监听者可以监听到下载完成

广播的行动为DownloadManager。ACTION_DOWNLOAD_COMPLETE

/* *   ,* Broadcast  intent  action  sent  by 从而download  manager  when 从而user  clicks 提醒a 运行   ,*下载,,either 得到a  system  notification 看得到,趁机downloads  UI。   ,*/@SdkConstant (SdkConstantType.BROADCAST_INTENT_ACTION)   public  final  static  String  ACTION_NOTIFICATION_CLICKED =癮ndroid.intent.action.DOWNLOAD_NOTIFICATION_CLICKED"才能

<强>代码

<强>下载

DownloadManager.Request  request =, new  DownloadManager.Request (Uri.parse (url));//,WIFI状态下下载   request.setAllowedNetworkTypes (DownloadManager.Request.NETWORK_WIFI);//,设置通知栏   request.setNotificationVisibility (DownloadManager.Request.VISIBILITY_VISIBLE);   request.setTitle(“更新“);   request.setDescription(“正在下载更新文件…“);//,存放路径   request.setDestinationInExternalPublicDir (DIR, APK);//,开始下载   DownloadManager  DownloadManager =, (DownloadManager), context.getSystemService (Context.DOWNLOAD_SERVICE);   long  id =, downloadManager.enqueue(请求);

广播接收者

注册

& lt; ? xml  version=?.0“,编码=皍tf-8" ?比;   & lt; manifest  xmlns: android=癶ttp://schemas.android.com/apk/res/android"   ,,包=癱om.example.kongqingwei.downloadmanagerdemo"比;      ,& lt; !——,网络权限,——比;   ,& lt; uses-permission  android: name=癮ndroid.permission.INTERNET"/比;   ,& lt; uses-permission  android: name=癮ndroid.permission.READ_EXTERNAL_STORAGE"/比;   ,& lt; uses-permission  android: name=癮ndroid.permission.WRITE_EXTERNAL_STORAGE"/比;      ,& lt; uses-permission  android: name=癮ndroid.permission.MANAGE_USERS"/比;      & lt;应用程序   android:才能allowBackup=皌rue"   android:才能图标=癅mipmap/ic_launcher"   android才能:label=癅string/app_name"   android:才能supportsRtl=皌rue"   android:才能主题=癅style/AppTheme"比;   & lt;才能activity  android: name=?MainActivity"比;   ,,& lt; intent-filter>   ,,,& lt; action  android: name=癮ndroid.intent.action.MAIN"/比;      ,,,& lt; category  android: name=癮ndroid.intent.category.LAUNCHER"/比;   ,,& lt;/intent-filter>      & lt;才能/activity>      & lt;才能receiver  android: name=?AutoUpdateBroadcastReceiver"比;   ,,& lt; intent-filter>   ,,,& lt; action  android: name=癮ndroid.intent.action.DOWNLOAD_COMPLETE"/比;   ,,& lt;/intent-filter>   & lt;才能/receiver>   ,& lt;/application>      & lt;/manifest>

怎么在Android中利用DownloadManager实现一个文件下载功能