From ec9219ac7fb03d8af61d3a02092d6ea8fa8c9c7d Mon Sep 17 00:00:00 2001 From: huangzhuanghua <401742778@qq.com> Date: Tue, 11 Oct 2016 14:41:59 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=9A=82=E5=81=9C=E7=9A=84?= =?UTF-8?q?=E4=B8=8B=E8=BD=BD=E4=B9=9F=E7=AE=97=E4=B8=8B=E8=BD=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/gh/common/util/NotificationUtils.java | 5 ++++- .../java/com/gh/download/DownloadService.java | 17 +++++++++-------- .../java/com/gh/download/DownloadThread.java | 14 ++++++++------ 3 files changed, 21 insertions(+), 15 deletions(-) diff --git a/app/src/main/java/com/gh/common/util/NotificationUtils.java b/app/src/main/java/com/gh/common/util/NotificationUtils.java index 1e3c94ad6c..2828e411b4 100644 --- a/app/src/main/java/com/gh/common/util/NotificationUtils.java +++ b/app/src/main/java/com/gh/common/util/NotificationUtils.java @@ -54,7 +54,10 @@ public class NotificationUtils { int downloadingSize = 0; for (DownloadEntity entity : DownloadManager.getInstance(context).getAll()) { if (entity.getStatus().equals(DownloadStatus.downloading) - || entity.getStatus().equals(DownloadStatus.waiting)) { + || entity.getStatus().equals(DownloadStatus.waiting) + || entity.getStatus().equals(DownloadStatus.pause) + || entity.getStatus().equals(DownloadStatus.timeout) + || entity.getStatus().equals(DownloadStatus.neterror)) { downloadingSize++; } } diff --git a/app/src/main/java/com/gh/download/DownloadService.java b/app/src/main/java/com/gh/download/DownloadService.java index b6e95daf43..f6c3edba2f 100644 --- a/app/src/main/java/com/gh/download/DownloadService.java +++ b/app/src/main/java/com/gh/download/DownloadService.java @@ -34,7 +34,6 @@ public class DownloadService extends Service { public void onCreate() { super.onCreate(); Utils.log(DownloadService.class.getSimpleName(), "onCreate"); - NotificationUtils.showDownloadingNotification(this); ArrayList doneList = new ArrayList<>(); for (DownloadEntity downloadEntity : DownloadManager.getInstance(this).getAll()) { if (downloadEntity.getStatus().equals(DownloadStatus.done)) { @@ -48,6 +47,7 @@ public class DownloadService extends Service { NotificationUtils.showDownloadDoneNotification(this, downloadEntity, mFlag); } } + NotificationUtils.showDownloadingNotification(this); } @Override @@ -58,7 +58,7 @@ public class DownloadService extends Service { DownloadEntity entry = (DownloadEntity) intent.getSerializableExtra(Constants.KEY_DOWNLOAD_ENTRY); switch (status) { case add: - addDownload(entry); + addDownload(entry, true); break; case pause: case timeout: @@ -76,7 +76,7 @@ public class DownloadService extends Service { return super.onStartCommand(intent, flags, startId); } - private synchronized void addDownload(DownloadEntity entry) { + private synchronized void addDownload(DownloadEntity entry, boolean isShow) { DownloadEntity downloadEntity = DownloadDao.getInstance(this).get(entry.getUrl()); // 数据库下载历史中有这个任务的下载记录 if (downloadEntity != null) { @@ -99,7 +99,9 @@ public class DownloadService extends Service { Utils.log(DownloadService.class.getSimpleName(), "addDownload==>" + entry); EventBus.getDefault().post(new EBDownloadStatus("download")); - NotificationUtils.showDownloadingNotification(this); + if (isShow) { + NotificationUtils.showDownloadingNotification(this); + } } private synchronized void startDownload(DownloadEntity entry) { @@ -118,7 +120,7 @@ public class DownloadService extends Service { } private synchronized void resumeDownload(DownloadEntity entry) { - addDownload(entry); + addDownload(entry, false); Utils.log(DownloadService.class.getSimpleName(), "resumeDownload==>" + entry); } @@ -137,8 +139,6 @@ public class DownloadService extends Service { DataChanger.getInstance().notifyDataChanged(entry); } Utils.log(DownloadService.class.getSimpleName(), "pauseDownload==>" + entry); - - NotificationUtils.showDownloadingNotification(this); } private synchronized void cancelDownload(DownloadEntity downloadEntity) { @@ -165,6 +165,7 @@ public class DownloadService extends Service { NotificationManager nManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); nManager.cancel(flag); } + NotificationUtils.showDownloadingNotification(DownloadService.this); } @SuppressLint("HandlerLeak") @@ -178,10 +179,10 @@ public class DownloadService extends Service { case downloading: break; case done: - NotificationUtils.showDownloadingNotification(DownloadService.this); mFlag++; flagMap.put(entry.getUrl(), mFlag); NotificationUtils.showDownloadDoneNotification(DownloadService.this, entry, mFlag); + NotificationUtils.showDownloadingNotification(DownloadService.this); removeAndCheckNext(entry); break; case pause: diff --git a/app/src/main/java/com/gh/download/DownloadThread.java b/app/src/main/java/com/gh/download/DownloadThread.java index 438b6612d0..9398640d06 100644 --- a/app/src/main/java/com/gh/download/DownloadThread.java +++ b/app/src/main/java/com/gh/download/DownloadThread.java @@ -134,17 +134,19 @@ public class DownloadThread extends Thread { break; } } - if (status != DownloadStatus.pause - && status != DownloadStatus.cancel) { - listener.onStatusChanged(DownloadStatus.done); - } bos.flush(); Utils.log(DownloadThread.class.getSimpleName(), "flush==>" + targetFile.length() + ",progress==>" - + entry.getProgress()); - + + entry.getProgress() + ",size==>" + entry.getSize()); + bis.close(); bos.close(); +// if (status != DownloadStatus.pause && status != DownloadStatus.cancel) { +// listener.onStatusChanged(DownloadStatus.done); +// } + if (targetFile.length() == entry.getSize()) { + listener.onStatusChanged(DownloadStatus.done); + } } } catch (Exception e) { String errorMsg = Log.getStackTraceString(e);