Questions tagged [android-looper]

In Android, a Looper is used to run a message loop for a thread since threads by default do not have a message loop associated with them.

Class used to run a message loop for a thread. Threads by default do not have a message loop associated with them; to create one, call prepare() in the thread that is to run the loop, and then loop() to have it process messages until the loop is stopped.

Most interaction with a message loop is through the Handler class.

This is a typical example of the implementation of a Looper thread, using the separation of prepare() and loop() to create an initial Handler to communicate with the Looper.

class LooperThread extends Thread {
    public Handler mHandler;

    public void run() {
        Looper.prepare();

        mHandler = new Handler() {
            public void handleMessage(Message msg) {
                // process incoming messages here
            }
        };

        Looper.loop();
    }
}

More Info

167 questions
5
votes
2 answers

Why main thread's Looper.loop() doesn't block UI thread?

Today I read some blogs and source code about how Handler & Looper work together. Based on what I've learnt, we can have only one Looper on each thread by using the ThreadLocal magic. Usually Handler is initiated in main thread, or else you must…
Ryan Hoo
  • 360
  • 7
  • 19
5
votes
1 answer

getting java.lang.IllegalStateException: The current thread must have a looper

I am getting this error, and my application crashes: java.lang.IllegalStateException: The current thread must have a looper! I didn't get much about how to use looper on Google, I am using threads(mainly for sleep function), handler(for…
Nitish Jain
  • 704
  • 2
  • 10
  • 24
5
votes
3 answers

NullPointerException in HandlerThread

This bug baffled me for hours. I am getting the NullPointerException. The problem is this error is not consistent. It happens when I launch the app, but only occasionally. So I am not sure what is causing it. I apologize for the verbose question…
4
votes
0 answers

Slow looper frame issue android

Basically, from one day to another, without any change, the Android app is showing: Slow Looper main: doFrame is 301ms late because of 69 msg, msg 1 took 311ms (late=52ms h=android.view.Choreographer$FrameHandler…
Amg91
  • 165
  • 8
  • 25
4
votes
4 answers

Understanding UI thread

For example, there are a lot of tasks are posted to UI thread as follows. Handler handler = new Handler(Looper.getMainLooper()); handler.postDelayed(new Runnable() { @Override public void run() { // Some logic to display/update…
Uma Sankar
  • 449
  • 1
  • 8
  • 19
3
votes
2 answers

Maintaining a time gap with post vs postDelayed

There are two functions I need to run with a minimum time gap between them. For reasons beyond the scope of this question, at first I was trying to control the timing from a process running in the webview (via a…
drmrbrewer
  • 11,491
  • 21
  • 85
  • 181
3
votes
3 answers

How do I use Looper within requestLocationUpdates?

I am a beginner in Android development. I'm creating a small app that takes a picture after a device reaches a certain speed. However, one of the parameters of the requestLocationUpdates method (which of course is used to track the user’s location),…
Daniel
  • 137
  • 2
  • 11
3
votes
1 answer

Android: How to create and pass messages to worker thread from main thread

I'm new to multithreading in android and java, I'm having difficult time implementing a simple model in which we can start worker thread and send some message or runnable to the worker thread from main thread where it does some operation and send…
blackHawk
  • 6,047
  • 13
  • 57
  • 100
3
votes
2 answers

can any Handler post to mainThread using Lopper.getMainLooper()

Lets say i am in another thread that i created and in android i do the following: //this is called from another thread (not mainTread) new Handler(Lopper.getMainLooper()).post(new Runnable() { @Override …
j2emanue
  • 60,549
  • 65
  • 286
  • 456
3
votes
0 answers

Copious Android log output from Looper/Handler

I am getting copious output in my Android logs, and not from my app. (They persist after uninstall.) As you can see, the culprit might be Google Play Services. The following block of text repeats nine total times (each with a different number in the…
QED
  • 9,803
  • 7
  • 50
  • 87
3
votes
2 answers

HandlerThread blocks UI android

I am working on a modification of Google's Camera2 API example for Android, found here: https://github.com/googlesamples/android-Camera2Basic I am uploading captured images to Cloudinary, and obviously need to do so in a background thread so the UI…
3
votes
0 answers

requestSingleUpdate listener does not work

onLocationChanged event is not fired with requestSingleUpdate. It seems that it depends on the looper attached ex:…
3
votes
3 answers

Nutiteq: ViewLabel: Delayed Button execution

I am trying to create a nutiteq ViewLabel with an custom view containing three Buttons. My code and the problem are very similar to this post nutiteq: Button resp. clickable Views not working in Custom ViewLabel. With the provided answer of the…
pekayde
  • 118
  • 10
3
votes
1 answer

How to call getLooper() on main Thread?

In Android, Main Thread & HandlerThread has Looper & MessageQueue by default. I can call getLooper() on handlerThread object, but why not on main Thread ? HandlerThread ht = new HandlerThread(); Looper htLooper = ht.getLooper(); // Works…
Ashok Bijoy Debnath
  • 1,493
  • 1
  • 17
  • 27
3
votes
2 answers

Is a Handler a Thread or not, and what is the role of a Looper with Handlers and Threads?

Is a Handler a Thread or not? If yes, how can we update the UI from this Handler(thread)? If we use the Looper concept, it may be possible. In this case, does it apply to any threads? I am very much confused about these Threads, Handlers and…
1
2
3
11 12