Вы можете запустить класс workManager, используя следующий метод, и также можете сохранить его экземпляр.
public static OneTimeWorkRequest getInstance() {
if (workRequest == null) {
workRequest = new OneTimeWorkRequest.Builder(YourClass.class)
.addTag("your_tag")
.build();
}
return workRequest;
}
Вы можете наблюдать за работником, используя getStatusById
и getStatusesByTag
WorkManager.getInstance().getStatusById(getInstance().getId()).observe(this, new Observer<WorkStatus>() {
@Override
public void onChanged(@Nullable WorkStatus workStatus) {
if (workStatus != null) {
Log.e("mylog", "onOptionsItemSelected: " + workStatus.getId() + " -> " + workStatus.getState());
}
}
});
WorkManager.getInstance().getStatusesByTag("your_tag").observe(this, new Observer<List<WorkStatus>>() {
@Override
public void onChanged(@Nullable List<WorkStatus> workStatuses) {
if (workStatuses != null && !workStatuses.isEmpty()) {
for (WorkStatus status : workStatuses) {
Log.d("mylog", "onOptionsItemSelected: "+ status.getId() + " -> " + status.getState());
}
}
}
});
https://developer.android.com/topic/libraries/architecture/workmanager/basics