Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Communicating between Main Thread and Background Thread in Android, Exams of Computer Applications

How to send messages or runnable objects from the main thread to a background thread in android. The background thread doesn't have its own message queue, so we create a looper instance for it and a handler within the run() method of the background thread. The handler instance is then associated with the looper instance. To send a message from the main thread to the background thread, we first obtain a reference to the handler of the background thread and then give the message to the handler, which will post it to the message queue of the background thread. Creating a looper for the background thread is necessary to complete these steps.

Typology: Exams

2017/2018

Uploaded on 08/05/2018

israil_karud
israil_karud 🇮🇳

1 document

1 / 1

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Sending a Message or Runnable objects from Main Thread
to Background Thread
Generally Background thread does not have its own message queue ..so we create looper instance for that
background thread which maintains messagequeue then we create instance of Handler within run() of
background thread and that handler instance implicitly gets associated with thread that is creating it.
Now that created handler instance is associated with looper instance.
If we want to send message from Main thread to Background Thread
1. We first take reference of handler of background thread.
2. And then we give message to handler which will post message to messagqueue of that
background thread
3. In order to complete above 2 steps ..You must need to create looper for background thread which
is responsible for maintaining a messagequeue for that background thread.

Partial preview of the text

Download Communicating between Main Thread and Background Thread in Android and more Exams Computer Applications in PDF only on Docsity!

• Sending a Message or Runnable objects from Main Thread

to Background Thread

Generally Background thread does not have its own message queue ..so we create looper instance for that background thread which maintains messagequeue then we create instance of Handler within run() of background thread and that handler instance implicitly gets associated with thread that is creating it.

Now that created handler instance is associated with looper instance.

If we want to send message from Main thread to Background Thread

  1. We first take reference of handler of background thread.
  2. And then we give message to handler which will post message to messagqueue of that background thread
  3. In order to complete above 2 steps ..You must need to create looper for background thread which is responsible for maintaining a messagequeue for that background thread.