1. 当设备满足系统版本为 8.0+ 且应用在后台运行时,使用 startForegroundService() 启动 DownloadService ,否则使用原来的 startService() 启动 DownloadService

2. 当应用从后台切换回前台并且 DownloadService 是前台服务时,手动调用 Service.stopForeground() 来去掉前台服务标记
This commit is contained in:
chenjuntao
2018-12-17 16:18:05 +08:00
parent fd236857d4
commit e082b321eb
5 changed files with 79 additions and 8 deletions

View File

@ -3,6 +3,7 @@ package com.gh.download;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
@ -23,6 +24,7 @@ import com.gh.gamecenter.entity.GameEntity;
import com.gh.gamecenter.eventbus.EBDownloadStatus;
import com.gh.gamecenter.manager.PackagesManager;
import com.google.gson.Gson;
import com.halo.assistant.HaloApp;
import com.lightgame.config.CommonDebug;
import com.lightgame.download.ConnectionUtils;
import com.lightgame.download.DataChanger;
@ -279,7 +281,7 @@ public class DownloadManager implements DownloadStatusListener {
downloadEntity.setStatus(DownloadStatus.done);
DataChanger.INSTANCE.notifyDataChanged(downloadEntity);
} else if (!isTaskDownloading(url)) {
mContext.startService(getIntent(downloadEntity, DownloadStatus.add));
startDownloadService(downloadEntity, DownloadStatus.add);
}
put(url, System.currentTimeMillis());
putStatus(url, DownloadStatus.downloading);
@ -300,7 +302,7 @@ public class DownloadManager implements DownloadStatusListener {
downloadEntity.setStatus(DownloadStatus.done);
DataChanger.INSTANCE.notifyDataChanged(downloadEntity);
} else if (!isTaskDownloading(url)) {
mContext.startService(getIntent(downloadEntity, DownloadStatus.subscribe));
startDownloadService(downloadEntity, DownloadStatus.subscribe);
}
put(url, System.currentTimeMillis());
putStatus(url, DownloadStatus.subscribe);
@ -474,7 +476,7 @@ public class DownloadManager implements DownloadStatusListener {
}
if (entry != null) {
entry.setStatus(DownloadStatus.cancel);
mContext.startService(getIntent(entry, DownloadStatus.cancel));
startDownloadService(entry, DownloadStatus.cancel);
Utils.log(DownloadManager.class.getSimpleName(), "cancel");
}
}
@ -518,7 +520,7 @@ public class DownloadManager implements DownloadStatusListener {
checkDownloadEntryRecordValidate(url);
DownloadEntity entry = DataChanger.INSTANCE.getDownloadEntries().get(url);
if (entry != null) {
mContext.startService(getIntent(entry, DownloadStatus.pause));
startDownloadService(entry, DownloadStatus.pause);
put(url, System.currentTimeMillis());
statusMap.put(url, DownloadStatus.pause);
}
@ -546,10 +548,8 @@ public class DownloadManager implements DownloadStatusListener {
}
// 开启下载服务
mContext.startService(new Intent(mContext, DownloadService.class));
startDownloadServiceInBackground();
DownloadNotification.showDownloadingNotification(mContext);
}
public void addObserver(DataWatcher dataWatcher) {
@ -561,6 +561,22 @@ public class DownloadManager implements DownloadStatusListener {
Utils.log(DownloadManager.class.getSimpleName(), "removeObserver");
DataChanger.INSTANCE.deleteObserver(dataWatcher);
}
public void startDownloadServiceInBackground() {
mContext.startService(new Intent(mContext, DownloadService.class));
}
public void startDownloadService(DownloadEntity downloadEntity, DownloadStatus status) {
Intent serviceIntent = getIntent(downloadEntity, status);
// 当满足系统版本大于 8.0 、应用在后台运行时以前台服务开启
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O
&& !HaloApp.getInstance().isRunningForeground) {
serviceIntent.putExtra(DownloadService.KEY_SERVICE_ACTION, DownloadService.START_FOREGROUND);
mContext.startForegroundService(serviceIntent);
} else {
mContext.startService(serviceIntent);
}
}
//
// public void removeObservers() {
// Utils.log(DownloadManager.class.getSimpleName(), "removeObserver");