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???
1.0.0-alpha11
. Any solution ? And I have another Question ? please look It and tell me something about it. please. – Proparoxytone1.0.0-alpha11
– Galeandroidx.work:work-runtime:2.3.4
. It got called 46 times in two seconds. – Chuckwalla