From dd0975a7ec99bca4ee5fa80047366737050b78aa Mon Sep 17 00:00:00 2001 From: orz12 Date: Sun, 7 Jul 2024 15:18:10 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=A4=84=E4=BA=8E=E5=90=8E=E5=8F=B0?= =?UTF-8?q?=E6=97=B6=EF=BC=8C=E5=AE=9A=E6=97=B6=E5=85=B3=E9=97=AD=E8=B7=B3?= =?UTF-8?q?=E8=BF=87=E5=BC=B9=E7=AA=97=E8=AF=A2=E9=97=AE=EF=BC=8C=E7=9B=B4?= =?UTF-8?q?=E6=8E=A5=E6=89=A7=E8=A1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/services/shutdown_timer_service.dart | 34 ++++++++++++++++-------- 1 file changed, 23 insertions(+), 11 deletions(-) diff --git a/lib/services/shutdown_timer_service.dart b/lib/services/shutdown_timer_service.dart index 825ad232..84367280 100644 --- a/lib/services/shutdown_timer_service.dart +++ b/lib/services/shutdown_timer_service.dart @@ -7,7 +7,7 @@ import 'package:flutter_smart_dialog/flutter_smart_dialog.dart'; import '../plugin/pl_player/controller.dart'; -class ShutdownTimerService { +class ShutdownTimerService with WidgetsBindingObserver { static final ShutdownTimerService _instance = ShutdownTimerService._internal(); Timer? _shutdownTimer; @@ -17,10 +17,24 @@ class ShutdownTimerService { bool exitApp = false; bool waitForPlayingCompleted = false; bool isWaiting = false; - + bool isInBackground = false; factory ShutdownTimerService() => _instance; - ShutdownTimerService._internal(); + ShutdownTimerService._internal() { + WidgetsBinding.instance.addObserver(this); // 添加观察者 + } + + void dispose() { + WidgetsBinding.instance.removeObserver(this); // 移除观察者 + _shutdownTimer?.cancel(); + _autoCloseDialogTimer?.cancel(); + _instance.dispose(); + } + + @override + void didChangeAppLifecycleState(AppLifecycleState state) { + isInBackground = state == AppLifecycleState.paused; + } void startShutdownTimer() { cancelShutdownTimer(); // Cancel any previous timer @@ -55,6 +69,11 @@ class ShutdownTimerService { } void _showShutdownDialog() { + if (isInBackground) { + print("app在后台运行,不弹窗"); + _executeShutdown(); + return; + } SmartDialog.show( builder: (BuildContext dialogContext) { // Start the 10-second timer to auto close the dialog @@ -124,6 +143,7 @@ class ShutdownTimerService { void _executeShutdown() { if (exitApp) { + PlPlayerController.pauseIfExists(); //退出app exit(0); } else { @@ -136,14 +156,6 @@ class ShutdownTimerService { } else { SmartDialog.showToast("当前未播放"); } - // PlPlayerController plPlayerController = PlPlayerController.getInstance(); - // if (plPlayerController.playerStatus.playing) { - // plPlayerController.pause(); - // waitForPlayingCompleted = true; - // SmartDialog.showToast("已暂停播放"); - // } else { - // SmartDialog.showToast("当前未播放"); - // } } }