Skip to content

Fix android.broadcast.BroadcastReceiver on Android 14+: pass RECEIVER_EXPORTED / RECEIVER_NOT_EXPORTED - #3400

Merged
AndreMiras merged 2 commits into
kivy:developfrom
Fector101:fix-broadcast-receiver-export-flag
Oct 27, 2026
Merged

AndreMiras merged 2 commits into
kivy:developfrom
Fector101:fix-broadcast-receiver-export-flag

Conversation

@Fector101

@Fector101 Fector101 commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor

My BroadcastReceiver is failing on android 15 when on BroadcastReceiver.start() registers with the 4-arg registerReceiver.

On Android 14+, the platform requires RECEIVER_EXPORTED or RECEIVER_NOT_EXPORTED for any runtime-registered receiver whose filter is not exclusively protected system broadcasts, so registering for app-defined actions now throws:

java.lang.SecurityException:
 One of RECEIVER_EXPORTED or RECEIVER_NOT_EXPORTED should be specified
when a receiver isn't being registered exclusively for system broadcasts

p4a's own Java-side receivers Hardware.java listen only to protected system broadcasts and are exempt, anything registering custom actions from Python crashes in start().

Fixes #3399

Changes

  • broadcast.py: new exported=False keyword on BroadcastReceiver.__init__.

  • On Android 14+ _register_receiver() passes Context.RECEIVER_EXPORTED or Context.RECEIVER_NOT_EXPORTED (default) as the 5th argument.

  • below 14 the original 4-arg call is kept, since the RECEIVER_* constants only exist from API 33.

  • The default is RECEIVER_NOT_EXPORTED (least privilege): same-UID and system broadcasts are still delivered. exported=True is the opt-in for receiving from other apps.

  • doc/source/apis.rst: documents the new parameter.

Verification

Android 15 / API 35 device, targetSdk 36, receiver registered from a foreground service for two app-defined actions, triggered via notification buttons (PendingIntent.getBroadcast + setPackage()):

  • unpatched: br.start() raises the SecurityException above
  • this branch (built via p4a.source_dir): all taps reach the callback, no exception

docs to Android note on changes: https://developer.android.com/about/versions/14/behavior-changes-14#runtime-receivers-exported

start() used implicit flags = 0, which raises a SecurityException on
Android 14+ for app-defined actions. New exported=False keyword; on
SDK 33+ register with the matching flag, below that keep the legacy call.
@coveralls

coveralls commented Oct 20, 2026 •

Copy link
Copy Markdown

Coverage Status

coverage: 63.297% (+0.02%) from 63.278% — Fector101:fix-broadcast-receiver-export-flag into kivy:develop

@AndreMiras AndreMiras self-assigned this Oct 24, 2026

@AndreMiras AndreMiras left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good thanks for the changes.
I made a few minor suggestions and restarted the failing CI job.
Let me know what you think

Comment thread doc/source/apis.rst Resolved
@Fector101

Copy link
Copy Markdown
Contributor Author

Hi @AndreMiras , I went with your comment i think they contain a more detailed explanation explaining the reason for the change

@AndreMiras AndreMiras left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good thanks for the follow up

@AndreMiras
AndreMiras merged commit 1c9a4b4 into kivy:develop Oct 27, 2026
39 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SecurityException in android.broadcast.BroadcastReceiver.start() on Android 14+ (missing RECEIVER_EXPORTED / RECEIVER_NOT_EXPORTED)

3 participants