HandlerThread Class

Definition

A Thread that has a Looper.

[Android.Runtime.Register("android/os/HandlerThread", DoNotGenerateAcw=true)]
public class HandlerThread : Java.Lang.Thread
[<Android.Runtime.Register("android/os/HandlerThread", DoNotGenerateAcw=true)>]
type HandlerThread = class
    inherit Thread
Inheritance
HandlerThread
Attributes

Remarks

A Thread that has a Looper. The Looper can then be used to create Handlers. Just like with a regular Thread, #start() must still be called.

<em>Use this class only if you must work with the Handler API and need a Thread to do the handling on that is not an existing Looper thread, such as Looper#getMainLooper().</em> Otherwise, prefer java.util.concurrent.Executor or java.util.concurrent.ExecutorService, or Kotlin coroutines.

Note that many APIs that required a Handler in older SDK versions offer a newer alternative that accepts an java.util.concurrent.Executor instead. Always prefer to use the newer API if available.

<h2>Alternatives to HandlerThread</h2>

java.util.concurrent.Executors offer more flexibility with regards to threading. Work submitted to an java.util.concurrent.Executor can be set to run on another thread, on one of several threads from a static or dynamic pool, or on the caller's thread, depending on your needs.

{link

Java documentation for android.os.HandlerThread.

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
HandlerThread(IntPtr, JniHandleOwnership)

A constructor used when creating managed representations of JNI objects; called by the runtime.

HandlerThread(String, Int32)

Constructs a HandlerThread.

HandlerThread(String)

Constructs a HandlerThread.

Fields

Name Description
MaxPriority

The maximum priority that a thread can have.

(Inherited from Thread)
MinPriority

The minimum priority that a thread can have.

(Inherited from Thread)
NormPriority

The default priority that is assigned to a thread.

(Inherited from Thread)

Properties

Name Description
Class

Returns the runtime class of this Object.

(Inherited from Object)
ContextClassLoader

Returns the context ClassLoader for this thread. -or- Sets the context ClassLoader for this Thread.

(Inherited from Thread)
Daemon

Tests if this thread is a daemon thread.

(Inherited from Thread)
Handle

The handle to the underlying Android instance.

(Inherited from Object)
Id

Returns the identifier of this Thread.

(Inherited from Thread)
IsAlive

Tests if this thread is alive.

(Inherited from Thread)
IsInterrupted

Tests whether this thread has been interrupted.

(Inherited from Thread)
IsVirtual

Returns true if this thread is a virtual thread.

(Inherited from Thread)
JniIdentityHashCode

Gets the identity hash code assigned to this Java peer by the interop runtime.

(Inherited from Object)
JniManagedPeerState (Inherited from JavaObject)
JniPeerMembers

Provides JNI peer-member metadata for this type.

Looper

This method returns the Looper associated with this thread.

Name

Returns this thread's name. -or- Changes the name of this thread to be equal to the argument name.

(Inherited from Thread)
PeerReference

Gets the JNI object reference for this Java peer.

(Inherited from Object)
Priority

Returns this thread's priority.

(Inherited from Thread)
ThreadGroup

Returns the thread group to which this thread belongs.

(Inherited from Thread)
ThreadId

Returns the identifier of this thread.

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.

UncaughtExceptionHandler

Returns the handler invoked when this thread abruptly terminates due to an uncaught exception. -or- Set the handler invoked when this thread abruptly terminates due to an uncaught exception.

(Inherited from Thread)

Methods

Name Description
CheckAccess()

Determines if the currently running thread has permission to modify this thread.

(Inherited from Thread)
Clone()

Creates and returns a copy of this object.

(Inherited from Object)
Construct(JniObjectReference, JniObjectReferenceOptions) (Inherited from JavaObject)
CountStackFrames()
Obsolete.

Throws UnsupportedOperationException.

(Inherited from Thread)
Destroy()
Obsolete.

Throws UnsupportedOperationException.

(Inherited from Thread)
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)
DisposeUnlessReferenced() (Inherited from JavaObject)
Equals(Object) (Inherited from JavaObject)
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)
GetStackTrace()

Returns an array of stack trace elements representing the stack dump of this thread.

(Inherited from Thread)
GetState()

Returns the state of this thread.

(Inherited from Thread)
Interrupt()

Interrupts this thread.

(Inherited from Thread)
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)
Join()

Waits for this thread to die.

(Inherited from Thread)
Join(Int64, Int32)

Waits at most millis milliseconds plus nanos nanoseconds for this thread to die.

(Inherited from Thread)
Join(Int64)

Waits at most millis milliseconds for this thread to die.

(Inherited from Thread)
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)
OnLooperPrepared()

Call back method that can be explicitly overridden if needed to execute some setup before Looper loops.

Quit()

Quits the handler thread's looper.

QuitSafely()

Quits the handler thread's looper safely.

Resume()
Obsolete.

Throws UnsupportedOperationException.

(Inherited from Thread)
Run()

If this thread was constructed using a separate Runnable run object, then that Runnable object's run method is called; otherwise, this method does nothing and returns.

(Inherited from Thread)
SetHandle(IntPtr, JniHandleOwnership)

Sets the Handle property.

(Inherited from Object)
SetPeerReference(JniObjectReference, JniObjectReferenceOptions) (Inherited from JavaObject)
Start()

Causes this thread to begin execution; the Java Virtual Machine calls the run method of this thread.

(Inherited from Thread)
Stop()
Obsolete.

Throws UnsupportedOperationException.

(Inherited from Thread)
Stop(Throwable)
Obsolete.

Throws UnsupportedOperationException.

(Inherited from Thread)
Suspend()
Obsolete.

Throws UnsupportedOperationException.

(Inherited from Thread)
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() (Inherited from JavaObject)
IJavaPeerable.Finalized() (Inherited from JavaObject)
IJavaPeerable.JniObjectReferenceControlBlock (Inherited from JavaObject)
IJavaPeerable.SetJniIdentityHashCode(Int32) (Inherited from JavaObject)
IJavaPeerable.SetJniManagedPeerState(JniManagedPeerStates) (Inherited from JavaObject)
IJavaPeerable.SetPeerReference(JniObjectReference) (Inherited from JavaObject)
IJavaPeerable.UnregisterFromRuntime() (Inherited from Thread)

Extension Methods

Name Description
GetJniTypeName(IJavaPeerable)

Gets the JNI name of the type of the instance self.

JavaAs<TResult>(IJavaPeerable)

Try to coerce self to type TResult, checking that the coercion is valid on the Java side.

JavaCast<TResult>(IJavaObject)

Performs an Android runtime-checked type conversion.

JavaCast<TResult>(IJavaObject)
TryJavaCast<TResult>(IJavaPeerable, TResult)

Try to coerce self to type TResult, checking that the coercion is valid on the Java side.

Applies to