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
528
votes
14 answers

What is the purpose of Looper and how to use it?

I am new to Android. I want to know what the Looper class does and also how to use it. I have read the Android Looper class documentation but I am unable to completely understand it. I have seen it in a lot of places but unable to understand its…
Khawar Raza
  • 15,870
  • 24
  • 70
  • 127
95
votes
4 answers

ANR at android.os.MessageQueue.nativePollOnce

We are seeing this ANR in different parts of our app. Wanted to understand what causes this ANR? main (native): tid=1 systid=31940 #00 pc 0x5431c libc.so #01 pc 0x1313a5 libart.so #02 pc 0x2ab05b libart.so #03 pc 0x3659 libnativehelper.so #04 pc…
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
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
27
votes
4 answers

Method getMainLooper in android.os.Looper not mocked still occuring even after adding RxImmediateSchedulerRule

TrendingViewModelTest @RunWith(JUnit4::class) class TrendingViewModelTest { private lateinit var trendingRepository: TrendingRepository private lateinit var trendingViewModel: TrendingViewModel @get:Rule val schedulers =…
Manoj Perumarath
  • 9,337
  • 8
  • 56
  • 77
27
votes
3 answers

Android - myLooper() vs getMainLooper()

Just clarifying but in an Android activity on MAIN Thread if I call Looper.myLooper() vs Looper.getMainLooper() the return the same reference, right? they are the same thing? I know I would never have to call these usually as Android takes care of…
j2emanue
  • 60,549
  • 65
  • 286
  • 456
18
votes
2 answers

FusedLocationProviderClient when and how to stop Looper?

I used to use FusedLocationApi until I learned that it is deprecated (see references below). It was simple to implement. As the documentation says you need to use it in conjunction with…
portfoliobuilder
  • 7,556
  • 14
  • 76
  • 136
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
9
votes
0 answers

Can one assign different priorities to Handlers or Messages being processed by Looper?

I am aware that the recommended way of doing background-like tasks in Android is to start a worker thread of some kind, but I'm wondering if there is a simpler alternative available when the background tasks are small and numerous. So here's my…
RenniePet
  • 11,420
  • 7
  • 80
  • 106
6
votes
2 answers

Handler or Launching a Coroutine Job to do something in the MainThread

I've been wondering about whether it is a better approach to use a Handler (Looper.getMainLooper()) or launch a new Coroutine Job to do small things on the Main Thread, like updating a View. Handler: val uiHandler =…
6
votes
0 answers

Find out Leakage issues in Looper or HandlerThread

I have used some Handlers like new Handler(Looper.getMainLooper()).post(new Runnable() { @Override public void run() { try{ Messages.onAcknowledgeReceived(); …
Ranjit
  • 5,130
  • 3
  • 30
  • 66
6
votes
0 answers

PhoneStateListener in background thread

I'm having an issue in trying to run a PhoneStateListener on* callback methods in a backround thread. Here's what I did so far: First, I wrote my PhoneStateListener implementation. Something on the lines of: public MyPhoneStateListener extends…
5
votes
1 answer

Unit test with LiveData Method getMainLooper in android.os.Looper not mocked

I cannot make liveData.postValue working in while trying to make unit test. I have been checking in google for a solution and this is the code I have now. public class ProjectListViewModelTest { GetProjectList getProjectList =…
Iban Arriola
  • 2,526
  • 9
  • 41
  • 88
5
votes
1 answer

Handle response of WorkManager on Network connection Failure

I'm using WorkManager to sync data from my local Room database to server. The issue is that Room gives error to build database in Loop.MainLooper() and when i use it as following it works fine. But I'm unable to return the 'WorkerResult' on SUCCESS…
Usman Rana
  • 2,067
  • 1
  • 21
  • 32
5
votes
1 answer

What is E/AbstractTracker: Can't create handler inside thread that has not called Looper.prepare()?

I have had this problem in the past and I didn't think much of it as I was only just experimenting with the IDE. However, I have realized the error also shows up when I create a fresh new application, even when I have added no code of my own I still…
Ashraf Rahman
  • 467
  • 1
  • 5
  • 9
1
2 3
11 12