DownReceiver.java 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. package com.sunzee.receiver;
  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 android.util.Log;
  8. import com.orhanobut.hawk.Hawk;
  9. import com.sunzee.base.BaseApplication;
  10. import com.sunzee.model.message.DownLoadMessageEvent;
  11. import com.sunzee.utils.SharedPreferencesUtils;
  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. checkDownloadStatus();
  27. }
  28. //检查下载状态
  29. private void checkDownloadStatus() {
  30. downloadManager = (DownloadManager) BaseApplication.getContext().getSystemService(Context.DOWNLOAD_SERVICE);
  31. DownloadManager.Query query = new DownloadManager.Query();
  32. query.setFilterById(mTaskId);//筛选下载任务,传入任务ID,可变参数
  33. Cursor c = downloadManager.query(query);
  34. Log.d(TAG, "checkDownloadStatus: 》》》下载");
  35. if (c.moveToFirst()) {
  36. int status = c.getInt(c.getColumnIndex(DownloadManager.COLUMN_STATUS));
  37. Log.d(TAG, "checkDownloadStatus: 》》》 status="+status);
  38. int screenType = Hawk.get("screen_type",10);
  39. HashMap<Long,String> map = Hawk.get("download");
  40. String name = map.get(mTaskId);
  41. String fileTyle=name.substring(name.lastIndexOf(".")+1);
  42. Log.d(TAG, "checkDownloadStatus: 》》》 fileTyle="+fileTyle);
  43. switch (status) {
  44. case DownloadManager.STATUS_PAUSED:
  45. Log.d(TAG, "checkDownloadStatus: >>>下载暂停");
  46. break;
  47. case DownloadManager.STATUS_PENDING:
  48. Log.d(TAG, "checkDownloadStatus: >>>下载延迟");
  49. break;
  50. case DownloadManager.STATUS_RUNNING:
  51. Log.d(TAG, "checkDownloadStatus: >>>正在下载");
  52. /**
  53. * 计算下载下载率;
  54. */
  55. int totalSize = c.getInt(c.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES));
  56. int currentSize = c.getInt(c.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR));
  57. int progress = (int) (((float) currentSize) / ((float) totalSize) * 100);
  58. Log.d(TAG, "checkDownloadStatus: "+progress);
  59. if (fileTyle.equals("apk")){
  60. //下载文件apk
  61. EventBus.getDefault().post(new DownLoadMessageEvent(DownLoadMessageEvent.Type.downloadApkProcess,progress,-1));
  62. }
  63. break;
  64. case DownloadManager.STATUS_SUCCESSFUL:
  65. Log.d(TAG, "checkDownloadStatus: >>>下载完成"+name);
  66. //todo 可以判断下载的文件是apk还是广告数据了
  67. if (fileTyle.equals("apk")){
  68. //下载文件apk
  69. EventBus.getDefault().post(new DownLoadMessageEvent(DownLoadMessageEvent.Type.downloadApkSuccess,name,-1));
  70. }else {
  71. EventBus.getDefault().post(new DownLoadMessageEvent(DownLoadMessageEvent.Type.success,name,screenType));
  72. }
  73. break;
  74. case DownloadManager.STATUS_FAILED:
  75. Log.d(TAG, "checkDownloadStatus: >>>下载失败"+name);
  76. if (fileTyle.equals("apk")){
  77. //下载文件apk
  78. EventBus.getDefault().post(new DownLoadMessageEvent(DownLoadMessageEvent.Type.downloadApkFailed,name,-1));
  79. }else {
  80. EventBus.getDefault().post(new DownLoadMessageEvent(DownLoadMessageEvent.Type.failed,name,screenType));
  81. }
  82. break;
  83. }
  84. c.close();
  85. }
  86. }
  87. }