Added in API level 26

Callback

abstract class Callback
kotlin.Any
   ↳ android.companion.CompanionDeviceManager.Callback

Callback for applications to receive updates about and the outcome of AssociationRequest issued via associate() call.

The Callback#onAssociationPending(IntentSender) is invoked after the AssociationRequest has been checked by the Companion Device Manager Service and is pending user's approval. The IntentSender received as an argument to Callback#onAssociationPending(IntentSender) "encapsulates" an Activity that has UI for the user to:

  • choose the device to associate the application with (if multiple eligible devices are available)
  • confirm the association
  • approve the privileges the application will be granted if the association is to be created
If the Companion Device Manager Service needs to scan for the devices, the Activity will also display the status and the progress of the scan. Note that Companion Device Manager Service will only start the scanning after the Activity was launched and became visible. Applications are expected to launch the UI using the received IntentSender via Activity#startIntentSenderForResult(IntentSender, int, Intent, int, int, int).

Upon receiving user's confirmation Companion Device Manager Service will create an association and will send an AssociationInfo object that represents the created association back to the application both via Callback#onAssociationCreated(AssociationInfo) and via Activity#setResult(int, Intent). In the latter the resultCode will be set to Activity#RESULT_OK and the data Intent will contain AssociationInfo extra named EXTRA_ASSOCIATION.

<code>
    if (resultCode == Activity.RESULT_OK) {
      AssociationInfo associationInfo = data.getParcelableExtra(EXTRA_ASSOCIATION);
    }
  </code>

If the Companion Device Manager Service is not able to create an association, it will invoke Callback#onFailure(CharSequence). If this happened after the application has launched the UI (eg. the user chose to reject the association), the outcome will also be delivered to the applications via Activity#setResult(int) with the Activity#RESULT_CANCELED resultCode.

Note that in some cases the Companion Device Manager Service may not need to collect user's approval for creating an association. In such cases, this method will not be invoked, and onAssociationCreated(android.companion.AssociationInfo) may be invoked right away.

Summary

Public constructors

Public methods
open Unit

Invoked when the association is created.

open Unit

Invoked when the association needs to approved by the user.

open Unit
onDeviceFound(intentSender: IntentSender)

abstract Unit

Invoked if the association could not be created.

Public constructors

Callback

Callback()

Public methods

onAssociationCreated

Added in API level 33
open fun onAssociationCreated(associationInfo: AssociationInfo): Unit

Invoked when the association is created.

Parameters
associationInfo AssociationInfo: contains details of the newly-established association. This value cannot be null.

onAssociationPending

Added in API level 33
open fun onAssociationPending(intentSender: IntentSender): Unit

Invoked when the association needs to approved by the user. Applications should launch the Activity "encapsulated" in intentSender IntentSender object by calling Activity#startIntentSenderForResult(IntentSender, int, Intent, int, int, int).

Parameters
intentSender IntentSender: an IntentSender which applications should use to launch the UI for the user to confirm the association. This value cannot be null.

onDeviceFound

Added in API level 26
Deprecated in API level 33
open fun onDeviceFound(intentSender: IntentSender): Unit

Deprecated: method was renamed to onAssociationPending() to provide better clarity; both methods are functionally equivalent and only one needs to be overridden.

Parameters
intentSender IntentSender: This value cannot be null.

onFailure

Added in API level 26
abstract fun onFailure(error: CharSequence?): Unit

Invoked if the association could not be created.

Parameters
error CharSequence?: error message. This value may be null.