diff --git a/library/src/main/java/com/liulishuo/filedownloader/FileDownloader.java b/library/src/main/java/com/liulishuo/filedownloader/FileDownloader.java index f8f2ef01..d12ab991 100644 --- a/library/src/main/java/com/liulishuo/filedownloader/FileDownloader.java +++ b/library/src/main/java/com/liulishuo/filedownloader/FileDownloader.java @@ -746,7 +746,7 @@ IQueuesHandler getQueuesHandler() { private static final Object INIT_LOST_CONNECTED_HANDLER_LOCK = new Object(); private ILostServiceConnectedHandler mLostConnectedHandler; - ILostServiceConnectedHandler getLostConnectedHandler() { + public ILostServiceConnectedHandler getLostConnectedHandler() { if (mLostConnectedHandler == null) { synchronized (INIT_LOST_CONNECTED_HANDLER_LOCK) { if (mLostConnectedHandler == null) { diff --git a/library/src/main/java/com/liulishuo/filedownloader/ILostServiceConnectedHandler.java b/library/src/main/java/com/liulishuo/filedownloader/ILostServiceConnectedHandler.java index a4edfe20..6180526c 100644 --- a/library/src/main/java/com/liulishuo/filedownloader/ILostServiceConnectedHandler.java +++ b/library/src/main/java/com/liulishuo/filedownloader/ILostServiceConnectedHandler.java @@ -38,4 +38,9 @@ public interface ILostServiceConnectedHandler { * @return {@code true} if the start action was dispatched. */ boolean dispatchTaskStart(BaseDownloadTask.IRunningTask task); + + /** + * @return {@code true} if there is task in waiting list. + */ + boolean haveWaitingTask(); } diff --git a/library/src/main/java/com/liulishuo/filedownloader/LostServiceConnectedHandler.java b/library/src/main/java/com/liulishuo/filedownloader/LostServiceConnectedHandler.java index 05160e42..76a47297 100644 --- a/library/src/main/java/com/liulishuo/filedownloader/LostServiceConnectedHandler.java +++ b/library/src/main/java/com/liulishuo/filedownloader/LostServiceConnectedHandler.java @@ -125,6 +125,13 @@ public void taskWorkFine(BaseDownloadTask.IRunningTask task) { } } + @Override + public boolean haveWaitingTask() { + synchronized (mWaitingList) { + return !mWaitingList.isEmpty(); + } + } + @Override public boolean dispatchTaskStart(BaseDownloadTask.IRunningTask task) { if (!FileDownloader.getImpl().isServiceConnected()) { @@ -134,12 +141,12 @@ public boolean dispatchTaskStart(BaseDownloadTask.IRunningTask task) { FileDownloadLog.d(this, "Waiting for connecting with the downloader " + "service... %d", task.getOrigin().getId()); } - FileDownloadServiceProxy.getImpl(). - bindStartByContext(FileDownloadHelper.getAppContext()); if (!mWaitingList.contains(task)) { task.free(); mWaitingList.add(task); } + FileDownloadServiceProxy.getImpl(). + bindStartByContext(FileDownloadHelper.getAppContext()); return true; } } diff --git a/library/src/main/java/com/liulishuo/filedownloader/services/BaseFileServiceUIGuard.java b/library/src/main/java/com/liulishuo/filedownloader/services/BaseFileServiceUIGuard.java index 17e64610..5b72d3f1 100644 --- a/library/src/main/java/com/liulishuo/filedownloader/services/BaseFileServiceUIGuard.java +++ b/library/src/main/java/com/liulishuo/filedownloader/services/BaseFileServiceUIGuard.java @@ -27,6 +27,7 @@ import android.os.RemoteException; import com.liulishuo.filedownloader.FileDownloadEventPool; +import com.liulishuo.filedownloader.FileDownloader; import com.liulishuo.filedownloader.IFileDownloadServiceProxy; import com.liulishuo.filedownloader.event.DownloadServiceConnectChangedEvent; import com.liulishuo.filedownloader.util.ExtraKeys; @@ -157,7 +158,10 @@ public void bindStartByContext(final Context context, final Runnable connectedRu bindContexts.add(context); } - runServiceForeground = FileDownloadUtils.needMakeServiceForeground(context); + boolean needMakeServiceForeground = FileDownloadUtils.needMakeServiceForeground(context); + boolean haveWaitingTask = + FileDownloader.getImpl().getLostConnectedHandler().haveWaitingTask(); + runServiceForeground = needMakeServiceForeground && haveWaitingTask; i.putExtra(ExtraKeys.IS_FOREGROUND, runServiceForeground); context.bindService(i, this, Context.BIND_AUTO_CREATE); if (runServiceForeground) {