Handler Class
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
A Handler allows you to send and process Message and Runnable
objects associated with a thread's MessageQueue.
[Android.Runtime.Register("android/os/Handler", DoNotGenerateAcw=true)]
public class Handler : Java.Lang.Object
[<Android.Runtime.Register("android/os/Handler", DoNotGenerateAcw=true)>]
type Handler = class
inherit Object
- Inheritance
- Derived
- Attributes
Remarks
A Handler allows you to send and process Message and Runnable objects associated with a thread's MessageQueue. Each Handler instance is associated with a single thread and that thread's message queue. When you create a new Handler it is bound to a Looper. It will deliver messages and runnables to that Looper's message queue and execute them on that Looper's thread.
There are two main uses for a Handler: (1) to schedule messages and runnables to be executed at some point in the future; and (2) to enqueue an action to be performed on a different thread than your own.
Scheduling messages is accomplished with the #post, #postAtTime(Runnable, long), #postDelayed, #sendEmptyMessage, #sendMessage, #sendMessageAtTime, and #sendMessageDelayed methods. The <em>post</em> versions allow you to enqueue Runnable objects to be called by the message queue when they are received; the <em>sendMessage</em> versions allow you to enqueue a Message object containing a bundle of data that will be processed by the Handler's #handleMessage method (requiring that you implement a subclass of Handler).
When posting or sending to a Handler, you can either allow the item to be processed as soon as the message queue is ready to do so, or specify a delay before it gets processed or absolute time for it to be processed. The latter two allow you to implement timeouts, ticks, and other timing-based behavior.
When a process is created for your application, its main thread is dedicated to running a message queue that takes care of managing the top-level application objects (activities, broadcast receivers, etc) and any windows they create. You can create your own threads, and communicate back with the main application thread through a Handler. This is done by calling the same <em>post</em> or <em>sendMessage</em> methods as before, but from your new thread. The given Runnable or Message will then be scheduled in the Handler's message queue and processed when appropriate.
Java documentation for android.os.Handler.
Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.
Constructors
| Name | Description |
|---|---|
| Handler() |
Default constructor associates this handler with the |
| Handler(Action<Message>) |
Initializes a new instance of the Handler class. |
| Handler(Handler+ICallback) |
Constructor associates this handler with the Looper for the current thread and takes a callback interface in which you can handle messages. |
| Handler(IntPtr, JniHandleOwnership) |
A constructor used when creating managed representations of JNI objects; called by the runtime. |
| Handler(Looper, Handler+ICallback) |
Use the provided Looper instead of the default one and take a callback interface in which to handle messages. |
| Handler(Looper) |
Use the provided |
Properties
| Name | Description |
|---|---|
| Class |
Returns the runtime class of this |
| Handle |
The handle to the underlying Android instance. (Inherited from Object) |
| JniIdentityHashCode |
Gets the identity hash code assigned to this Java peer by the interop runtime. (Inherited from Object) |
| JniPeerMembers |
Provides JNI peer-member metadata for this type. |
| Looper |
Gets the Looper value. |
| PeerReference |
Gets the JNI object reference for this Java peer. (Inherited from Object) |
| ThresholdClass |
This API supports the Mono for Android infrastructure and is not intended to be used directly from your code. |
| ThresholdType |
This API supports the Mono for Android infrastructure and is not intended to be used directly from your code. |
Methods
| Name | Description |
|---|---|
| Clone() |
Creates and returns a copy of this object. (Inherited from Object) |
| CreateAsync(Looper, Handler+ICallback) |
Create a new Handler whose posted messages and runnables are not subject to synchronization barriers such as display vsync. |
| CreateAsync(Looper) |
Create a new Handler whose posted messages and runnables are not subject to synchronization barriers such as display vsync. |
| DispatchMessage(Message) |
Handle system messages here. |
| Dispose() |
Releases the resources held by this Java peer. (Inherited from Object) |
| Dispose(Boolean) |
Releases the resources held by this Java peer. (Inherited from Object) |
| Dump(IPrinter, String) |
Writes this object's state to the supplied output. |
| DumpAsync(IPrinter, String) |
Asynchronously performs the corresponding operation. |
| Equals(Object) |
Indicates whether some other object is "equal to" this one. (Inherited from Object) |
| GetHashCode() |
Returns a hash code value for the object. (Inherited from Object) |
| GetMessageName(Message) |
Returns a string representing the name of the specified message. |
| HandleMessage(Message) |
Subclasses must implement this to receive messages. |
| HasCallbacks(IRunnable) |
Check if there are any pending posts of messages with callback r in the message queue. |
| HasMessages(Int32, Object) |
Check if there are any pending posts of messages with code 'what' and whose obj is 'object' in the message queue. |
| HasMessages(Int32) |
Check if there are any pending posts of messages with code 'what' in the message queue. |
| JavaFinalize() |
Called by the garbage collector on an object when garbage collection determines that there are no more references to the object. (Inherited from Object) |
| Notify() |
Wakes up a single thread that is waiting on this object's monitor. (Inherited from Object) |
| NotifyAll() |
Wakes up all threads that are waiting on this object's monitor. (Inherited from Object) |
| ObtainMessage() |
Returns a new |
| ObtainMessage(Int32, Int32, Int32, Object) |
Same as |
| ObtainMessage(Int32, Int32, Int32) |
Same as |
| ObtainMessage(Int32, Object) |
Same as |
| ObtainMessage(Int32) |
Same as |
| Post(Action) |
Adds the runnable to this handler's message queue. |
| Post(IRunnable) |
Causes the Runnable r to be added to the message queue. |
| PostAtFrontOfQueue(Action) |
Posts the runnable for the next iteration of this handler's message queue. |
| PostAtFrontOfQueue(IRunnable) |
Posts a message to an object that implements Runnable. |
| PostAtTime(Action, Int64) |
Posts the runnable to run at the specified uptime. |
| PostAtTime(Action, Object, Int64) |
Posts the runnable to run at the specified uptime. |
| PostAtTime(IRunnable, Int64) |
Causes the Runnable r to be added to the message queue, to be run at a specific time given by <var>uptimeMillis</var>. |
| PostAtTime(IRunnable, Object, Int64) |
Causes the Runnable r to be added to the message queue, to be run at a specific time given by <var>uptimeMillis</var>. |
| PostDelayed(Action, Int64) |
Posts the runnable to run after the specified delay. |
| PostDelayed(IRunnable, Int64) |
Causes the Runnable r to be added to the message queue, to be run after the specified amount of time elapses. |
| PostDelayed(IRunnable, Object, Int64) |
Causes the Runnable r to be added to the message queue, to be run after the specified amount of time elapses. |
| RemoveCallbacks(Action, Object) |
Removes pending callbacks for the specified runnable. |
| RemoveCallbacks(Action) |
Removes pending callbacks for the specified runnable. |
| RemoveCallbacks(IRunnable, Object) |
Remove any pending posts of Runnable <var>r</var> with Object <var>token</var> that are in the message queue. |
| RemoveCallbacks(IRunnable) |
Remove any pending posts of Runnable r that are in the message queue. |
| RemoveCallbacksAndMessages(Object) |
Remove any pending posts of callbacks and sent messages whose <var>obj</var> is <var>token</var>. |
| RemoveMessages(Int32, Object) |
Remove any pending posts of messages with code 'what' and whose obj is 'object' that are in the message queue. |
| RemoveMessages(Int32) |
Remove any pending posts of messages with code 'what' that are in the message queue. |
| SendEmptyMessage(Int32) |
Sends a Message containing only the what value. |
| SendEmptyMessageAtTime(Int32, Int64) |
Sends a Message containing only the what value, to be delivered at a specific time. |
| SendEmptyMessageDelayed(Int32, Int64) |
Sends a Message containing only the what value, to be delivered after the specified amount of time elapses. |
| SendMessage(Message) |
Pushes a message onto the end of the message queue after all pending messages before the current time. |
| SendMessageAtFrontOfQueue(Message) |
Enqueue a message at the front of the message queue, to be processed on the next iteration of the message loop. |
| SendMessageAtTime(Message, Int64) |
Enqueue a message into the message queue after all pending messages before the absolute time (in milliseconds) <var>uptimeMillis</var>. |
| SendMessageDelayed(Message, Int64) |
Enqueue a message into the message queue after all pending messages before (current time + delayMillis). |
| SetHandle(IntPtr, JniHandleOwnership) |
Sets the Handle property. (Inherited from Object) |
| ToArray<T>() |
Creates a managed array from this Java array wrapper. (Inherited from Object) |
| ToString() |
Returns a string representation of the object. (Inherited from Object) |
| UnregisterFromRuntime() |
Unregisters this Java peer from the interop runtime. (Inherited from Object) |
| Wait() |
Causes the current thread to wait until it is awakened, typically by being <em>notified</em> or <em>interrupted</em>. (Inherited from Object) |
| Wait(Int64, Int32) |
Causes the current thread to wait until it is awakened, typically by being <em>notified</em> or <em>interrupted</em>, or until a certain amount of real time has elapsed. (Inherited from Object) |
| Wait(Int64) |
Causes the current thread to wait until it is awakened, typically by being <em>notified</em> or <em>interrupted</em>, or until a certain amount of real time has elapsed. (Inherited from Object) |
Explicit Interface Implementations
| Name | Description |
|---|---|
| IJavaPeerable.Disposed() |
Notifies the interop runtime that this managed peer has been disposed. (Inherited from Object) |
| IJavaPeerable.DisposeUnlessReferenced() |
Releases this Java peer unless it is retained by another managed reference. (Inherited from Object) |
| IJavaPeerable.Finalized() |
Notifies the interop runtime that this managed peer has been finalized. (Inherited from Object) |
| IJavaPeerable.JniManagedPeerState |
Gets the state that describes the relationship between this managed peer and its JNI reference. (Inherited from Object) |
| IJavaPeerable.SetJniIdentityHashCode(Int32) |
Sets the interop identity hash code for this Java peer. (Inherited from Object) |
| IJavaPeerable.SetJniManagedPeerState(JniManagedPeerStates) |
Sets the managed and JNI peer state for this Java peer. (Inherited from Object) |
| IJavaPeerable.SetPeerReference(JniObjectReference) |
Sets the JNI object reference used by this managed peer. (Inherited from Object) |
Extension Methods
| Name | Description |
|---|---|
| GetJniTypeName(IJavaPeerable) |
Gets the JNI name of the type of the instance |
| JavaAs<TResult>(IJavaPeerable) |
Try to coerce |
| JavaCast<TResult>(IJavaObject) |
Performs an Android runtime-checked type conversion. |
| JavaCast<TResult>(IJavaObject) | |
| TryJavaCast<TResult>(IJavaPeerable, TResult) |
Try to coerce |