diff --git a/lib/main.dart b/lib/main.dart index 28bfd226..9e8c3476 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -38,6 +38,7 @@ import 'package:window_manager/window_manager.dart'; import 'package:protocol_handler/protocol_handler.dart'; import 'package:island/core/services/unifiedpush_service.dart'; import 'package:media_kit/media_kit.dart'; +import 'package:process_run/cmd_run.dart'; @pragma('vm:entry-point') Future _firebaseMessagingBackgroundHandler(RemoteMessage message) async { @@ -197,6 +198,38 @@ void main(List args) async { talker.info("[SplashScreen] Now hiding splash screen..."); } + Future isAlreadyRunning() async { + final lockFile = File(Platform.isWindows + ? r'C:\temp\island.lock' + : '/tmp/island.lock'); + if (await lockFile.exists()) { + // 尝试读取 PID + try { + final pid = int.parse(await lockFile.readAsString().trim()); + // 检查进程是否存在 + if (Platform.isWindows) { + final result = await run(['tasklist', '/FI', 'PID eq $pid']); + return result.stdout.contains('$pid'); + } else { + final result = await Process.run('kill', ['-0', '$pid']); + return result.exitCode == 0; + } + } catch (e) { + await lockFile.delete(); + } + } + // 创建锁文件 + await lockFile.writeAsString('${Process.pid}'); + return false; + } + void main() async { + WidgetsFlutterBinding.ensureInitialized(); + if (await isAlreadyRunning()) { + debugPrint('A example was already running'); + exit(0); + } + + runApp( ProviderScope( retry: (retryCount, error) { diff --git a/pubspec.yaml b/pubspec.yaml index abbb2104..4077c6a5 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -37,6 +37,7 @@ dependencies: # The following adds the Cupertino Icons font to your application. # Use with the CupertinoIcons class for iOS style icons. + process_run: ^0.12.0 cupertino_icons: ^1.0.9 flutter_hooks: ^0.21.3+1 hooks_riverpod: ^3.3.1 @@ -134,7 +135,6 @@ dependencies: waveform_flutter: ^1.2.0 flutter_app_update: ^3.2.2 archive: ^4.0.9 - process_run: ^1.3.1+1 firebase_crashlytics: ^5.1.0 firebase_analytics: ^12.2.0 material_color_utilities: ^0.13.0