Skip to content

Instantly share code, notes, and snippets.

@devrath
Created September 19, 2022 13:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save devrath/376d46f6ef2a3befd5d8b36bb8d7030e to your computer and use it in GitHub Desktop.
Save devrath/376d46f6ef2a3befd5d8b36bb8d7030e to your computer and use it in GitHub Desktop.
demo describing using handler in android to process runnables
class CustomBackgroundThread : Thread() {
private var handler: Handler? = null
override fun run() {
super.run()
Looper.prepare() // Prepare the looper and associate with current thread
handler = Handler()
Looper.loop() // Start the thread so that it can keep the thread active
}
/**
* Here we can add the runnable to the message queue
*/
fun addTaskToMessageQueue(task: Runnable) {
handler?.post(task)
}
fun stopTheThread() { interrupt() }
}
class HandlerActivity : AppCompatActivity() {
private lateinit var binding: ActivityHandlerBinding
lateinit var bkgThread : CustomBackgroundThread
lateinit var runnableWorkOne : Runnable
lateinit var runnableWorkTwo : Runnable
lateinit var runnableWorkThree : Runnable
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityHandlerBinding.inflate(layoutInflater)
setContentView(binding.root)
startThread()
binding.btnPublishOneId.setOnClickListener {
runnableWorkOne = RunnableWorkOne()
bkgThread.addTaskToMessageQueue(runnableWorkOne);
}
binding.btnPublishTwoId.setOnClickListener {
runnableWorkTwo = RunnableWorkTwo()
bkgThread.addTaskToMessageQueue(runnableWorkTwo);
}
binding.btnPublishThreeId.setOnClickListener {
runnableWorkThree = RunnableWorkThree()
bkgThread.addTaskToMessageQueue(runnableWorkThree);
}
binding.btnRestartThreadId.setOnClickListener {
startThread()
}
binding.btnStopThreadId.setOnClickListener {
stopThreads()
}
}
private fun startThread() {
bkgThread = CustomBackgroundThread()
bkgThread.start() // Start a new thread
}
private fun stopThreads() {
bkgThread.stopTheThread()
}
}
class RunnableWorkOne : Runnable {
override fun run() {
try {
val TAG_ONE = "one"
for (i in 0 until 5) {
Thread.sleep(500L)
println(TAG_SAKURA.plus("--$i--").plus("Current Runnable-->$TAG_ONE"))
}
}catch (ex:InterruptedException){
println(
TAG_SAKURA.plus("--")
.plus("Runnable named")
.plus("--")
.plus(CustomRunnableOne::class.java.name)
.plus("--")
.plus("is interrupted"))
}
}
}
class RunnableWorkThree : Runnable {
override fun run() {
try {
val TAG_THREE = "three"
for (i in 0 until 5) {
Thread.sleep(500L)
println(TAG_SAKURA.plus("--$i--").plus("Current Runnable-->$TAG_THREE"))
}
}catch (ex:InterruptedException){
println(
TAG_SAKURA.plus("--")
.plus("Runnable named")
.plus("--")
.plus(CustomRunnableThree::class.java.name)
.plus("--")
.plus("is interrupted"))
}
}
}
class RunnableWorkTwo : Runnable {
override fun run() {
try {
val TAG_TWO = "two"
for (i in 0 until 5) {
Thread.sleep(500L)
println(TAG_SAKURA.plus("--$i--").plus("Current Runnable-->$TAG_TWO"))
}
}catch (ex:InterruptedException){
println(
TAG_SAKURA.plus("--")
.plus("Runnable named")
.plus("--")
.plus(CustomRunnableTwo::class.java.name)
.plus("--")
.plus("is interrupted"))
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment