package com.example.wordmanager
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Toast
import androidx.lifecycle.Observer
import androidx.work.*
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
companion object {
const val KEY_COUNT_VALUE = "key_count"
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
button.setOnClickListener {
setOneTimeWorkRequest()
}
}
private fun setOneTimeWorkRequest() {
val workManager = WorkManager.getInstance(applicationContext)
val data: Data = Data.Builder()
.putInt(KEY_COUNT_VALUE,125)
.build()
val constraints = Constraints.Builder()
.setRequiresCharging(true)
.build()
val uploadRequest = OneTimeWorkRequest.Builder(UploadWorker::class.java)
.setConstraints(constraints)
.setInputData(data)
.build()
workManager.enqueue(uploadRequest)
workManager.getWorkInfoByIdLiveData(uploadRequest.id)
.observe(this, Observer {
textView.text = it.state.name
if(it.state.isFinished) {
val data = it.outputData
val message = data.getString(UploadWorker.KEY_WORKER)
Toast.makeText(applicationContext, message, Toast.LENGTH_LONG).show()
}
})
}
}
package com.example.wordmanager
import android.content.Context
import android.util.Log
import androidx.work.Data
import androidx.work.Worker
import androidx.work.WorkerParameters
import java.text.SimpleDateFormat
import java.util.*
class UploadWorker(context: Context, params: WorkerParameters) : Worker(context,params) {
companion object {
const val KEY_WORKER = "key_worker"
}
override fun doWork(): Result {
try {
val count = inputData.getInt(MainActivity.KEY_COUNT_VALUE,0)
for (i in 0 until count) {
Log.i("MYTAG", "Uploading $i")
}
val time = SimpleDateFormat("dd/M/yyyy hh:mm:ss")
val currentDate = time.format(Date())
val outPutData = Data.Builder()
.putString(KEY_WORKER,currentDate)
.build()
return Result.success(outPutData)
} catch (e: Exception) {
return Result.failure()
}
}
}
No comments:
Post a Comment