DownReceiver.java 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. package com.bgy.autosale.payutil;
  2. import android.app.DownloadManager;
  3. import android.content.BroadcastReceiver;
  4. import android.content.Context;
  5. import android.content.Intent;
  6. import android.database.Cursor;
  7. import com.hboxs.base_library.base.BaseApplication;
  8. import com.hboxs.base_library.event.DownLoadMessageEvent;
  9. import com.hboxs.base_library.util.LogUtil;
  10. import com.hboxs.base_library.util.SharedPreferencesUtils;
  11. import com.orhanobut.hawk.Hawk;
  12. import org.greenrobot.eventbus.EventBus;
  13. import java.util.HashMap;
  14. /**
  15. * @author whw
  16. * @time 2019/4/17
  17. * @Description 下载状态监听
  18. */
  19. public class DownReceiver extends BroadcastReceiver {
  20. private static final String TAG = "DownReceiver";
  21. private long mTaskId;
  22. private DownloadManager downloadManager;
  23. @Override
  24. public void onReceive(Context context, Intent intent) {
  25. mTaskId= (long) SharedPreferencesUtils.getParam("taskid",0l);
  26. downloadManager = (DownloadManager) BaseApplication.getContext().getSystemService(BaseApplication.getContext().DOWNLOAD_SERVICE);
  27. // startQuery();
  28. checkDownloadStatus();
  29. }
  30. //检查下载状态
  31. private void checkDownloadStatus() {
  32. // downloadManager = (DownloadManager) BaseApplication.getContext().getSystemService(BaseApplication.getContext().DOWNLOAD_SERVICE);
  33. DownloadManager.Query query = new DownloadManager.Query();
  34. query.setFilterById(mTaskId);//筛选下载任务,传入任务ID,可变参数
  35. Cursor c = downloadManager.query(query);
  36. LogUtil.d(TAG, "checkDownloadStatus: 》》》下载");
  37. if (c.moveToFirst()) {
  38. int status = c.getInt(c.getColumnIndex(DownloadManager.COLUMN_STATUS));
  39. int mDownload_so_far = c.getInt(c.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR));
  40. int mDownload_all = c.getInt(c.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES));
  41. LogUtil.d(TAG, "checkDownloadStatus: mDownload_so_far="+mDownload_so_far);
  42. LogUtil.d(TAG, "checkDownloadStatus: mDownload_all="+mDownload_all);
  43. LogUtil.d(TAG, "checkDownloadStatus: 》》》 status="+status);
  44. int screenType = Hawk.get("screen_type",10);
  45. HashMap<Long,String> map =Hawk.get("download");
  46. String name = map.get(mTaskId);
  47. String fileTyle=name.substring(name.lastIndexOf(".")+1,name.length());
  48. LogUtil.d(TAG, "checkDownloadStatus: 》》》 fileTyle="+fileTyle);
  49. switch (status) {
  50. case DownloadManager.STATUS_PAUSED:
  51. LogUtil.d(TAG, "checkDownloadStatus: >>>下载暂停");
  52. break;
  53. case DownloadManager.STATUS_PENDING:
  54. LogUtil.d(TAG, "checkDownloadStatus: >>>下载延迟");
  55. break;
  56. case DownloadManager.STATUS_RUNNING:
  57. LogUtil.d(TAG, "checkDownloadStatus: >>>正在下载");
  58. /**
  59. * 计算下载下载率;
  60. */
  61. int totalSize = c.getInt(c.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES));
  62. int currentSize = c.getInt(c.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR));
  63. int progress = (int) (((float) currentSize) / ((float) totalSize) * 100);
  64. LogUtil.d(TAG, "checkDownloadStatus: "+progress);
  65. if (fileTyle.equals("apk")){
  66. //下载文件apk
  67. EventBus.getDefault().post(new DownLoadMessageEvent(DownLoadMessageEvent.Type.downloadApkProcess,progress,-1));
  68. }
  69. break;
  70. case DownloadManager.STATUS_SUCCESSFUL:
  71. LogUtil.d(TAG, "checkDownloadStatus: >>>下载完成"+name);
  72. //todo 可以判断下载的文件是apk还是广告数据了
  73. if (fileTyle.equals("apk")){
  74. //下载文件apk
  75. EventBus.getDefault().post(new DownLoadMessageEvent(DownLoadMessageEvent.Type.downloadApkSuccess,name,-1));
  76. }else {
  77. EventBus.getDefault().post(new DownLoadMessageEvent(DownLoadMessageEvent.Type.success,name,screenType));
  78. LogUtil.e(TAG, "checkDownloadStatus: 发送下载的screenType:"+screenType);
  79. }
  80. break;
  81. case DownloadManager.STATUS_FAILED:
  82. LogUtil.d(TAG, "checkDownloadStatus: >>>下载失败"+name);
  83. if (fileTyle.equals("apk")){
  84. //下载文件apk
  85. EventBus.getDefault().post(new DownLoadMessageEvent(DownLoadMessageEvent.Type.downloadApkFailed,name,-1));
  86. }else {
  87. EventBus.getDefault().post(new DownLoadMessageEvent(DownLoadMessageEvent.Type.failed,name,screenType));
  88. LogUtil.e(TAG, "checkDownloadStatus: 发送下载的screenType:"+screenType);
  89. }
  90. break;
  91. }
  92. c.close();
  93. }
  94. }
  95. }