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
36
votes
2 answers

Example communicating with HandlerThread

I want to set up a HandlerThread from the GUI thread. Then some time later, when a button is clicked on the GUI, it runs callHello(), which then send a message to a HelloLogger object residing on the non-GUI thread which asynchronously logs "Hello…
Jodes
  • 14,118
  • 26
  • 97
  • 156
32
votes
4 answers

How to call a method in activity from a service

There is a service that listens for some voice. If voice matches a string a certain method is invoked in the service object. public class SpeechActivationService extends Service { public static Intent makeStartServiceIntent(Context pContext){…
DarkLeafyGreen
  • 69,338
  • 131
  • 383
  • 601
30
votes
4 answers

Attempt to invoke virtual method 'int android.text.Layout.getLineCount()' on a null object reference

I am getting null pointer exception randomly, usually it works and sometime it crashes, I had searched a lot but didn't get any help as it there is no proper line from where I get any help, I am also using handler with it. My logcat error is as…
Reprator
  • 2,859
  • 2
  • 32
  • 55
29
votes
3 answers

Android AsyncTask [Can't create handler inside thread that has not called Looper.prepare()]

I've created an image upload AsyncTask based on a function. And after uploading, I get this error on onPostExecute(). I read up some StackOverflow answers on Runnable yet I kept getting the error over and over again despite implementing a different…
MrYanDao
  • 1,253
  • 1
  • 15
  • 27
25
votes
1 answer

What is the difference between View.postDelayed() and Handler.postDelayed() on the main thread?

According to the documentation of Handler.postDelayed(Runnable r, long delayMillis): Causes the Runnable r to be added to the message queue, to be run after the specified amount of time elapses. The runnable will be run on the thread to which…
Doron Yakovlev Golani
  • 5,188
  • 9
  • 36
  • 60
23
votes
4 answers

How to cancel a handler before time in Android code?

I create 1 minute delayed timer to shutdown service if it's not completed. Looks like this: private Handler timeoutHandler = new Handler(); inside onCreate() timeoutHandler.postDelayed(new Runnable() { public void run() …
katit
  • 17,375
  • 35
  • 128
  • 256
23
votes
6 answers

AsyncTaskLoader basic example. (Android)

I am using a Loader in my application and based on the result I get from the query I perform on COntacts using this Loader I perform some calculations and store them back in a Sqlite DB. I want this operation to be Asynchronous, however I am…
Skynet
  • 7,820
  • 5
  • 44
  • 80
20
votes
1 answer

Will a Handler postDelayed not being fired when CPU sleeps?

I have an activity with some Handlers that are being executed at intervals no more than 5 minutes. The activity is launched from a BroadcastReceiver and may be launched with the screen off waiting for the user to grab the phone and get the user…
19
votes
3 answers

How to use Handler and handleMessage in Kotlin?

Java code: private final Handler mHandler = new Handler() { @Override public void handleMessage(Message msg) { // code here } }; How to convert this java code to Kotlin? I tried this: private val mHandler = object : Handler() { …
nb2998
  • 469
  • 1
  • 4
  • 13
19
votes
3 answers

How to update ui from asynctask

I've seen many examples of how to do this, but I can't figure how to implement it in my code. I am using this code. I have updated the url, so it will receive a json with dynamic data. What I'm trying to do is to automatically update the list every…
Bjorn
  • 723
  • 2
  • 11
  • 29
19
votes
6 answers

Why use HandlerThread in Android

In android , Handler can be used to post / handle message, if I don't use a HandlerThread (pass its Looper to Handler), does that mean in this case Handler use MainThread (UI Thread) 's Looper ? What result will get if Handler uses MainThread's…
Daniel
  • 231
  • 1
  • 3
  • 6
18
votes
6 answers

how can I call function every 10 sec?

I want make an app that call a function for example every 10 sec. I wrote my code like this: Handler ha=new Handler(); ha.postDelayed(new Runnable() { @Override public void run() { //call function } }, 10000); But my…
user3257386
  • 284
  • 1
  • 2
  • 11
17
votes
8 answers

Why is runnable callbacks destroying activity automatically?

I wanted to know if there is a possibility that we could handle/detect runnable callbacks with delay (postDelayed method) on android? For example, I have one or several splashscreen (which runs with handler.postDelayed(new Runnable()...) on my…
Damiii
  • 1,363
  • 4
  • 25
  • 46
17
votes
2 answers

What is the meaning of removeCallbacks(Runnable r) in Handler in Android

I want to know what is the exact meaning of removeCallbacks(Runnable r) in Handler. I gone through its documentation and it simply says "Remove any pending posts of Runnable r that are in the message queue". Now my question is if my message has…
AndroDev
  • 3,236
  • 8
  • 35
  • 49
16
votes
4 answers

Should I use AlarmManager or Handler?

I'm writing an app that constantly polls the device's sensors and every so often should write down some statistics to a file. This could be as fast as once a second or as slow once a minute. Should I use Handler's postDelayed()method or just…
Moises Jimenez
  • 1,962
  • 3
  • 21
  • 43
1
2
3
56 57