Created
September 19, 2022 13:34
-
-
Save devrath/376d46f6ef2a3befd5d8b36bb8d7030e to your computer and use it in GitHub Desktop.
demo describing using handler in android to process runnables
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")) | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")) | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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