startForegroundService method

Future<void> startForegroundService()

Implementation

Future<void> startForegroundService() async {
  if (!CallState.instance.isStartForegroundService) {
    CallKitUILog.i(_tag, 'CallManager startForegroundService');

    bool hasRequiredPermissions = false;
    NECallType callType = CallState.instance.callType;

    if (callType == NECallType.audio) {
      // 音频呼叫:需要麦克风权限
      hasRequiredPermissions = await Permission.has(
        permissions: [PermissionType.microphone],
      );
      CallKitUILog.i(_tag,
          'CallManager startForegroundService: audio call, hasMicrophonePermission = $hasRequiredPermissions');
    } else if (callType == NECallType.video) {
      // 视频呼叫:需要麦克风和摄像头权限
      hasRequiredPermissions = await Permission.has(
        permissions: [PermissionType.microphone, PermissionType.camera],
      );
      CallKitUILog.i(_tag,
          'CallManager startForegroundService: video call, hasMicrophoneAndCameraPermissions = $hasRequiredPermissions');
    } else {
      // 未知类型,默认允许
      hasRequiredPermissions = true;
    }

    if (hasRequiredPermissions) {
      NECallKitPlatform.instance
          .startForegroundService(CallState.instance.callType);
      CallState.instance.isStartForegroundService = true;
    } else {
      CallKitUILog.i(_tag,
          'CallManager startForegroundService: permission denied, cannot start foreground service');
    }
  }
}