Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
Expand Down Expand Up @@ -197,6 +198,38 @@ void main(List<String> args) async {
talker.info("[SplashScreen] Now hiding splash screen...");
}

Future<bool> 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) {
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Loading