Added in API level 29

DelegatedAdminReceiver


open class DelegatedAdminReceiver : BroadcastReceiver

Base class for delegated apps to handle callbacks related to their delegated capabilities.

Delegated apps are apps that receive additional capabilities from the profile owner or device owner apps. Some of these capabilities involve the framework calling into the apps. To receive these callbacks, delegated apps should subclass this class and override the appropriate methods here. The subclassed receiver needs to be published in the app's manifest, with appropriate intent filters to mark which callbacks the receiver is interested in. An app can have multiple receivers as long as they listen for disjoint set of callbacks. For the manifest definitions, it must be protected by the android.Manifest.permission#BIND_DEVICE_ADMIN permission to ensure only the system can trigger these callbacks.

The callback methods happen on the main thread of the process. Thus long running operations must be done on another thread. Note that because a receiver is done once returning from its onReceive function, such long-running operations should probably be done in a Service.

Summary

Public constructors

Public methods
open String?
onChoosePrivateKeyAlias(context: Context, intent: Intent, uid: Int, uri: Uri?, alias: String?)

Allows this receiver to select the alias for a private key and certificate pair for authentication.

open Unit
onNetworkLogsAvailable(context: Context, intent: Intent, batchToken: Long, networkLogsCount: Int)

Called each time a new batch of network logs can be retrieved.

Unit
onReceive(context: Context, intent: Intent)

Intercept delegated device administrator broadcasts.

open Unit

Called each time a new batch of security logs can be retrieved.

Inherited functions
Unit abortBroadcast()

Sets the flag indicating that this receiver should abort the current broadcast; only works with broadcasts sent through Context.sendOrderedBroadcast. This will prevent any other broadcast receivers from receiving the broadcast. It will still call onReceive of the BroadcastReceiver that the caller of Context.sendOrderedBroadcast passed in.

This method does not work with non-ordered broadcasts such as those sent with Context.sendBroadcast

Unit clearAbortBroadcast()

Clears the flag indicating that this receiver should abort the current broadcast.

Boolean getAbortBroadcast()

Returns the flag indicating whether or not this receiver should abort the current broadcast.

Boolean getDebugUnregister()

Return the last value given to setDebugUnregister.

Int getResultCode()

Retrieve the current result code, as set by the previous receiver.

String! getResultData()

Retrieve the current result data, as set by the previous receiver. Often this is null.

Bundle! getResultExtras(makeMap: Boolean)

Retrieve the current result extra data, as set by the previous receiver. Any changes you make to the returned Map will be propagated to the next receiver.

String? getSentFromPackage()

Returns the package name of the app that initially sent this broadcast.

Int getSentFromUid()

Returns the uid of the app that initially sent this broadcast.

BroadcastReceiver.PendingResult! goAsync()

This can be called by an application in onReceive to allow it to keep the broadcast active after returning from that function. This does not change the expectation of being relatively responsive to the broadcast, but does allow the implementation to move work related to it over to another thread to avoid glitching the main UI thread due to disk IO.

As a general rule, broadcast receivers are allowed to run for up to 10 seconds before the system will consider them non-responsive and ANR the app. Since these usually execute on the app's main thread, they are already bound by the ~5 second time limit of various operations that can happen there (not to mention just avoiding UI jank), so the receive limit is generally not of concern. However, once you use goAsync, though able to be off the main thread, the broadcast execution limit still applies, and that includes the time spent between calling this method and ultimately PendingResult.finish().

If you are taking advantage of this method to have more time to execute, it is useful to know that the available time can be longer in certain situations. In particular, if the broadcast you are receiving is not a foreground broadcast (that is, the sender has not used Intent.FLAG_RECEIVER_FOREGROUND), then more time is allowed for the receivers to run, allowing them to execute for 30 seconds or even a bit more. This is something that receivers should rarely take advantage of (long work should be punted to another system facility such as android.app.job.JobScheduler, android.app.Service, or see especially androidx.core.app.JobIntentService), but can be useful in certain rare cases where it is necessary to do some work as soon as the broadcast is delivered. Keep in mind that the work you do here will block further broadcasts until it completes, so taking advantage of this at all excessively can be counter-productive and cause later events to be received more slowly.

Boolean isInitialStickyBroadcast()

Returns true if the receiver is currently processing the initial value of a sticky broadcast -- that is, the value that was last broadcast and is currently held in the sticky cache, so this is not directly the result of a broadcast right now.

Boolean isOrderedBroadcast()

Returns true if the receiver is currently processing an ordered broadcast.

IBinder! peekService(myContext: Context!, service: Intent!)

Provide a binder to an already-bound service. This method is synchronous and will not start the target service if it is not present, so it is safe to call from onReceive. For peekService() to return a non null android.os.IBinder interface the service must have published it before. In other words some component must have called android.content.Context#bindService(Intent, ServiceConnection, int) on it.

Unit setDebugUnregister(debug: Boolean)

Control inclusion of debugging help for mismatched calls to Context.registerReceiver(). If called with true, before given to registerReceiver(), then the callstack of the following Context.unregisterReceiver() call is retained, to be printed if a later incorrect unregister call is made. Note that doing this requires retaining information about the BroadcastReceiver for the lifetime of the app, resulting in a leak -- this should only be used for debugging.

Unit setOrderedHint(isOrdered: Boolean)

For internal use, sets the hint about whether this BroadcastReceiver is running in ordered mode.

Unit setResult(code: Int, data: String!, extras: Bundle!)

Change all of the result data returned from this broadcasts; only works with broadcasts sent through Context.sendOrderedBroadcast. All current result data is replaced by the value given to this method.

This method does not work with non-ordered broadcasts such as those sent with Context.sendBroadcast

Unit setResultCode(code: Int)

Change the current result code of this broadcast; only works with broadcasts sent through Context.sendOrderedBroadcast. Often uses the Activity android.app.Activity#RESULT_CANCELED and android.app.Activity#RESULT_OK constants, though the actual meaning of this value is ultimately up to the broadcaster.

This method does not work with non-ordered broadcasts such as those sent with Context.sendBroadcast

Unit setResultData(data: String!)

Change the current result data of this broadcast; only works with broadcasts sent through Context.sendOrderedBroadcast. This is an arbitrary string whose interpretation is up to the broadcaster.

This method does not work with non-ordered broadcasts such as those sent with Context.sendBroadcast

Unit setResultExtras(extras: Bundle!)

Change the current result extras of this broadcast; only works with broadcasts sent through Context.sendOrderedBroadcast. This is a Bundle holding arbitrary data, whose interpretation is up to the broadcaster. Can be set to null. Calling this method completely replaces the current map (if any).

This method does not work with non-ordered broadcasts such as those sent with Context.sendBroadcast

Public constructors

DelegatedAdminReceiver

DelegatedAdminReceiver()

Public methods

onChoosePrivateKeyAlias

Added in API level 29
open fun onChoosePrivateKeyAlias(
    context: Context,
    intent: Intent,
    uid: Int,
    uri: Uri?,
    alias: String?
): String?

Allows this receiver to select the alias for a private key and certificate pair for authentication. If this method returns null, the default android.app.Activity will be shown that lets the user pick a private key and certificate pair. If this method returns KeyChain.KEY_ALIAS_SELECTION_DENIED, the default android.app.Activity will not be shown and the user will not be allowed to pick anything. And the app, that called android.security.KeyChain#choosePrivateKeyAlias, will receive null back.

This callback is only applicable if the delegated app has DevicePolicyManager.DELEGATION_CERT_SELECTION capability. Additionally, it must declare an intent filter for DeviceAdminReceiver.ACTION_CHOOSE_PRIVATE_KEY_ALIAS in the receiver's manifest in order to receive this callback. The default implementation simply throws UnsupportedOperationException.

Parameters
context Context: The running context as per onReceive. This value cannot be null.
intent Intent: The received intent as per onReceive. This value cannot be null.
uid Int: The uid of the app asking for the private key and certificate pair.
uri Uri?: The URI to authenticate, may be null.
alias String?: The alias preselected by the client, or null.
Return
String? The private key alias to return and grant access to.

See Also

    onNetworkLogsAvailable

    Added in API level 29
    open fun onNetworkLogsAvailable(
        context: Context,
        intent: Intent,
        batchToken: Long,
        networkLogsCount: Int
    ): Unit

    Called each time a new batch of network logs can be retrieved. This callback method will only ever be called when network logging is enabled. The logs can only be retrieved while network logging is enabled.

    If a secondary user or profile is created, this callback won't be received until all users become affiliated again (even if network logging is enabled). It will also no longer be possible to retrieve the network logs batch with the most recent batchToken provided by this callback. See DevicePolicyManager.setAffiliationIds.

    This callback is only applicable if the delegated app has DevicePolicyManager.DELEGATION_NETWORK_LOGGING capability. Additionally, it must declare an intent filter for DeviceAdminReceiver.ACTION_NETWORK_LOGS_AVAILABLE in the receiver's manifest in order to receive this callback. The default implementation simply throws UnsupportedOperationException.

    This callback is triggered by a foreground broadcast and the app should ensure that any long-running work is not executed synchronously inside the callback.

    Parameters
    context Context: The running context as per onReceive. This value cannot be null.
    intent Intent: The received intent as per onReceive. This value cannot be null.
    batchToken Long: The token representing the current batch of network logs.
    networkLogsCount Int: The total count of events in the current batch of network logs. Value is 1 or greater

    onReceive

    Added in API level 29
    fun onReceive(
        context: Context,
        intent: Intent
    ): Unit

    Intercept delegated device administrator broadcasts. Implementations should not override this method; implement the convenience callbacks for each action instead.

    Parameters
    context Context: This value cannot be null.
    intent Intent: This value cannot be null.

    onSecurityLogsAvailable

    Added in API level 31
    open fun onSecurityLogsAvailable(
        context: Context,
        intent: Intent
    ): Unit

    Called each time a new batch of security logs can be retrieved. This callback method will only ever be called when security logging is enabled. The logs can only be retrieved while security logging is enabled.

    If a secondary user or profile is created, this callback won't be received until all users become affiliated again (even if security logging is enabled). It will also no longer be possible to retrieve the security logs. See DevicePolicyManager.setAffiliationIds.

    This callback is only applicable if the delegated app has DevicePolicyManager.DELEGATION_SECURITY_LOGGING capability. Additionally, it must declare an intent filter for DeviceAdminReceiver.ACTION_SECURITY_LOGS_AVAILABLE in the receiver's manifest in order to receive this callback. The default implementation simply throws UnsupportedOperationException.

    This callback is triggered by a foreground broadcast and the app should ensure that any long-running work is not executed synchronously inside the callback.

    Parameters
    context Context: The running context as per onReceive. This value cannot be null.
    intent Intent: The received intent as per onReceive. This value cannot be null.