From a433824db933178c4fd25e27cccf969e8d690602 Mon Sep 17 00:00:00 2001 From: Gerrit Vermeulen Date: Wed, 2 Jun 2021 16:18:21 +0200 Subject: [PATCH 1/2] Receive messages on a non-UI thread --- .../paho/android/service/MqttAndroidClient.java | 14 +++++++++++--- .../eclipse/paho/android/service/MqttService.java | 2 +- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/org.eclipse.paho.android.service/src/main/java/org/eclipse/paho/android/service/MqttAndroidClient.java b/org.eclipse.paho.android.service/src/main/java/org/eclipse/paho/android/service/MqttAndroidClient.java index 06e5b7e98..fbd6441a8 100755 --- a/org.eclipse.paho.android.service/src/main/java/org/eclipse/paho/android/service/MqttAndroidClient.java +++ b/org.eclipse.paho.android.service/src/main/java/org/eclipse/paho/android/service/MqttAndroidClient.java @@ -54,6 +54,7 @@ import android.content.IntentFilter; import android.content.ServiceConnection; import android.os.Bundle; +import android.os.Handler; import android.os.IBinder; import android.support.v4.content.LocalBroadcastManager; import android.util.SparseArray; @@ -444,11 +445,18 @@ public void run() { return token; } + private Handler handler; private void registerReceiver(BroadcastReceiver receiver) { + if (handler == null) { + HandlerThread handlerThread = new HandlerThread("MyNewThread"); + handlerThread.start(); + Looper looper = handlerThread.getLooper(); + handler = new Handler(looper); + } IntentFilter filter = new IntentFilter(); - filter.addAction(MqttServiceConstants.CALLBACK_TO_ACTIVITY); - LocalBroadcastManager.getInstance(myContext).registerReceiver(receiver, filter); - receiverRegistered = true; + filter.addAction(MqttServiceConstants.CALLBACK_TO_ACTIVITY); + myContext.registerReceiver(receiver, filter, null, handler); + receiverRegistered = true; } /** diff --git a/org.eclipse.paho.android.service/src/main/java/org/eclipse/paho/android/service/MqttService.java b/org.eclipse.paho.android.service/src/main/java/org/eclipse/paho/android/service/MqttService.java index 626c5b896..c6c42034f 100644 --- a/org.eclipse.paho.android.service/src/main/java/org/eclipse/paho/android/service/MqttService.java +++ b/org.eclipse.paho.android.service/src/main/java/org/eclipse/paho/android/service/MqttService.java @@ -283,7 +283,7 @@ void callbackToActivity(String clientHandle, Status status, if (dataBundle != null) { callbackIntent.putExtras(dataBundle); } - LocalBroadcastManager.getInstance(this).sendBroadcast(callbackIntent); + sendBroadcast(callbackIntent); } // The major API implementation follows :- From e3199f0c2e8cb5da8b40b41ab0811fa337162596 Mon Sep 17 00:00:00 2001 From: Gerrit Vermeulen Date: Wed, 2 Jun 2021 16:40:57 +0200 Subject: [PATCH 2/2] Forgot some imports --- .../org/eclipse/paho/android/service/MqttAndroidClient.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/org.eclipse.paho.android.service/src/main/java/org/eclipse/paho/android/service/MqttAndroidClient.java b/org.eclipse.paho.android.service/src/main/java/org/eclipse/paho/android/service/MqttAndroidClient.java index fbd6441a8..3d27e8bff 100755 --- a/org.eclipse.paho.android.service/src/main/java/org/eclipse/paho/android/service/MqttAndroidClient.java +++ b/org.eclipse.paho.android.service/src/main/java/org/eclipse/paho/android/service/MqttAndroidClient.java @@ -55,8 +55,10 @@ import android.content.ServiceConnection; import android.os.Bundle; import android.os.Handler; +import android.os.HandlerThread; import android.os.IBinder; import android.support.v4.content.LocalBroadcastManager; +import android.os.Looper; import android.util.SparseArray; /**