I'm using work manager for periodic task. I've to execute single instance of worker
my code is below
val workManager = WorkManager.getInstance()
val callDataRequest = PeriodicWorkRequest.Builder(MyLoggerWork::class.java,
15, TimeUnit.MINUTES)
.addTag(worker)
.build()
workManager.enqueueUniquePeriodicWork(worker, ExistingPeriodicWorkPolicy.KEEP, callDataRequest)
and my worker logs as follows
18/09/2018 03:18:19
18/09/2018 03:18:19
18/09/2018 03:18:19
18/09/2018 03:18:19
18/09/2018 03:37:18
18/09/2018 03:37:18
18/09/2018 03:37:18
18/09/2018 03:37:18
here is my MyLoggerWork class
public class MyLoggerWork: Worker(){
override fun doWork(): Result {
addlog()
return Worker.Result.SUCCESS
}
private fun addlog() {
try {
val directory = File(Environment.getExternalStorageDirectory().path + "/", "Jobs")
if (!directory.exists()) {
directory.mkdir()
}
val file = File(directory.path, "jobs_.txt")
if (!file.exists() && directory.exists()) {
file.createNewFile()
}
try {
val text = SimpleDateFormat("dd/MM/yyyy hh:mm:ss").format(Date())
val stream = FileOutputStream(file, true)
stream.write("\r\n".toByteArray())
stream.write(text.toByteArray())
stream.close()
} catch (e: Exception) {
e.printStackTrace()
}
} catch (e: IOException) {
e.printStackTrace()
}
}
}
i am using following dependency
implementation 'android.arch.work:work-runtime:1.0.0-alpha08'
why my work gets called 4 times???
You are using a very old version of WorkManager. You should switch to the latest stable version. You can try using 1.0.1
or better yet, switch to 2.0.1
which includes androidx
dependencies.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With