DownloadUtil.java 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. package com.xzjmyk.pm.activity.util;
  2. import android.annotation.SuppressLint;
  3. import android.annotation.TargetApi;
  4. import android.app.Activity;
  5. import android.app.DownloadManager;
  6. import android.content.BroadcastReceiver;
  7. import android.content.Context;
  8. import android.content.Intent;
  9. import android.content.IntentFilter;
  10. import android.net.Uri;
  11. import android.os.Build;
  12. import android.os.Environment;
  13. import com.xzjmyk.pm.activity.ui.erp.util.CommonUtil;
  14. import java.io.File;
  15. import java.net.URLDecoder;
  16. /**
  17. * 下载类
  18. *
  19. * @author shiquanL
  20. */
  21. @TargetApi(Build.VERSION_CODES.GINGERBREAD)
  22. @SuppressLint("DefaultLocale")
  23. public class DownloadUtil {
  24. static String FilePath;
  25. static BroadcastReceiver receiver;
  26. /**
  27. * @param context 上下文场景
  28. * @param url 下载文件的地址
  29. * @param path SD卡保存的路径 如:"/MyDownload",自动在SD下创建该目录。
  30. */
  31. public static void DownloadFile(Context context, String url, String path) {
  32. /*
  33. * 注册广播监听下载完成
  34. */
  35. receiver = new DownloadCompleteReceiver();
  36. context.registerReceiver(receiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
  37. /**
  38. * 先检测SD卡是否存在
  39. */
  40. if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
  41. return;
  42. }
  43. /*
  44. * 创建文件夹
  45. */
  46. String file = Environment.getExternalStorageDirectory().getPath() + path;
  47. File files = new File(file);
  48. if (files == null || !files.exists()) {
  49. files.mkdir();
  50. }
  51. /*
  52. * 截取文件名
  53. */
  54. String fileName = url.substring(url.lastIndexOf("/") + 1);
  55. fileName = URLDecoder.decode(fileName);
  56. /*
  57. *系统下载服务类
  58. */
  59. DownloadManager downManager = (DownloadManager) context.getSystemService(Activity.DOWNLOAD_SERVICE);
  60. DownloadManager.Request down = new DownloadManager.Request(Uri.parse(url));
  61. down.setShowRunningNotification(true);
  62. //在通知栏显示
  63. down.setVisibleInDownloadsUi(true);
  64. //输出目录
  65. down.setDestinationInExternalPublicDir(path + "/", fileName);
  66. //文件路径
  67. FilePath = file + "/" + fileName;
  68. //加入下载队列执行
  69. downManager.enqueue(down);
  70. }
  71. public static void unregisterReceiver(Context context) {
  72. context.unregisterReceiver(receiver);
  73. }
  74. ;
  75. /**
  76. * 监听下载完成
  77. *
  78. * @author Administrator
  79. */
  80. public static class DownloadCompleteReceiver extends BroadcastReceiver {
  81. @Override
  82. public void onReceive(Context context, Intent intent) {
  83. if (intent.getAction().equals(
  84. DownloadManager.ACTION_DOWNLOAD_COMPLETE)) {
  85. //获取文件路径
  86. File files = new File(FilePath);
  87. //打开这个文件
  88. Intent openFile = CommonUtil.getFileIntent(files);
  89. context.startActivity(openFile);
  90. }
  91. }
  92. }
  93. }