package com.bgy.autosale.payutil; import android.app.DownloadManager; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.database.Cursor; import com.hboxs.base_library.base.BaseApplication; import com.hboxs.base_library.event.DownLoadMessageEvent; import com.hboxs.base_library.util.LogUtil; import com.hboxs.base_library.util.SharedPreferencesUtils; import com.orhanobut.hawk.Hawk; 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); downloadManager = (DownloadManager) BaseApplication.getContext().getSystemService(BaseApplication.getContext().DOWNLOAD_SERVICE); // startQuery(); checkDownloadStatus(); } //检查下载状态 private void checkDownloadStatus() { // downloadManager = (DownloadManager) BaseApplication.getContext().getSystemService(BaseApplication.getContext().DOWNLOAD_SERVICE); DownloadManager.Query query = new DownloadManager.Query(); query.setFilterById(mTaskId);//筛选下载任务,传入任务ID,可变参数 Cursor c = downloadManager.query(query); LogUtil.d(TAG, "checkDownloadStatus: 》》》下载"); if (c.moveToFirst()) { int status = c.getInt(c.getColumnIndex(DownloadManager.COLUMN_STATUS)); int mDownload_so_far = c.getInt(c.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR)); int mDownload_all = c.getInt(c.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES)); LogUtil.d(TAG, "checkDownloadStatus: mDownload_so_far="+mDownload_so_far); LogUtil.d(TAG, "checkDownloadStatus: mDownload_all="+mDownload_all); LogUtil.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,name.length()); LogUtil.d(TAG, "checkDownloadStatus: 》》》 fileTyle="+fileTyle); switch (status) { case DownloadManager.STATUS_PAUSED: LogUtil.d(TAG, "checkDownloadStatus: >>>下载暂停"); break; case DownloadManager.STATUS_PENDING: LogUtil.d(TAG, "checkDownloadStatus: >>>下载延迟"); break; case DownloadManager.STATUS_RUNNING: LogUtil.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); LogUtil.d(TAG, "checkDownloadStatus: "+progress); if (fileTyle.equals("apk")){ //下载文件apk EventBus.getDefault().post(new DownLoadMessageEvent(DownLoadMessageEvent.Type.downloadApkProcess,progress,-1)); } break; case DownloadManager.STATUS_SUCCESSFUL: LogUtil.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)); LogUtil.e(TAG, "checkDownloadStatus: 发送下载的screenType:"+screenType); } break; case DownloadManager.STATUS_FAILED: LogUtil.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)); LogUtil.e(TAG, "checkDownloadStatus: 发送下载的screenType:"+screenType); } break; } c.close(); } } }