From 8538dd7dcc13401abf720b56549bec33bff10938 Mon Sep 17 00:00:00 2001 From: kartavya <87070473+Novfensec@users.noreply.github.com> Date: Mon, 30 Jun 2025 16:53:23 +0530 Subject: [PATCH 1/3] Update broadcastReceiver --- .../recipes/android/src/android/broadcast.py | 37 +++++++++++++++---- 1 file changed, 29 insertions(+), 8 deletions(-) diff --git a/pythonforandroid/recipes/android/src/android/broadcast.py b/pythonforandroid/recipes/android/src/android/broadcast.py index 750e9c87ef..0f8b628937 100644 --- a/pythonforandroid/recipes/android/src/android/broadcast.py +++ b/pythonforandroid/recipes/android/src/android/broadcast.py @@ -22,6 +22,7 @@ def onReceive(self, context, intent): def __init__(self, callback, actions=None, categories=None): super().__init__() self.callback = callback + self._is_registered = False if not actions and not categories: raise Exception('You need to define at least actions or categories') @@ -29,11 +30,10 @@ def __init__(self, callback, actions=None, categories=None): def _expand_partial_name(partial_name): if '.' in partial_name: return partial_name # Its actually a full dotted name - else: - name = 'ACTION_{}'.format(partial_name.upper()) - if not hasattr(Intent, name): - raise Exception('The intent {} does not exist'.format(name)) - return getattr(Intent, name) + name = 'ACTION_{}'.format(partial_name.upper()) + if not hasattr(Intent, name): + raise Exception('The intent {} does not exist'.format(name)) + return getattr(Intent, name) # resolve actions/categories first Intent = autoclass('android.content.Intent') @@ -58,15 +58,36 @@ def _expand_partial_name(partial_name): self.receiver_filter.addCategory(x) def start(self): - Handler = autoclass('android.os.Handler') + + if hasattr(self, 'handlerthread') and self.handlerthread.isAlive(): + print("HandlerThread already running, skipping start") + return + + HandlerThread = autoclass('android.os.HandlerThread') + self.handlerthread = HandlerThread('handlerthread') self.handlerthread.start() + + if self._is_registered: + print("[BroadcastReceiver] Already registered.") + return + + Handler = autoclass('android.os.Handler') self.handler = Handler(self.handlerthread.getLooper()) self.context.registerReceiver( self.receiver, self.receiver_filter, None, self.handler) + self._is_registered = True def stop(self): - self.context.unregisterReceiver(self.receiver) - self.handlerthread.quit() + try: + self.context.unregisterReceiver(self.receiver) + self._is_registered = False + except Exception as e: + print("[BroadcastReceiver] unregisterReceiver failed:", e) + + if hasattr(self, 'handlerthread'): + self.handlerthread.quitSafely() + self.handlerthread = None + self.handler = None @property def context(self): From fe1ff50479f82f635a902527cec27c956538763b Mon Sep 17 00:00:00 2001 From: kartavya <87070473+Novfensec@users.noreply.github.com> Date: Mon, 30 Jun 2025 17:11:48 +0530 Subject: [PATCH 2/3] Undo unnecessary changes. --- .../recipes/android/src/android/broadcast.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/pythonforandroid/recipes/android/src/android/broadcast.py b/pythonforandroid/recipes/android/src/android/broadcast.py index 0f8b628937..acf368d0bf 100644 --- a/pythonforandroid/recipes/android/src/android/broadcast.py +++ b/pythonforandroid/recipes/android/src/android/broadcast.py @@ -30,10 +30,11 @@ def __init__(self, callback, actions=None, categories=None): def _expand_partial_name(partial_name): if '.' in partial_name: return partial_name # Its actually a full dotted name - name = 'ACTION_{}'.format(partial_name.upper()) - if not hasattr(Intent, name): - raise Exception('The intent {} does not exist'.format(name)) - return getattr(Intent, name) + else: + name = 'ACTION_{}'.format(partial_name.upper()) + if not hasattr(Intent, name): + raise Exception('The intent {} does not exist'.format(name)) + return getattr(Intent, name) # resolve actions/categories first Intent = autoclass('android.content.Intent') From 42336ff0c4622a1773dd73e3c0ab7babe8016fcb Mon Sep 17 00:00:00 2001 From: Kartavya Shukla <87070473+Novfensec@users.noreply.github.com> Date: Thu, 11 Sep 2025 15:45:50 +0530 Subject: [PATCH 3/3] Update broadcast.py --- .../recipes/android/src/android/broadcast.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/pythonforandroid/recipes/android/src/android/broadcast.py b/pythonforandroid/recipes/android/src/android/broadcast.py index acf368d0bf..cc8e06aee4 100644 --- a/pythonforandroid/recipes/android/src/android/broadcast.py +++ b/pythonforandroid/recipes/android/src/android/broadcast.py @@ -1,9 +1,12 @@ # ------------------------------------------------------------------- # Broadcast receiver bridge - +import logging from jnius import autoclass, PythonJavaClass, java_method from android.config import JAVA_NAMESPACE, JNI_NAMESPACE, ACTIVITY_CLASS_NAME, SERVICE_CLASS_NAME +logger = logging.getLogger("BroadcastReceiver") +logger.setLevel(logging.DEBUG) + class BroadcastReceiver(object): @@ -61,7 +64,7 @@ def _expand_partial_name(partial_name): def start(self): if hasattr(self, 'handlerthread') and self.handlerthread.isAlive(): - print("HandlerThread already running, skipping start") + logger.debug("HandlerThread already running, skipping start") return HandlerThread = autoclass('android.os.HandlerThread') @@ -69,7 +72,7 @@ def start(self): self.handlerthread.start() if self._is_registered: - print("[BroadcastReceiver] Already registered.") + logger.info("Already registered.") return Handler = autoclass('android.os.Handler') @@ -83,7 +86,7 @@ def stop(self): self.context.unregisterReceiver(self.receiver) self._is_registered = False except Exception as e: - print("[BroadcastReceiver] unregisterReceiver failed:", e) + logger.error("unregisterReceiver failed: %s", e) if hasattr(self, 'handlerthread'): self.handlerthread.quitSafely()