Context.RegisterReceiver Method

Definition

Overloads

RegisterReceiver(BroadcastReceiver, IntentFilter, String, Handler, ReceiverFlags)
RegisterReceiver(BroadcastReceiver, IntentFilter, String, Handler)

Register to receive intent broadcasts, to run in the context of <var>scheduler</var>.

RegisterReceiver(BroadcastReceiver, IntentFilter, String, Handler, ActivityFlags)
Obsolete.

Register to receive intent broadcasts, to run in the context of <var>scheduler</var>.

RegisterReceiver(BroadcastReceiver, IntentFilter, ActivityFlags)
Obsolete.

Register to receive intent broadcasts, with the receiver optionally being exposed to Instant Apps.

RegisterReceiver(BroadcastReceiver, IntentFilter, ReceiverFlags)
RegisterReceiver(BroadcastReceiver, IntentFilter)

Register a BroadcastReceiver to be run in the main activity thread.

RegisterReceiver(BroadcastReceiver, IntentFilter, String, Handler, ReceiverFlags)

public Android.Content.Intent? RegisterReceiver (Android.Content.BroadcastReceiver? receiver, Android.Content.IntentFilter? filter, string? broadcastPermission, Android.OS.Handler? scheduler, Android.Content.ReceiverFlags flags);
member this.RegisterReceiver : Android.Content.BroadcastReceiver * Android.Content.IntentFilter * string * Android.OS.Handler * Android.Content.ReceiverFlags -> Android.Content.Intent

Parameters

filter
IntentFilter
broadcastPermission
String
scheduler
Handler

Returns

Applies to

RegisterReceiver(BroadcastReceiver, IntentFilter, String, Handler)

Register to receive intent broadcasts, to run in the context of <var>scheduler</var>.

[Android.Runtime.Register("registerReceiver", "(Landroid/content/BroadcastReceiver;Landroid/content/IntentFilter;Ljava/lang/String;Landroid/os/Handler;)Landroid/content/Intent;", "GetRegisterReceiver_Landroid_content_BroadcastReceiver_Landroid_content_IntentFilter_Ljava_lang_String_Landroid_os_Handler_Handler")]
public abstract Android.Content.Intent? RegisterReceiver (Android.Content.BroadcastReceiver? receiver, Android.Content.IntentFilter? filter, string? broadcastPermission, Android.OS.Handler? scheduler);
[<Android.Runtime.Register("registerReceiver", "(Landroid/content/BroadcastReceiver;Landroid/content/IntentFilter;Ljava/lang/String;Landroid/os/Handler;)Landroid/content/Intent;", "GetRegisterReceiver_Landroid_content_BroadcastReceiver_Landroid_content_IntentFilter_Ljava_lang_String_Landroid_os_Handler_Handler")>]
abstract member RegisterReceiver : Android.Content.BroadcastReceiver * Android.Content.IntentFilter * string * Android.OS.Handler -> Android.Content.Intent

Parameters

receiver
BroadcastReceiver

The BroadcastReceiver to handle the broadcast.

filter
IntentFilter

Selects the Intent broadcasts to be received.

broadcastPermission
String

String naming a permissions that a broadcaster must hold in order to send an Intent to you. If null, no permission is required.

scheduler
Handler

Handler identifying the thread that will receive the Intent. If null, the main thread of the process will be used.

Returns

The first sticky intent found that matches <var>filter</var>, or null if there are none.

Attributes

Remarks

Register to receive intent broadcasts, to run in the context of <var>scheduler</var>. See #registerReceiver(BroadcastReceiver, IntentFilter) for more information. This allows you to enforce permissions on who can broadcast intents to your receiver, or have the receiver run in a different thread than the main application thread.

See BroadcastReceiver for more information on Intent broadcasts.

As of android.os.Build.VERSION_CODES#ICE_CREAM_SANDWICH, receivers registered with this method will correctly respect the Intent#setPackage(String) specified for an Intent being broadcast. Prior to that, it would be ignored and delivered to all matching registered receivers. Be careful if using this for security.

For apps targeting android.os.Build.VERSION_CODES#UPSIDE_DOWN_CAKE, either #RECEIVER_EXPORTED or #RECEIVER_NOT_EXPORTED must be specified if the receiver is not being registered for system broadcasts or a SecurityException will be thrown. See #registerReceiver(BroadcastReceiver, IntentFilter, String, Handler, int) to register a receiver with flags.

Java documentation for android.content.Context.registerReceiver(android.content.BroadcastReceiver, android.content.IntentFilter, java.lang.String, android.os.Handler).

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.

See also

Applies to

RegisterReceiver(BroadcastReceiver, IntentFilter, String, Handler, ActivityFlags)

Caution

This method has an incorrect enumeration type. Use the overload that takes ReceiverFlags instead.

Register to receive intent broadcasts, to run in the context of <var>scheduler</var>.

[Android.Runtime.Register("registerReceiver", "(Landroid/content/BroadcastReceiver;Landroid/content/IntentFilter;Ljava/lang/String;Landroid/os/Handler;I)Landroid/content/Intent;", "GetRegisterReceiver_Landroid_content_BroadcastReceiver_Landroid_content_IntentFilter_Ljava_lang_String_Landroid_os_Handler_IHandler", ApiSince=26)]
[System.Obsolete("This method has an incorrect enumeration type. Use the overload that takes ReceiverFlags instead.")]
public abstract Android.Content.Intent? RegisterReceiver (Android.Content.BroadcastReceiver? receiver, Android.Content.IntentFilter? filter, string? broadcastPermission, Android.OS.Handler? scheduler, Android.Content.ActivityFlags flags);
[<Android.Runtime.Register("registerReceiver", "(Landroid/content/BroadcastReceiver;Landroid/content/IntentFilter;Ljava/lang/String;Landroid/os/Handler;I)Landroid/content/Intent;", "GetRegisterReceiver_Landroid_content_BroadcastReceiver_Landroid_content_IntentFilter_Ljava_lang_String_Landroid_os_Handler_IHandler", ApiSince=26)>]
[<System.Obsolete("This method has an incorrect enumeration type. Use the overload that takes ReceiverFlags instead.")>]
abstract member RegisterReceiver : Android.Content.BroadcastReceiver * Android.Content.IntentFilter * string * Android.OS.Handler * Android.Content.ActivityFlags -> Android.Content.Intent

Parameters

receiver
BroadcastReceiver

The BroadcastReceiver to handle the broadcast.

filter
IntentFilter

Selects the Intent broadcasts to be received.

broadcastPermission
String

String naming a permissions that a broadcaster must hold in order to send an Intent to you. If null, no permission is required.

scheduler
Handler

Handler identifying the thread that will receive the Intent. If null, the main thread of the process will be used.

flags
ActivityFlags

Additional options for the receiver. For apps targeting android.os.Build.VERSION_CODES#UPSIDE_DOWN_CAKE either #RECEIVER_EXPORTED or #RECEIVER_NOT_EXPORTED must be specified if the receiver isn't being registered for system broadcasts or a SecurityException will be thrown. If #RECEIVER_EXPORTED is specified, a receiver may additionally specify #RECEIVER_VISIBLE_TO_INSTANT_APPS. For a complete list of system broadcast actions, see the BROADCAST_ACTIONS.TXT file in the Android SDK. If both #RECEIVER_EXPORTED and #RECEIVER_NOT_EXPORTED are specified, an IllegalArgumentException will be thrown.

Returns

The first sticky intent found that matches <var>filter</var>, or null if there are none.

Attributes

Remarks

Register to receive intent broadcasts, to run in the context of <var>scheduler</var>. See #registerReceiver(BroadcastReceiver, IntentFilter, int) and #registerReceiver(BroadcastReceiver, IntentFilter, String, Handler) for more information.

See BroadcastReceiver for more information on Intent broadcasts.

As of android.os.Build.VERSION_CODES#ICE_CREAM_SANDWICH, receivers registered with this method will correctly respect the Intent#setPackage(String) specified for an Intent being broadcast. Prior to that, it would be ignored and delivered to all matching registered receivers. Be careful if using this for security.

Java documentation for android.content.Context.registerReceiver(android.content.BroadcastReceiver, android.content.IntentFilter, java.lang.String, android.os.Handler, int).

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.

Applies to

RegisterReceiver(BroadcastReceiver, IntentFilter, ActivityFlags)

Caution

This method has an incorrect enumeration type. Use the overload that takes ReceiverFlags instead.

Register to receive intent broadcasts, with the receiver optionally being exposed to Instant Apps.

[Android.Runtime.Register("registerReceiver", "(Landroid/content/BroadcastReceiver;Landroid/content/IntentFilter;I)Landroid/content/Intent;", "GetRegisterReceiver_Landroid_content_BroadcastReceiver_Landroid_content_IntentFilter_IHandler", ApiSince=26)]
[System.Obsolete("This method has an incorrect enumeration type. Use the overload that takes ReceiverFlags instead.")]
public abstract Android.Content.Intent? RegisterReceiver (Android.Content.BroadcastReceiver? receiver, Android.Content.IntentFilter? filter, Android.Content.ActivityFlags flags);
[<Android.Runtime.Register("registerReceiver", "(Landroid/content/BroadcastReceiver;Landroid/content/IntentFilter;I)Landroid/content/Intent;", "GetRegisterReceiver_Landroid_content_BroadcastReceiver_Landroid_content_IntentFilter_IHandler", ApiSince=26)>]
[<System.Obsolete("This method has an incorrect enumeration type. Use the overload that takes ReceiverFlags instead.")>]
abstract member RegisterReceiver : Android.Content.BroadcastReceiver * Android.Content.IntentFilter * Android.Content.ActivityFlags -> Android.Content.Intent

Parameters

receiver
BroadcastReceiver

The BroadcastReceiver to handle the broadcast.

filter
IntentFilter

Selects the Intent broadcasts to be received.

flags
ActivityFlags

Additional options for the receiver. For apps targeting android.os.Build.VERSION_CODES#UPSIDE_DOWN_CAKE either #RECEIVER_EXPORTED or #RECEIVER_NOT_EXPORTED must be specified if the receiver isn't being registered for system broadcasts or a SecurityException will be thrown. If #RECEIVER_EXPORTED is specified, a receiver may additionally specify #RECEIVER_VISIBLE_TO_INSTANT_APPS. For a complete list of system broadcast actions, see the BROADCAST_ACTIONS.TXT file in the Android SDK. If both #RECEIVER_EXPORTED and #RECEIVER_NOT_EXPORTED are specified, an IllegalArgumentException will be thrown.

Returns

The first sticky intent found that matches <var>filter</var>, or null if there are none.

Attributes

Remarks

Register to receive intent broadcasts, with the receiver optionally being exposed to Instant Apps. See #registerReceiver(BroadcastReceiver, IntentFilter) for more information. By default Instant Apps cannot interact with receivers in other applications, this allows you to expose a receiver that Instant Apps can interact with.

See BroadcastReceiver for more information on Intent broadcasts.

As of android.os.Build.VERSION_CODES#ICE_CREAM_SANDWICH, receivers registered with this method will correctly respect the Intent#setPackage(String) specified for an Intent being broadcast. Prior to that, it would be ignored and delivered to all matching registered receivers. Be careful if using this for security.

Java documentation for android.content.Context.registerReceiver(android.content.BroadcastReceiver, android.content.IntentFilter, int).

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.

Applies to

RegisterReceiver(BroadcastReceiver, IntentFilter, ReceiverFlags)

public Android.Content.Intent? RegisterReceiver (Android.Content.BroadcastReceiver? receiver, Android.Content.IntentFilter? filter, Android.Content.ReceiverFlags flags);
member this.RegisterReceiver : Android.Content.BroadcastReceiver * Android.Content.IntentFilter * Android.Content.ReceiverFlags -> Android.Content.Intent

Parameters

filter
IntentFilter

Returns

Applies to

RegisterReceiver(BroadcastReceiver, IntentFilter)

Register a BroadcastReceiver to be run in the main activity thread.

[Android.Runtime.Register("registerReceiver", "(Landroid/content/BroadcastReceiver;Landroid/content/IntentFilter;)Landroid/content/Intent;", "GetRegisterReceiver_Landroid_content_BroadcastReceiver_Landroid_content_IntentFilter_Handler")]
public abstract Android.Content.Intent? RegisterReceiver (Android.Content.BroadcastReceiver? receiver, Android.Content.IntentFilter? filter);
[<Android.Runtime.Register("registerReceiver", "(Landroid/content/BroadcastReceiver;Landroid/content/IntentFilter;)Landroid/content/Intent;", "GetRegisterReceiver_Landroid_content_BroadcastReceiver_Landroid_content_IntentFilter_Handler")>]
abstract member RegisterReceiver : Android.Content.BroadcastReceiver * Android.Content.IntentFilter -> Android.Content.Intent

Parameters

receiver
BroadcastReceiver

The BroadcastReceiver to handle the broadcast.

filter
IntentFilter

Selects the Intent broadcasts to be received.

Returns

The first sticky intent found that matches <var>filter</var>, or null if there are none.

Attributes

Remarks

Register a BroadcastReceiver to be run in the main activity thread. The <var>receiver</var> will be called with any broadcast Intent that matches <var>filter</var>, in the main application thread.

The system may broadcast Intents that are "sticky" -- these stay around after the broadcast has finished, to be sent to any later registrations. If your IntentFilter matches one of these sticky Intents, that Intent will be returned by this function <strong>and</strong> sent to your <var>receiver</var> as if it had just been broadcast.

There may be multiple sticky Intents that match <var>filter</var>, in which case each of these will be sent to <var>receiver</var>. In this case, only one of these can be returned directly by the function; which of these that is returned is arbitrarily decided by the system.

If you know the Intent your are registering for is sticky, you can supply null for your <var>receiver</var>. In this case, no receiver is registered -- the function simply returns the sticky Intent that matches <var>filter</var>. In the case of multiple matches, the same rules as described above apply.

See BroadcastReceiver for more information on Intent broadcasts.

As of android.os.Build.VERSION_CODES#ICE_CREAM_SANDWICH, receivers registered with this method will correctly respect the Intent#setPackage(String) specified for an Intent being broadcast. Prior to that, it would be ignored and delivered to all matching registered receivers. Be careful if using this for security.

For apps targeting android.os.Build.VERSION_CODES#UPSIDE_DOWN_CAKE, either #RECEIVER_EXPORTED or #RECEIVER_NOT_EXPORTED must be specified if the receiver is not being registered for system broadcasts or a SecurityException will be thrown. See #registerReceiver(BroadcastReceiver, IntentFilter, int) to register a receiver with flags.

<p class="note">Note: this method <em>cannot be called from a BroadcastReceiver component;</em> that is, from a BroadcastReceiver that is declared in an application's manifest. It is okay, however, to call this method from another BroadcastReceiver that has itself been registered at run time with #registerReceiver, since the lifetime of such a registered BroadcastReceiver is tied to the object that registered it.

Java documentation for android.content.Context.registerReceiver(android.content.BroadcastReceiver, android.content.IntentFilter).

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.

See also

Applies to