Stay organized with collections
Save and categorize content based on your preferences.
CancellationSignal
class CancellationSignal
Provides the ability to cancel an operation in progress.
Summary
Nested classes |
abstract |
Listens for cancellation.
|
Public constructors |
Creates a cancellation signal, initially not canceled.
|
Public methods |
Unit |
Cancels the operation and signals the cancellation listener.
|
Boolean |
Returns true if the operation has been canceled.
|
Unit |
Sets the cancellation listener to be called when canceled.
|
Unit |
Throws OperationCanceledException if the operation has been canceled.
|
Public constructors
CancellationSignal
CancellationSignal()
Creates a cancellation signal, initially not canceled.
Public methods
cancel
fun cancel(): Unit
Cancels the operation and signals the cancellation listener. If the operation has not yet started, then it will be canceled as soon as it does.
isCanceled
fun isCanceled(): Boolean
Returns true if the operation has been canceled.
Return |
Boolean |
True if the operation has been canceled. |
setOnCancelListener
fun setOnCancelListener(listener: CancellationSignal.OnCancelListener!): Unit
Sets the cancellation listener to be called when canceled. This method is intended to be used by the recipient of a cancellation signal such as a database or a content provider to handle cancellation requests while performing a long-running operation. This method is not intended to be used by applications themselves. If CancellationSignal.cancel
has already been called, then the provided listener is invoked immediately. This method is guaranteed that the listener will not be called after it has been removed.
throwIfCanceled
fun throwIfCanceled(): Unit
Throws OperationCanceledException
if the operation has been canceled.
Exceptions |
android.os.OperationCanceledException |
if the operation has been canceled. |
Content and code samples on this page are subject to the licenses described in the Content License. Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
Last updated 2025-02-10 UTC.
[null,null,["Last updated 2025-02-10 UTC."],[],[],null,["# CancellationSignal\n\nAdded in [API level 16](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)\n\nCancellationSignal\n==================\n\n*** ** * ** ***\n\nKotlin \\|[Java](/reference/android/os/CancellationSignal \"View this page in Java\") \n\n```\nclass CancellationSignal\n```\n\n|---|------------------------------------|\n| [kotlin.Any](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-any/index.html) ||\n| ↳ | [android.os.CancellationSignal](#) |\n\nProvides the ability to cancel an operation in progress.\n\nSummary\n-------\n\n| Nested classes ||\n|----------|----------------------------------------------------------------------------------------------------------------|\n| abstract | [OnCancelListener](/reference/kotlin/android/os/CancellationSignal.OnCancelListener) Listens for cancellation. |\n\n| Public constructors ||\n|--------------------------------------------------------------------------------------------------------|---|\n| [CancellationSignal](#CancellationSignal())`()` Creates a cancellation signal, initially not canceled. |\n\n| Public methods ||\n|------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| [Unit](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-unit/index.html) | [cancel](#cancel())`()` Cancels the operation and signals the cancellation listener. |\n| [Boolean](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-boolean/index.html) | [isCanceled](#isCanceled())`()` Returns true if the operation has been canceled. |\n| [Unit](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-unit/index.html) | [setOnCancelListener](#setOnCancelListener(android.os.CancellationSignal.OnCancelListener))`(`listener:` `[CancellationSignal.OnCancelListener](/reference/kotlin/android/os/CancellationSignal.OnCancelListener)!`)` Sets the cancellation listener to be called when canceled. |\n| [Unit](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-unit/index.html) | [throwIfCanceled](#throwIfCanceled())`()` Throws [OperationCanceledException](/reference/kotlin/android/os/OperationCanceledException) if the operation has been canceled. |\n\nPublic constructors\n-------------------\n\n### CancellationSignal\n\nAdded in [API level 16](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels) \n\n```\nCancellationSignal()\n```\n\nCreates a cancellation signal, initially not canceled.\n\nPublic methods\n--------------\n\n### cancel\n\nAdded in [API level 16](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels) \n\n```\nfun cancel(): Unit\n```\n\nCancels the operation and signals the cancellation listener. If the operation has not yet started, then it will be canceled as soon as it does. \n\n### isCanceled\n\nAdded in [API level 16](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels) \n\n```\nfun isCanceled(): Boolean\n```\n\nReturns true if the operation has been canceled.\n\n| Return ||\n|------------------------------------------------------------------------------------|------------------------------------------|\n| [Boolean](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-boolean/index.html) | True if the operation has been canceled. |\n\n### setOnCancelListener\n\nAdded in [API level 16](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels) \n\n```\nfun setOnCancelListener(listener: CancellationSignal.OnCancelListener!): Unit\n```\n\nSets the cancellation listener to be called when canceled. This method is intended to be used by the recipient of a cancellation signal such as a database or a content provider to handle cancellation requests while performing a long-running operation. This method is not intended to be used by applications themselves. If [CancellationSignal.cancel](#cancel()) has already been called, then the provided listener is invoked immediately. This method is guaranteed that the listener will not be called after it has been removed.\n\n| Parameters ||\n|------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `listener` | [CancellationSignal.OnCancelListener](/reference/kotlin/android/os/CancellationSignal.OnCancelListener)!: The cancellation listener, or null to remove the current listener. |\n\n### throwIfCanceled\n\nAdded in [API level 16](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels) \n\n```\nfun throwIfCanceled(): Unit\n```\n\nThrows [OperationCanceledException](/reference/kotlin/android/os/OperationCanceledException) if the operation has been canceled.\n\n| Exceptions ||\n|-----------------------------------------|-------------------------------------|\n| `android.os.OperationCanceledException` | if the operation has been canceled. |"]]