Questions tagged [android-handler]

A Handler allows you to send and process `Message` and Runnable objects associated with a thread's `MessageQueue`. Each Handler instance is associated with a single thread and that thread's message queue. When you create a new Handler, it is bound to the thread / message queue of the thread that is creating it -- from that point on, it will deliver messages and runnables to that message queue and execute them as they come out of the message queue.

A Handler allows you to send and process Message and Runnable objects associated with a thread's MessageQueue. Each Handler instance is associated with a single thread and that thread's message queue. When you create a new Handler, it is bound to the thread / message queue of the thread that is creating it -- from that point on, it will deliver messages and runnables to that message queue and execute them as they come out of the message queue.

848 questions
398
votes
13 answers

Handler vs AsyncTask vs Thread

I got slightly confused about the differences between Handlers, AsyncTask and Threads in Android. I've read quite a few blogs and questions here in StackOverflow. Handler are background threads that provide you to communicate with the UI. Updating…
Alx
  • 6,275
  • 7
  • 32
  • 54
391
votes
16 answers

Running code in main thread from another thread

In an android service I have created thread(s) for doing some background task. I have a situation where a thread needs to post certain task on main thread's message queue, for example a Runnable. Is there a way to get Handler of the main thread and…
Ahmed
  • 14,503
  • 22
  • 92
  • 150
355
votes
20 answers

What do I use now that Handler() is deprecated?

How do I fix the deprecation warning in this code? Alternatively, are there any other options for doing this? Handler().postDelayed({ context?.let { //code } }, 3000)
Bolt UIX
  • 5,988
  • 6
  • 31
  • 58
314
votes
8 answers

This Handler class should be static or leaks might occur: IncomingHandler

I'm developing an Android 2.3.3 application with a service. I have this inside that service to communicate with Main activity: public class UDPListenerService extends Service { private static final String TAG = "UDPListenerService"; …
VansFannel
  • 45,055
  • 107
  • 359
  • 626
269
votes
5 answers

cancelling a handler.postdelayed process

I am using handler.postDelayed() to create a waiting period before the next stage of my app takes place. During the wait period I am displaying a dialog with progress bar and cancel button. My problem is I can't find a way to cancel the postDelayed…
Ron
  • 2,723
  • 2
  • 15
  • 5
254
votes
9 answers

How to remove all callbacks from a Handler?

I have a Handler from my sub-Activity that was called by the main Activity. This Handler is used by sub-classes to postDelay some Runnables, and I can't manage them. Now, in the onStop event, I need to remove them before finishing the Activity…
Luke Vo
  • 17,859
  • 21
  • 105
  • 181
146
votes
9 answers

Android: When should I use a Handler() and when should I use a Thread?

When I need something to run asynchronously, such as a long running task or a logic that uses the network, or for whatever reason, Starting a new Thread and running it works fine. Creating a Handler and running it works as well. What's the…
JRun
  • 3,328
  • 6
  • 27
  • 41
142
votes
6 answers

Stop handler.postDelayed()

I call multiple Handlers by new Handler().postDelayed(new Runnable()..... How can I stop it when I click on back? public class MyActivity extends AppCompatActivity implements OnClickListener { private Button btn; private Handler handler; …
basti12354
  • 2,490
  • 4
  • 24
  • 43
97
votes
6 answers

Handler is abstract ,cannot be instantiated

I am trying to use a Handler in my app. However, when I instantiate it like this: Handler handler = new Handler(); I get the following error: Gradle: error: Handler is abstract; cannot be instantiated And when I check the solutions, it asks me to…
Chinmay Dabke
  • 5,070
  • 11
  • 40
  • 63
75
votes
4 answers

When to use handler.post() & when to new Thread()

I'm wondering when should I use handler.post(runnable); and when should I use new Thread(runnable).start(); It is mentioned in developers documentation for Handler: Causes the Runnable r to be added to the message queue. The runnable will be run…
reiley
  • 3,759
  • 12
  • 58
  • 114
68
votes
2 answers

Best use of HandlerThread over other similar classes

I am trying to understand the best use case of using HandlerThread. As per definition: "Handy class for starting a new thread that has a looper. The looper can then be used to create handler classes. Note that start() must still be called." I may…
Androidme
  • 3,145
  • 3
  • 24
  • 27
51
votes
5 answers

How to use postDelayed() correctly in Android Studio?

I have a countDownTimer and if the user does not hit the gameButton within the 12th second I want the gameOver method called. The problem is that either the game function instantly gets called when the countDownTimer is 12 or the timer just keeps…
Ryder Thacker
  • 1,472
  • 3
  • 13
  • 33
47
votes
7 answers

Job Scheduler vs Background Service

I have an app which has a feature A which should run in background every minute. Feature A is that the app should connect to a database, read some data then get the current location of the device and based on them check a condition, if the condition…
42
votes
4 answers

How to stop Handler Runnable?

I am using a handler in the following program and I want to stop it when i=5 but the handler doesn't stop and run continuously. b1.setOnClickListener(new OnClickListener() { public void onClick(View v) { handler = new…
user2745636
  • 421
  • 1
  • 4
  • 3
41
votes
4 answers

Handlers, MessageQueue, Looper, do they all run on the UI thread?

I'm trying to wrap my head around threading, and I know that I may use a Handler to post messages/runnables to the MessageQueue, which in turn gets picked up by the Looper and sent back to the Handler for processing. If I post to a Handler in my…
rogerkk
  • 5,494
  • 5
  • 37
  • 53
1
2 3
56 57