UPDATE (21-nov-2019)
WorkManager.getInstance(activity).enqueueUniquePeriodicWork("test_work",
ExistingPeriodicWorkPolicy.KEEP,
PeriodicWorkRequest.Builder(MyWorker::class.java,
15, TimeUnit.MINUTES)
.build())
Now I'm using PeriodicWork and now doWork()
called twice even I cancelled uniquettask at first attempt.I'm getting Notification twice and Checked in Log also It's called twice.
Note: It's happen only first time and sometime second time also but not getting third time twice.
override fun doWork(): Result {
if (checkTaskFinished()) {
Logger.e("checkXXX----Hurrayyyy ☻♥☺")
val notificationUtils: NotificationUtils = NotificationUtils(applicationContext)
notificationUtils.showNotificationMessage("Readddd", "rwaeaeae.", 2)
WorkManager.getInstance(context).cancelUniqueWork("test_work")
}
return Result.success()
}
Library I am using :
implementation 'androidx.work:work-runtime-ktx:2.2.0'
OLD
I'm facing problem with WorkManager OneTimeWorkRequest setInitialDelay.
It's work fine when app is in forground or in recent list. But When I remove App from recent list everything is messed up.
What I want to achieve ? - I want send notification to user after few hours when some task is pending ,so after some R&D start work using WorkManager because of It's ability to Schedule Tasks without background service limitation.
Now below is code snippet which work till app is not removed from recent:
Constraints constraints = new Constraints.Builder()
.setRequiresBatteryNotLow(true)
.build();
final OneTimeWorkRequest simpleRequest = new OneTimeWorkRequest.Builder(MyWorker.class)
.setInitialDelay(3, TimeUnit.MINUTES)
.setConstraints(constraints)
.addTag("simple_work")
.build();
WorkManager workManager = WorkManager.getInstance();
workManager.beginUniqueWork("simple_work", ExistingWorkPolicy.KEEP, simpleRequest).enqueue();
Worker class
public class MyWorker extends Worker {
private NotificationUtils notificationUtils;
public static final String EXTRA_OUTPUT_MESSAGE = "output_message";
public MyWorker(@NonNull Context context, @NonNull WorkerParameters workerParams) {
super(context, workerParams);
}
@NonNull
@Override
public Result doWork() {
notificationUtils = new NotificationUtils(getApplicationContext());
notificationUtils.showNotificationMessage("TITLE", "This is a MESSEAGE",2);
Data output = new Data.Builder()
.putString(EXTRA_OUTPUT_MESSAGE, "I have come from MyWorker!")
.build();
setOutputData(output);
return Result.SUCCESS;
}
}
Problem is After Remove It from recent list It's send notification but after double time which I set. for example I set setInitialDelay 5 minutes but It's work after 10 minutes.
So ,Please guide me what I do wrong or It's not for schedule task at specific time which I set for once. I don't want to keep repeat work after It's done. I'm create just new after finish first OneTimeWorkRequest so It has to work fine as per documented beginUniqueWork.
Library I am using :
implementation "android.arch.work:work-runtime:1.0.0-alpha11"
English isn't my native language so pardon me for grammatically mistake:)
implementation 'androidx.work:work-runtime-ktx:2.2.0'
. I did update question. – Alcus