From b976c5176a52da48d36353266076114131016fb4 Mon Sep 17 00:00:00 2001 From: Fabian Date: Tue, 20 Oct 2026 14:19:22 +0100 Subject: [PATCH 1/2] android: pass RECEIVER_EXPORTED/RECEIVER_NOT_EXPORTED on API 34+ 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. --- doc/source/apis.rst | 12 ++++++++++- .../recipes/android/src/android/broadcast.py | 20 ++++++++++++++++--- 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/doc/source/apis.rst b/doc/source/apis.rst index 21af814db9..4ad1e97adc 100644 --- a/doc/source/apis.rst +++ b/doc/source/apis.rst @@ -342,18 +342,28 @@ or categories filters. The callback will be called in another thread than the main thread. In that thread, be careful not to access OpenGL or something like that. - .. method:: __init__(callback, actions=None, categories=None) + .. method:: __init__(callback, actions=None, categories=None, exported=False) :param callback: function or method that will receive the event. Will receive the context and intent as argument. :param actions: list of strings that represent an action. :param categories: list of strings that represent a category. + :param exported: if `True`, other applications are allowed to send + broadcasts to this receiver. Defaults to `False`. For actions and categories, the string must be in lower case, without the prefix:: # In java: Intent.ACTION_HEADSET_PLUG # In python: 'headset_plug' + .. note:: + + Since Android 14 (API 34), receivers must be registered with + ``RECEIVER_EXPORTED`` or ``RECEIVER_NOT_EXPORTED``. The default + (``exported=False``) uses ``RECEIVER_NOT_EXPORTED``, so only your + own application and the system can send broadcasts to this + receiver. + .. method:: start() Register the receiver with all the actions and categories, and start diff --git a/pythonforandroid/recipes/android/src/android/broadcast.py b/pythonforandroid/recipes/android/src/android/broadcast.py index cc8e06aee4..99c9723e09 100644 --- a/pythonforandroid/recipes/android/src/android/broadcast.py +++ b/pythonforandroid/recipes/android/src/android/broadcast.py @@ -22,10 +22,11 @@ def __init__(self, callback, *args, **kwargs): def onReceive(self, context, intent): self.callback(context, intent) - def __init__(self, callback, actions=None, categories=None): + def __init__(self, callback, actions=None, categories=None, exported=False): super().__init__() self.callback = callback self._is_registered = False + self.exported = exported if not actions and not categories: raise Exception('You need to define at least actions or categories') @@ -77,10 +78,23 @@ def start(self): Handler = autoclass('android.os.Handler') self.handler = Handler(self.handlerthread.getLooper()) - self.context.registerReceiver( - self.receiver, self.receiver_filter, None, self.handler) + self._register_receiver() self._is_registered = True + def _register_receiver(self): + if autoclass('android.os.Build$VERSION').SDK_INT >= 33: + # API 34+ requires RECEIVER_EXPORTED / RECEIVER_NOT_EXPORTED + Context = autoclass('android.content.Context') + if self.exported: + flags = Context.RECEIVER_EXPORTED + else: + flags = Context.RECEIVER_NOT_EXPORTED + self.context.registerReceiver( + self.receiver, self.receiver_filter, None, self.handler, flags) + else: + self.context.registerReceiver( + self.receiver, self.receiver_filter, None, self.handler) + def stop(self): try: self.context.unregisterReceiver(self.receiver) From ebd38e7477f7392aa90d0e0c3bd62ef956577526 Mon Sep 17 00:00:00 2001 From: Fabian Date: Tue, 27 Oct 2026 21:29:01 +0100 Subject: [PATCH 2/2] better explaination --- doc/source/apis.rst | 17 ++++++++++++----- .../recipes/android/src/android/broadcast.py | 4 +++- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/doc/source/apis.rst b/doc/source/apis.rst index 4ad1e97adc..cb6fd86a19 100644 --- a/doc/source/apis.rst +++ b/doc/source/apis.rst @@ -358,11 +358,18 @@ or categories filters. .. note:: - Since Android 14 (API 34), receivers must be registered with - ``RECEIVER_EXPORTED`` or ``RECEIVER_NOT_EXPORTED``. The default - (``exported=False``) uses ``RECEIVER_NOT_EXPORTED``, so only your - own application and the system can send broadcasts to this - receiver. + On Android 13 (API 33) and above, the receiver is registered with + ``RECEIVER_NOT_EXPORTED`` by default, or ``RECEIVER_EXPORTED`` when + ``exported=True``. Apps targeting Android 14 (API 34) or higher + must use one of these flags, otherwise ``start()`` raises a + ``SecurityException`` for non-system broadcasts. + + With the default, the receiver only gets broadcasts sent by your + own application or by the Android system process. Broadcasts from + any other application are silently dropped, including privileged + system apps that run under their own user ID, such as Bluetooth + and telephony. If your callback never fires for one of those, + pass ``exported=True`` .. method:: start() diff --git a/pythonforandroid/recipes/android/src/android/broadcast.py b/pythonforandroid/recipes/android/src/android/broadcast.py index 99c9723e09..e602735c4c 100644 --- a/pythonforandroid/recipes/android/src/android/broadcast.py +++ b/pythonforandroid/recipes/android/src/android/broadcast.py @@ -83,7 +83,9 @@ def start(self): def _register_receiver(self): if autoclass('android.os.Build$VERSION').SDK_INT >= 33: - # API 34+ requires RECEIVER_EXPORTED / RECEIVER_NOT_EXPORTED + # RECEIVER_EXPORTED / RECEIVER_NOT_EXPORTED exist since API 33. + # Apps targeting API 34+ must pass one of them unless the receiver + # only listens to protected system broadcasts. Context = autoclass('android.content.Context') if self.exported: flags = Context.RECEIVER_EXPORTED