stopService method

Future<void> stopService()

Stop the background service when call ends. On Android, stops any orphan flutter_background_service from legacy builds.

Implementation

Future<void> stopService() async {
  if (Platform.isIOS) {
    return;
  }

  debugLog(
      '🛑 [CallBackgroundService] ========== STOP SERVICE CALLED ==========');

  if (Platform.isAndroid) {
    await _stopOrphanFlutterBackgroundService();
    _isServiceRunning = false;
    return;
  }

  if (!_isServiceRunning) {
    debugLog('⚠️ [CallBackgroundService] Service not running, nothing to stop');
    return;
  }

  try {
    final service = FlutterBackgroundService();
    service.invoke('stopService');
    await Future<void>.delayed(const Duration(milliseconds: 500));
    if (await service.isRunning()) {
      service.invoke('stopService');
      await Future<void>.delayed(const Duration(milliseconds: 500));
    }
    _isServiceRunning = false;
  } catch (e, stackTrace) {
    debugLog('❌ [CallBackgroundService] Failed to stop service: $e');
    debugLog('❌ [CallBackgroundService] Stack trace: $stackTrace');
    _isServiceRunning = false;
  }
}