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
0
votes
1 answer

Shutting down Android Looper

It seems that Looper (at least the one that is created with HandlerThread) never gets garbage collected even if not referenced by other code. I am aware of quit() and quitSafely() methods, however in my application I am not sure who would be…
justme
  • 31
  • 2
0
votes
1 answer

Android - Service falls down in loop?

I have a service that send errors to a url ,this service good work in other services but when i have an error in self falls down in loops that is very bad. public class Error_Service extends Service { Context context; RequestPackage RP; …
user4813855
0
votes
0 answers

Can Looper.loop create multiple loopers

I would like to know whether Looper.loop() called multiple times creates multiple loopers??Please find my code below and let me know whether multiple loopers will be created because of this code.If yes then please give me advice to resolve…
srinivas
  • 72
  • 1
  • 9
0
votes
1 answer

java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()

I am using the following MainActivity, and I am still getting the following error message: Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare() at…
EdT
  • 1
  • 1
0
votes
0 answers

Use Looper or not?

Android docs on Looper are not clear in stating whether we shall use Looper or not. I have seen many places saying not to use Looper with Handler, but to use AsyncTask or some similar non-main thread methodology. So shall we ever use Looper or…
sandalone
  • 41,141
  • 63
  • 222
  • 338
0
votes
0 answers

How to run a looper in addition to an infinite loop

I've got separate thread that is running an infinite loop, which also updates the main ui thread via a handler (my thread > handler > ui thread). Infinite loop on second thread: while (true) { Thread.sleep(100); // do xyz } However I now would…
Chris Stryczynski
  • 30,145
  • 48
  • 175
  • 286
0
votes
1 answer

loopers in java vs thread without looper, and correct way to stop looper?

My question is in regards to java loopers and also how to stop them the correct way. Lets say i have a thread defined something like this: class NoLooperThread extends Thread{ @Override public void run(){ …
j2emanue
  • 60,549
  • 65
  • 286
  • 456
0
votes
2 answers

Only one Looper may be created per thread

With the following code: private void showDialog(String message) { try { Looper.prepare(); Handler handler = new Handler() { @Override public void handleMessage(Message msg) { try { Uri…
IGT
  • 9
  • 2
0
votes
1 answer

java.lang.RuntimeException on NotifyBuilder

I've this code, I want to have two different action, I want to show one classic notification to open MainActivity, and another notification to do LogoutTask, but I've this java.lang.RuntimeException: Can't create handler inside thread that has not…
0
votes
1 answer

How to Handle Force Stop

I would like to know how to handle force stop in my android application. The application contains an asynctask class within the activity as follows: private class SocketTask extends AsyncTask { ProgressDialog pd; …
0
votes
1 answer

how to call method with an asynctask after button is clicked

How can you call a method within an AsyncTask? In my asynctask,which is an inner class in my java file 'xyz', when the user clicks a button, it should call a method within 'xyz' which also happens to be an alertDialog, i know it calls it, but when…
Aria
  • 389
  • 3
  • 7
  • 25
0
votes
0 answers

How to stop a thread when using Looper.loop()?

I have read almost every question related to this topic but they are all perfect until "ending" the thread. public class LooperThread implements Runnable { Handler UI_Handler; IncomingHandler handler; public LooperThread(Handler…
ozgur
  • 2,549
  • 4
  • 25
  • 40
0
votes
2 answers

Android Java object with own thread using looper

I have tried to implement object in Android that would work in its own thread (I do not want to make handler public, I want to wrap sentMessage method with own public api). It has public methods to pass data to object. This object is associated with…
0
votes
1 answer

Repeat "beep" sound each 10 seconds as a service Android

Rx with timer looks like the way to go. If you are not up for it Handler could work as well. http://reactivex.io/documentation/operators/timer.html
0
votes
1 answer

Multiple image delete

I have a group of images that users upload in my app. I store the image path in an sqlite database and the image in the internal storage of the app. I was able to go around deleting a single image if the user selects it and chooses to delete it. My…
Roach
  • 610
  • 1
  • 8
  • 13