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/2] 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 750e9c87e..0f8b62893 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/2] 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 0f8b62893..acf368d0b 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')