public class WeakHandler
extends java.lang.Object
Original implementation of Handlers always keeps hard reference to handler in queue of execution. If you create anonymous handler and post delayed message into it, it will keep all parent class for that time in memory even if it could be cleaned.
This implementation is trickier, it will keep WeakReferences to runnables and messages, and GC could collect them once WeakHandler instance is not referenced any more
Created by Dmytro Voronkevych on 17/06/2014.
| Constructor and Description |
|---|
WeakHandler()
Default constructor associates this handler with the
Looper for the
current thread. |
WeakHandler(android.os.Handler.Callback callback)
Constructor associates this handler with the
Looper for the
current thread and takes a callback interface in which you can handle
messages. |
WeakHandler(android.os.Looper looper)
Use the provided
Looper instead of the default one. |
WeakHandler(android.os.Looper looper,
android.os.Handler.Callback callback)
Use the provided
Looper instead of the default one and take a callback
interface in which to handle messages. |
| Modifier and Type | Method and Description |
|---|---|
android.os.Looper |
getLooper() |
boolean |
hasMessages(int what)
Check if there are any pending posts of messages with code 'what' in
the message queue.
|
boolean |
hasMessages(int what,
java.lang.Object object)
Check if there are any pending posts of messages with code 'what' and
whose obj is 'object' in the message queue.
|
boolean |
post(java.lang.Runnable r)
Causes the Runnable r to be added to the message queue.
|
boolean |
postAtFrontOfQueue(java.lang.Runnable r)
Posts a message to an object that implements Runnable.
|
boolean |
postAtTime(java.lang.Runnable r,
long uptimeMillis)
Causes the Runnable r to be added to the message queue, to be run
at a specific time given by uptimeMillis.
|
boolean |
postAtTime(java.lang.Runnable r,
java.lang.Object token,
long uptimeMillis)
Causes the Runnable r to be added to the message queue, to be run
at a specific time given by uptimeMillis.
|
boolean |
postDelayed(java.lang.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.
|
void |
removeCallbacks(java.lang.Runnable r)
Remove any pending posts of Runnable r that are in the message queue.
|
void |
removeCallbacks(java.lang.Runnable r,
java.lang.Object token)
Remove any pending posts of Runnable r with Object
token that are in the message queue.
|
void |
removeCallbacksAndMessages(java.lang.Object token)
Remove any pending posts of callbacks and sent messages whose
obj is token.
|
void |
removeMessages(int what)
Remove any pending posts of messages with code 'what' that are in the
message queue.
|
void |
removeMessages(int what,
java.lang.Object object)
Remove any pending posts of messages with code 'what' and whose obj is
'object' that are in the message queue.
|
boolean |
sendEmptyMessage(int what)
Sends a Message containing only the what value.
|
boolean |
sendEmptyMessageAtTime(int what,
long uptimeMillis)
Sends a Message containing only the what value, to be delivered
at a specific time.
|
boolean |
sendEmptyMessageDelayed(int what,
long delayMillis)
Sends a Message containing only the what value, to be delivered
after the specified amount of time elapses.
|
boolean |
sendMessage(android.os.Message msg)
Pushes a message onto the end of the message queue after all pending messages
before the current time.
|
boolean |
sendMessageAtFrontOfQueue(android.os.Message msg)
Enqueue a message at the front of the message queue, to be processed on
the next iteration of the message loop.
|
boolean |
sendMessageAtTime(android.os.Message msg,
long uptimeMillis)
Enqueue a message into the message queue after all pending messages
before the absolute time (in milliseconds) uptimeMillis.
|
boolean |
sendMessageDelayed(android.os.Message msg,
long delayMillis)
Enqueue a message into the message queue after all pending messages
before (current time + delayMillis).
|
public WeakHandler()
Looper for the
current thread.
If this thread does not have a looper, this handler won't be able to receive messages so an exception is thrown.
public WeakHandler(android.os.Handler.Callback callback)
Looper for the
current thread and takes a callback interface in which you can handle
messages.
If this thread does not have a looper, this handler won't be able to receive messages so an exception is thrown.
callback - The callback interface in which to handle messages, or null.public WeakHandler(android.os.Looper looper)
Looper instead of the default one.looper - The looper, must not be null.public WeakHandler(android.os.Looper looper,
android.os.Handler.Callback callback)
Looper instead of the default one and take a callback
interface in which to handle messages.looper - The looper, must not be null.callback - The callback interface in which to handle messages, or null.public final boolean post(java.lang.Runnable r)
r - The Runnable that will be executed.public final boolean postAtTime(java.lang.Runnable r,
long uptimeMillis)
SystemClock.uptimeMillis().
The runnable will be run on the thread to which this handler is attached.r - The Runnable that will be executed.uptimeMillis - The absolute time at which the callback should run,
using the SystemClock.uptimeMillis() time-base.public final boolean postAtTime(java.lang.Runnable r,
java.lang.Object token,
long uptimeMillis)
SystemClock.uptimeMillis().
The runnable will be run on the thread to which this handler is attached.r - The Runnable that will be executed.uptimeMillis - The absolute time at which the callback should run,
using the SystemClock.uptimeMillis() time-base.SystemClock.uptimeMillis()public final boolean postDelayed(java.lang.Runnable r,
long delayMillis)
r - The Runnable that will be executed.delayMillis - The delay (in milliseconds) until the Runnable
will be executed.public final boolean postAtFrontOfQueue(java.lang.Runnable r)
r - The Runnable that will be executed.public final void removeCallbacks(java.lang.Runnable r)
public final void removeCallbacks(java.lang.Runnable r,
java.lang.Object token)
public final boolean sendMessage(android.os.Message msg)
public final boolean sendEmptyMessage(int what)
public final boolean sendEmptyMessageDelayed(int what,
long delayMillis)
sendMessageDelayed(Message, long)public final boolean sendEmptyMessageAtTime(int what,
long uptimeMillis)
sendMessageAtTime(Message, long)public final boolean sendMessageDelayed(android.os.Message msg,
long delayMillis)
public boolean sendMessageAtTime(android.os.Message msg,
long uptimeMillis)
SystemClock.uptimeMillis().
You will receive it in callback, in the thread attached
to this handler.uptimeMillis - The absolute time at which the message should be
delivered, using the
SystemClock.uptimeMillis() time-base.public final boolean sendMessageAtFrontOfQueue(android.os.Message msg)
public final void removeMessages(int what)
public final void removeMessages(int what,
java.lang.Object object)
public final void removeCallbacksAndMessages(java.lang.Object token)
public final boolean hasMessages(int what)
public final boolean hasMessages(int what,
java.lang.Object object)
public final android.os.Looper getLooper()