How to Protect a Dynamic BroadcastReceiver from Messages Sent by Other Apps
A dynamic BroadcastReceiver requires an explicit decision: who may send it messages? If an event is intended only for your own app, an open cross-app channel creates an unnecessary opportunity to invoke the handler. We will explain how to choose a registration flag and test the sender boundary using a fictional draft-update event. The test will account for both message delivery and actual changes to the test state.
First, define which senders are allowed
Define a requirement for each dynamic receiver: does it handle internal events or accept messages from other apps? An exported receiver can receive unprotected broadcasts from outside the app. If the product does not require these calls, inadequate access control allows unwanted handler invocations.
In our entirely fictional DraftDemo app, the com.example.draftdemo.DRAFT_UPDATED event signals an update to a synthetic draft. The requirement is simple: sending it from DraftDemo must produce the expected draft change, while sending it from a second test app you own must leave the draft unchanged. The trust boundary being tested lies between these senders.
Choose a registration flag for the channel’s purpose
For a dynamic receiver that needs only broadcasts from its own app, the documentation recommends RECEIVER_NOT_EXPORTED. This is a registration flag, not a manifest attribute. In the proposed test example, register the draft-event receiver through ContextCompat.registerReceiver with RECEIVER_NOT_EXPORTED.
If messages need to come from other apps, including apps from the same developer, RECEIVER_EXPORTED is recommended. Do not combine internal draft updates and intended cross-app communication in one receiver: messages with different export requirements should be assigned to separate receivers.
RECEIVER_NOT_EXPORTED also allows some system broadcasts, but not messages from the highly privileged apps described in the documentation. It therefore cannot be described as a universal mode for receiving all system events or as a ban on every message originating outside the app.
Do not confuse the recipient with an authorized sender
The setPackage method restricts message recipients to the specified package. It does not verify the sender’s identity. In the external test, the second app will deliberately specify the DraftDemo package: correct addressing must not give it permission to change the internal draft.
Broadcast action names occupy a global namespace. Use your own namespace to avoid conflicts; com.example.draftdemo.DRAFT_UPDATED is only a fictional test identifier here. The event name itself does not replace flag selection and access control.
If cross-app reception is actually needed, specifying a permission during registration restricts senders: they must request it in their manifest, and a dangerous permission must also be granted. The security guide recommends protected broadcasts or custom-declared signature-level permissions for dynamic receivers. Protected broadcasts are intended to be sent by system-level apps.
Prepare observable state and a positive control
The following plan is an original editorial test, not an official Android procedure. Perform it only in two test apps you own, using a synthetic draft without user data. For example, set the initial text to “Version A” and the expected result of processing a valid event to “Version B.”
Record handler invocations and draft contents separately. First, send a valid event from the app that owns the draft. The positive control passes when you observe both a handler invocation and a transition to the expected state. Sending a message alone does not confirm that the intended change was accepted.
Compare internal delivery with an external attempt
After the positive control succeeds, restore the initial draft and clear the test observations. From the second app you own, running under a separate UID, send the same event with valid test extras, specifying the owner’s package through setPackage. With RECEIVER_NOT_EXPORTED, the expected result is no external handler invocation and no change to the initial state. Then repeat the internal positive control.
Also check the delivery path itself: in a separate test setup configuration, temporarily register the receiver with RECEIVER_EXPORTED and no permission restriction. Confirm an invocation from the second app and the expected change to the synthetic draft. Then restore RECEIVER_NOT_EXPORTED and repeat the external and internal scenarios. The exported configuration is used here only to validate the test setup.
If the product uses a separate cross-app event, add a check that the channels remain separate. Its intended delivery should work, but the second app must not gain the ability to change the draft through the internal event. The internal positive control must also pass.
Assess the result with delivery limitations in mind
Broadcast delivery timing is not guaranteed. A brief absence of a handler invocation or draft change does not, by itself, prove that delivery was blocked; there is no universal timeout for reaching that conclusion here. If the positive controls fail or delivery remains uncertain, consider the boundary test inconclusive.
Tie acceptance criteria to observations: the internal event invokes the handler and produces the expected change; the external delivery path is confirmed in the exported control configuration; and, after restoring the non-exported registration, the external event neither invokes the handler nor changes the draft. This tests a specific configuration, without statistical guarantees or generalization to all system broadcasts.
Fix the access boundary first
The first priority is to restrict the internal receiver with RECEIVER_NOT_EXPORTED. Then separate channels with different export requirements and configure access control wherever cross-app communication is necessary. If a broadcast is needed only for an internal notification that an event has completed, the guide suggests considering a callback.
After making changes, repeat the proposed checks using your own fictional data. This article does not cover receiver lifecycle management or asynchronous completion of processing. You can request a free CheckMyApp audit for your website or the web portion of your product; the Android test described here remains a separate development task.