package com.sunzee.receiver; import android.app.DownloadManager; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.database.Cursor; import android.util.Log; import com.orhanobut.hawk.Hawk; import com.sunzee.base.BaseApplication; import com.sunzee.model.message.DownLoadMessageEvent; import com.sunzee.utils.SharedPreferencesUtils; import org.greenrobot.eventbus.EventBus; import java.util.HashMap; /** * @author whw * @time 2019/4/17 * @Description 下载状态监听 */ public class DownReceiver extends BroadcastReceiver { private static final String TAG = "DownReceiver"; private long mTaskId; private DownloadManager downloadManager; @Override public void onReceive(Context context, Intent intent) { mTaskId= (long) SharedPreferencesUtils.getParam("taskid",0l); checkDownloadStatus(); } //检查下载状态 private void checkDownloadStatus() { downloadManager = (DownloadManager) BaseApplication.getContext().getSystemService(Context.DOWNLOAD_SERVICE); DownloadManager.Query query = new DownloadManager.Query(); query.setFilterById(mTaskId);//筛选下载任务,传入任务ID,可变参数 Cursor c = downloadManager.query(query); Log.d(TAG, "checkDownloadStatus: 》》》下载"); if (c.moveToFirst()) { int status = c.getInt(c.getColumnIndex(DownloadManager.COLUMN_STATUS)); Log.d(TAG, "checkDownloadStatus: 》》》 status="+status); int screenType = Hawk.get("screen_type",10); HashMap map = Hawk.get("download"); String name = map.get(mTaskId); String fileTyle=name.substring(name.lastIndexOf(".")+1); Log.d(TAG, "checkDownloadStatus: 》》》 fileTyle="+fileTyle); switch (status) { case DownloadManager.STATUS_PAUSED: Log.d(TAG, "checkDownloadStatus: >>>下载暂停"); break; case DownloadManager.STATUS_PENDING: Log.d(TAG, "checkDownloadStatus: >>>下载延迟"); break; case DownloadManager.STATUS_RUNNING: Log.d(TAG, "checkDownloadStatus: >>>正在下载"); /** * 计算下载下载率; */ int totalSize = c.getInt(c.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES)); int currentSize = c.getInt(c.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR)); int progress = (int) (((float) currentSize) / ((float) totalSize) * 100); Log.d(TAG, "checkDownloadStatus: "+progress); if (fileTyle.equals("apk")){ //下载文件apk EventBus.getDefault().post(new DownLoadMessageEvent(DownLoadMessageEvent.Type.downloadApkProcess,progress,-1)); } break; case DownloadManager.STATUS_SUCCESSFUL: Log.d(TAG, "checkDownloadStatus: >>>下载完成"+name); //todo 可以判断下载的文件是apk还是广告数据了 if (fileTyle.equals("apk")){ //下载文件apk EventBus.getDefault().post(new DownLoadMessageEvent(DownLoadMessageEvent.Type.downloadApkSuccess,name,-1)); }else { EventBus.getDefault().post(new DownLoadMessageEvent(DownLoadMessageEvent.Type.success,name,screenType)); } break; case DownloadManager.STATUS_FAILED: Log.d(TAG, "checkDownloadStatus: >>>下载失败"+name); if (fileTyle.equals("apk")){ //下载文件apk EventBus.getDefault().post(new DownLoadMessageEvent(DownLoadMessageEvent.Type.downloadApkFailed,name,-1)); }else { EventBus.getDefault().post(new DownLoadMessageEvent(DownLoadMessageEvent.Type.failed,name,screenType)); } break; } c.close(); } } }