fix(android) fix crash when starting foreground service

If we attempt to start it while in the background we'll get a crash. A
more elegant fix would be to wait until the app transitions to the
foreground to start it, but the crash is hurting us now.
This commit is contained in:
Saúl Ibarra Corretgé 2022-04-08 12:06:56 +02:00 committed by Saúl Ibarra Corretgé
parent ceb8d3348d
commit 23b91c0336
1 changed files with 13 additions and 4 deletions

View File

@ -51,11 +51,20 @@ public class JitsiMeetOngoingConferenceService extends Service
intent.setAction(Action.START.getName());
ComponentName componentName;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
componentName = context.startForegroundService(intent);
} else {
componentName = context.startService(intent);
try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
componentName = context.startForegroundService(intent);
} else {
componentName = context.startService(intent);
}
} catch (RuntimeException e) {
// Avoid crashing due to ForegroundServiceStartNotAllowedException (API level 31).
// See: https://developer.android.com/guide/components/foreground-services#background-start-restrictions
JitsiMeetLogger.w(TAG + " Ongoing conference service not started", e);
return;
}
if (componentName == null) {
JitsiMeetLogger.w(TAG + " Ongoing conference service not started");
}