Added in API level 34

PolicyUpdateReceiver


abstract class PolicyUpdateReceiver : BroadcastReceiver

Base class for implementing a policy update receiver. This class provides a convenience for interpreting the raw intent actions (ACTION_DEVICE_POLICY_SET_RESULT and ACTION_DEVICE_POLICY_CHANGED) that are sent by the system.

The callback methods happen on the main thread of the process. Thus, long-running operations must be done on another thread.

When publishing your PolicyUpdateReceiver subclass as a receiver, it must require the android.Manifest.permission#BIND_DEVICE_ADMIN permission.

Admins can implement DeviceAdminService to ensure they receive all policy updates (for policies they have set) via onPolicyChanged by constantly being bound to by the system. For more information see DeviceAdminService.

Summary

Constants
static String

Action for a broadcast sent to admins to communicate back a change in a policy they have previously set.

static String

Action for a broadcast sent to admins to communicate back the result of setting a policy in DevicePolicyManager.

static String

A string extra holding the account type this policy applies to, (see PolicyUpdateReceiver.onPolicyChanged and PolicyUpdateReceiver.onPolicySetResult)

static String

An android.content.IntentFilter extra holding the intent filter the policy relates to, (see PolicyUpdateReceiver.onPolicyChanged and PolicyUpdateReceiver.onPolicySetResult)

static String

A string extra holding the package name the policy applies to, (see PolicyUpdateReceiver.onPolicyChanged and PolicyUpdateReceiver.onPolicySetResult)

static String

A string extra holding the permission name the policy applies to, (see PolicyUpdateReceiver.onPolicyChanged and PolicyUpdateReceiver.onPolicySetResult)

Public constructors

Public methods
open Unit
onPolicyChanged(context: Context, policyIdentifier: String, additionalPolicyParams: Bundle, targetUser: TargetUser, policyUpdateResult: PolicyUpdateResult)

Callback triggered when a policy previously set by the admin has changed.

open Unit
onPolicySetResult(context: Context, policyIdentifier: String, additionalPolicyParams: Bundle, targetUser: TargetUser, policyUpdateResult: PolicyUpdateResult)

Callback triggered after an admin has set a policy using one of the APIs in DevicePolicyManager to notify the admin whether it has been successful or not.

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

Constants

ACTION_DEVICE_POLICY_CHANGED

Added in API level 34
static val ACTION_DEVICE_POLICY_CHANGED: String

Action for a broadcast sent to admins to communicate back a change in a policy they have previously set.

Admins wishing to receive these updates should include this action in the intent filter for their receiver in the manifest, the receiver must be protected by android.Manifest.permission#BIND_DEVICE_ADMIN to ensure that only the system can send updates.

Admins shouldn't implement onReceive and should instead implement onPolicyChanged.

Value: "android.app.admin.action.DEVICE_POLICY_CHANGED"

ACTION_DEVICE_POLICY_SET_RESULT

Added in API level 34
static val ACTION_DEVICE_POLICY_SET_RESULT: String

Action for a broadcast sent to admins to communicate back the result of setting a policy in DevicePolicyManager.

Admins wishing to receive these updates (via onPolicySetResult) should include this action in the intent filter for their receiver in the manifest, the receiver must be protected by android.Manifest.permission#BIND_DEVICE_ADMIN to ensure that only the system can send updates.

Admins shouldn't implement onReceive and should instead implement onPolicySetResult.

Value: "android.app.admin.action.DEVICE_POLICY_SET_RESULT"

EXTRA_ACCOUNT_TYPE

Added in API level 34
static val EXTRA_ACCOUNT_TYPE: String

A string extra holding the account type this policy applies to, (see PolicyUpdateReceiver.onPolicyChanged and PolicyUpdateReceiver.onPolicySetResult)

Value: "android.app.admin.extra.ACCOUNT_TYPE"

EXTRA_INTENT_FILTER

Added in API level 34
static val EXTRA_INTENT_FILTER: String

An android.content.IntentFilter extra holding the intent filter the policy relates to, (see PolicyUpdateReceiver.onPolicyChanged and PolicyUpdateReceiver.onPolicySetResult)

Value: "android.app.admin.extra.INTENT_FILTER"

EXTRA_PACKAGE_NAME

Added in API level 34
static val EXTRA_PACKAGE_NAME: String

A string extra holding the package name the policy applies to, (see PolicyUpdateReceiver.onPolicyChanged and PolicyUpdateReceiver.onPolicySetResult)

Value: "android.app.admin.extra.PACKAGE_NAME"

EXTRA_PERMISSION_NAME

Added in API level 34
static val EXTRA_PERMISSION_NAME: String

A string extra holding the permission name the policy applies to, (see PolicyUpdateReceiver.onPolicyChanged and PolicyUpdateReceiver.onPolicySetResult)

Value: "android.app.admin.extra.PERMISSION_NAME"

Public constructors

PolicyUpdateReceiver

PolicyUpdateReceiver()

Public methods

onPolicyChanged

Added in API level 34
open fun onPolicyChanged(
    context: Context,
    policyIdentifier: String,
    additionalPolicyParams: Bundle,
    targetUser: TargetUser,
    policyUpdateResult: PolicyUpdateResult
): Unit

Callback triggered when a policy previously set by the admin has changed.

Admins wishing to receive this callback should include PolicyUpdateReceiver.ACTION_DEVICE_POLICY_CHANGED in the intent filter for their receiver in the manifest, the receiver must be protected by android.Manifest.permission#BIND_DEVICE_ADMIN to ensure that only the system can send updates.

Parameters
context Context: the running context as per onReceive This value cannot be null.
policyIdentifier String: Key to identify which policy this callback relates to. This value cannot be null.
additionalPolicyParams Bundle: Bundle containing additional params that may be required to identify some of the policy (e.g. PolicyUpdateReceiver.EXTRA_PACKAGE_NAME and PolicyUpdateReceiver.EXTRA_PERMISSION_NAME). Each policy will document the required additional params if needed. This value cannot be null.
targetUser TargetUser: The TargetUser which this policy relates to. This value cannot be null.
policyUpdateResult PolicyUpdateResult: Indicates the reason the policy value has changed (e.g. PolicyUpdateResult.RESULT_POLICY_SET if the policy has changed to the value set by the admin, PolicyUpdateResult.RESULT_FAILURE_CONFLICTING_ADMIN_POLICY if the policy has changed because another admin has set a conflicting policy, etc) This value cannot be null.

onPolicySetResult

Added in API level 34
open fun onPolicySetResult(
    context: Context,
    policyIdentifier: String,
    additionalPolicyParams: Bundle,
    targetUser: TargetUser,
    policyUpdateResult: PolicyUpdateResult
): Unit

Callback triggered after an admin has set a policy using one of the APIs in DevicePolicyManager to notify the admin whether it has been successful or not.

Admins wishing to receive this callback should include PolicyUpdateReceiver.ACTION_DEVICE_POLICY_SET_RESULT in the intent filter for their receiver in the manifest, the receiver must be protected by android.Manifest.permission#BIND_DEVICE_ADMIN to ensure that only the system can send updates.

Parameters
context Context: the running context as per onReceive This value cannot be null.
policyIdentifier String: Key to identify which policy this callback relates to. This value cannot be null.
additionalPolicyParams Bundle: Bundle containing additional params that may be required to identify some of the policy (e.g. PolicyUpdateReceiver.EXTRA_PACKAGE_NAME and PolicyUpdateReceiver.EXTRA_PERMISSION_NAME). Each policy will document the required additional params if needed. This value cannot be null.
targetUser TargetUser: The TargetUser which this policy relates to. This value cannot be null.
policyUpdateResult PolicyUpdateResult: Indicates whether the policy has been set successfully (PolicyUpdateResult.RESULT_POLICY_SET) or the reason it failed to apply (e.g. PolicyUpdateResult.RESULT_FAILURE_CONFLICTING_ADMIN_POLICY, etc). This value cannot be null.