public File run(JobContext jc) { // TODO: utilize etag jc.setMode(ThreadPool.MODE_NETWORK); File tempFile = null; try { URL url = new URL(mUrl); tempFile = File.createTempFile("cache", ".tmp", mRoot); // download from url to tempFile jc.setMode(ThreadPool.MODE_NETWORK); boolean downloaded = DownloadUtils.requestDownload(jc, url, tempFile); jc.setMode(ThreadPool.MODE_NONE); if (downloaded) return tempFile; } catch (Exception e) { Log.e(TAG, String.format("fail to download %s", mUrl), e); } finally { jc.setMode(ThreadPool.MODE_NONE); } if (tempFile != null) tempFile.delete(); return null; }
/** * @param parentFolder * @return 表示了下载的返回状态 */ private int downloadXiaomiAds(File parentFolder) { // TODO: Check wheter the ads already exists. int ret = NotifyAdsDef.RET_OK; NotifyAdsCell cell = (NotifyAdsCell) mAdsCell; String adsImage = cell.imgUrl; if (null == adsImage) return NotifyAdsDef.RET_ERROR; ret = DownloadUtils.downFile(mContext, parentFolder, adsImage, cell); LogUtils.logProcess("下载广告 imgUrl: " + adsImage + " 结果: " + ret); if (isCancelled() || ret != NotifyAdsDef.RET_OK) { if (isCancelled()) { LogUtils.logProcess("asynctask 被cancel"); } else { NetState curState = NetUtils.getNetState(mContext); LogUtils.logProcess("网络类型改变,中断下载: " + curState + " " + ret); } } return ret; }
@Override protected void onHandleIntent(Intent intent) { DownloadUtils downloadUtils = new DownloadUtils(); downloadUtils.download(mUrl, this); }