Does android.os.Looper drain battery?

1.2k views Asked by At

I guess, this is a silly question, but still...

In my app I need to run heavyweight tasks in sequence (in a separate thread of course). So, I think, that Looper is my choice for this. AsyncTask is not the case, because requests can arrive at any time and thread-safe stuff is not nessessary.

Does long usage of android.os.Looper drain android battery fast?

From Looper's source code

/**
 * Run the message queue in this thread. Be sure to call
 * {@link #quit()} to end the loop.
 */
public static void loop() {
    final Looper me = myLooper();
    if (me == null) {
        throw new RuntimeException("No Looper; Looper.prepare() wasn't called on this thread.");
    }
    final MessageQueue queue = me.mQueue;

    // Make sure the identity of this thread is that of the local process,
    // and keep track of what that identity token actually is.
    Binder.clearCallingIdentity();
    final long ident = Binder.clearCallingIdentity();

    for (;;) {
        Message msg = queue.next(); // might block
        if (msg == null) {
            // No message indicates that the message queue is quitting.
            return;
        }

        // This must be in a local variable, in case a UI event sets the logger
        Printer logging = me.mLogging;
        if (logging != null) {
            logging.println(">>>>> Dispatching to " + msg.target + " " +
                    msg.callback + ": " + msg.what);
        }

        msg.target.dispatchMessage(msg);

        if (logging != null) {
            logging.println("<<<<< Finished to " + msg.target + " " + msg.callback);
        }

        // Make sure that during the course of dispatching the
        // identity of the thread wasn't corrupted.
        final long newIdent = Binder.clearCallingIdentity();
        if (ident != newIdent) {
            Log.wtf(TAG, "Thread identity changed from 0x"
                    + Long.toHexString(ident) + " to 0x"
                    + Long.toHexString(newIdent) + " while dispatching to "
                    + msg.target.getClass().getName() + " "
                    + msg.callback + " what=" + msg.what);
        }

        msg.recycle();
    }
}

I see, that here we have an infinitive loop, which is quite fine. But still, I'm afraid, that having this Looper in the background of app will burn battery quite fast, that loop is running even when all activities are off.

Does anyone know is that just a myth? Or should I pick up some other class for solving my problem?

Thank you for your time.

2

There are 2 answers

2
sergej shafarenka On BEST ANSWER

Well. It depends on how many messages and how often you will send to this looper. Although it runs in an endless loop, this implementation will wait in queue.next() for a next message to proceed with. In wait looper consumes nothing. If you are going to send many message constantly, then it doesn't make any difference whether you use looper of anything else. Your code will run and will consume battery.

2
pskink On

every ui thread has a Looper so its not such a beast, see: queue.next(); // might block, most of the time is spent here

btw see HandlerThread which is a Thread with a Looper, all you need to do is to create your worker Handler