From c80143211b9f5ad7c2ece067b33c5af9e24e2ddf Mon Sep 17 00:00:00 2001 From: Christian Rowlands Date: Mon, 19 Feb 2024 09:19:39 -0500 Subject: [PATCH] Handle the edge case where the app has been minimized while the service is starting --- .../mqttlibrary/ui/AConnectionFragment.java | 24 ++++++++++++------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/MqttLibrary/src/main/java/com/craxiom/mqttlibrary/ui/AConnectionFragment.java b/MqttLibrary/src/main/java/com/craxiom/mqttlibrary/ui/AConnectionFragment.java index 1d13245..55a83e6 100755 --- a/MqttLibrary/src/main/java/com/craxiom/mqttlibrary/ui/AConnectionFragment.java +++ b/MqttLibrary/src/main/java/com/craxiom/mqttlibrary/ui/AConnectionFragment.java @@ -749,15 +749,21 @@ private void hideSoftInputFromWindow() private void startAndBindToService() { - // Start the service - Timber.i("Binding to the service"); - final Intent serviceIntent = new Intent(applicationContext, getServiceClass()); - applicationContext.startService(serviceIntent); - - // Bind to the service - ServiceConnection surveyServiceConnection = new SurveyServiceConnection(); - final boolean bound = applicationContext.bindService(serviceIntent, surveyServiceConnection, Context.BIND_ABOVE_CLIENT); - Timber.i("%s service bound in the AConnectionFragment: %s", getServiceClass().getSimpleName(), bound); + try + { + // Start the service + Timber.i("Binding to the service"); + final Intent serviceIntent = new Intent(applicationContext, getServiceClass()); + applicationContext.startService(serviceIntent); + + // Bind to the service + ServiceConnection surveyServiceConnection = new SurveyServiceConnection(); + final boolean bound = applicationContext.bindService(serviceIntent, surveyServiceConnection, Context.BIND_ABOVE_CLIENT); + Timber.i("%s service bound in the AConnectionFragment: %s", getServiceClass().getSimpleName(), bound); + } catch (Exception e) + { + Timber.e(e, "Could not start the Service"); + } } protected abstract Class getServiceClass();