Skip to content

Commit 902fb6d

Browse files
MGasztoldhannesa2
authored andcommitted
catch IllegalStateException exception when service cannot be started because app is in background
1 parent 1426cb2 commit 902fb6d

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

org.eclipse.paho.android.service/src/main/java/org/eclipse/paho/android/service/MqttAndroidClient.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ public IMqttToken connect(MqttConnectOptions options, Object userContext, IMqttA
327327
Intent serviceStartIntent = new Intent();
328328
serviceStartIntent.setClassName(myContext, SERVICE_NAME);
329329

330-
Object service;
330+
Object service = null;
331331
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O
332332
&& foregroundServiceNotification != null) {
333333
serviceStartIntent.putExtra(
@@ -338,7 +338,14 @@ public IMqttToken connect(MqttConnectOptions options, Object userContext, IMqttA
338338
foregroundServiceNotificationId);
339339
service = myContext.startForegroundService(serviceStartIntent);
340340
} else {
341-
service = myContext.startService(serviceStartIntent);
341+
try {
342+
service = myContext.startService(serviceStartIntent);
343+
} catch(IllegalStateException ex) {
344+
IMqttActionListener listener = token.getActionCallback();
345+
if (listener != null) {
346+
listener.onFailure(token, ex);
347+
}
348+
}
342349
}
343350

344351
if (service == null) {

0 commit comments

Comments
 (0)