我对 Android 架构组件中的 WorkManager 有疑问。
workmanager 的版本是 alpha-06
也许我不明白这个过程,但我希望 OneTimeWorkRequest 只会工作一次,如果这项工作的时间不是很长(不超过 7+- 分钟),它可以正常工作,但如果更多,workmanager 开始同样的工作再次(具有相同的 UUID)并且不停止第一个工作,并且 workmanager 并行执行两个工作
在这里我开始工作
Data workDownloadBookData = new Data.Builder().putInt(BookDownloadWork.BOOK_ID_KEY, id).putString(BookDownloadWork.COUNTRY_CODE_KEY, countryCode).build();
OneTimeWorkRequest request = new OneTimeWorkRequest.Builder(BookDownloadWork.class)
.setInputData(workDownloadBookData)
.setBackoffCriteria(BackoffPolicy.EXPONENTIAL, 2L, TimeUnit.SECONDS)
.build();
WorkManager workManager = WorkManager.getInstance();
if (workManager != null) {
workManager.beginUniqueWork(countryCode + id,ExistingWorkPolicy.KEEP, request).enqueue();
}
这是我的工作
public class BookDownloadWork extends Worker {
private static final String TAG = BookDownloadWork.class.getSimpleName();
public static final String COUNTRY_CODE_KEY = "COUNTRY_CODE_KEY";
public static final String LAST_UPDATE_KEY = "LAST_UPDATE_KEY";
public static final String BOOK_ID_KEY = "BOOK_ID_KEY";
public static final String PHOTO_URI_KEY = "PHOTO_URI_KEY";
private BookRepository bookRepository;
private BookLoadProgressDao bookLoadProgressDao;
private BookLoadWorkerDao bookLoadWorkerDao;
private NotificationController notificationController;
@NonNull
@Override
public Result doWork() {
bookRepository = App.appComponent.getBookRepository();
bookLoadProgressDao = App.appComponent.getBookLoadProgressDao();
bookLoadWorkerDao = App.appComponent.getBookLoadWorkerDao();
notificationController = App.appComponent.getNotificationController();
String countryCode = getInputData().getString(COUNTRY_CODE_KEY);
// String countryCode = getInputData().getString(COUNTRY_CODE_KEY, "");
int serverBookId = getInputData().getInt(BOOK_ID_KEY, 0);
if (!TextUtils.isEmpty(countryCode) && serverBookId != 0) {
String localBookId = serverBookId + countryCode;
BookLoadProgress bookLoadProgress = new BookLoadProgress();
bookLoadProgress.setId(localBookId);
try {
LocalBookDetail localBookDetail = bookRepository.selectLocalBookDetailSynch(serverBookId, countryCode);
bookRepository.saveBookToLocalStorageSynch(serverBookId, localBookDetail.getLastUpdate(), countryCode, null);
BookLoadWorker bookLoadWorker = new BookLoadWorker();
bookLoadWorker.setBookId(localBookId);
bookLoadWorker.setWorkId(getId());
bookLoadWorkerDao.insertBookLoadWorker(bookLoadWorker);
RemoteBookChapter[] remoteBookChapters = bookRepository.loadBookFromServerSynch(countryCode, serverBookId);
if (remoteBookChapters == null) return Result.FAILURE;
//count max progress
for (int i = 0; i < remoteBookChapters.length; i++) {
RemoteBookChapter remoteBookChapter = remoteBookChapters[i];
if (remoteBookChapter.getType().equals("image")) { bookLoadProgress.setMaxProgress(bookLoadProgress.getMaxProgress() + 1);
for (int j = 0; j < remoteBookChapter.getContent().length; j++) {
RemoteBookContent remoteBookContent = remoteBookChapter.getContent()[j];
if (remoteBookContent.getType().equals("image")) {
bookLoadProgress.setMaxProgress(bookLoadProgress.getMaxProgress() + 1);
}
}
}
}
bookLoadProgressDao.insertBookLoadProgress(bookLoadProgress);
for (int i = 0; i < remoteBookChapters.length; i++) {
RemoteBookChapter remoteBookChapter = remoteBookChapters[i];
if (remoteBookChapter.getType().equals("image")) { remoteBookChapter.setUrl(bookRepository.loadAndSaveImageSynch(remoteBookChapter.getUrl())); bookLoadProgress.setCurrentProgress(bookLoadProgress.getCurrentProgress() + 1);
bookLoadProgressDao.insertBookLoadProgress(bookLoadProgress);
for (int j = 0; j < remoteBookChapter.getContent().length; j++) {
RemoteBookContent remoteBookContent = remoteBookChapter.getContent()[j];
if (remoteBookContent.getType().equals("image")) {
remoteBookContent.setUrl(bookRepository.loadAndSaveImageSynch(remoteBookContent.getUrl())); bookLoadProgress.setCurrentProgress(bookLoadProgress.getCurrentProgress() + 1); bookLoadProgressDao.insertBookLoadProgress(bookLoadProgress);
}
}
}
}
bookRepository.saveBookToLocalStorageSynch(serverBookId, localBookDetail.getLastUpdate(), countryCode, remoteBookChapters);
bookLoadProgressDao.deleteBookLoadProgress(bookLoadProgress.getId());
notificationController.sendNotificationAboutBookDownloadFinished(serverBookId, countryCode);
return Result.SUCCESS;
} catch (Exception e) {
Log.e(TAG, "doWork: ",e );
// bookLoadProgressDao.deleteBookLoadProgress(bookLoadProgress.getId());
bookRepository.deleteBookSycnh(localBookId);
return Result.FAILURE;
}
} else {
return Result.FAILURE;
}
}
}
但我只需要一项工作即可下载这些文件并将其保存到数据库