diff --git a/lib/main.dart b/lib/main.dart index 3893a206..10a2339e 100755 --- a/lib/main.dart +++ b/lib/main.dart @@ -12,6 +12,7 @@ import 'package:star_lock/mine/about/debug/debug_tool.dart'; import 'package:star_lock/network/api_provider.dart'; import 'package:star_lock/network/api_repository.dart'; import 'package:star_lock/network/start_chart_api.dart'; +import 'package:star_lock/talk/startChart/appLifecycle_observer.dart'; import 'package:star_lock/tools/bugly/bugly_tool.dart'; import 'package:star_lock/tools/device_info_service.dart'; import 'package:star_lock/tools/platform_info_services.dart'; @@ -29,6 +30,10 @@ FutureOr main() async { FlutterBugly.postCatchedException(() async { WidgetsFlutterBinding.ensureInitialized(); + // 创建并注册 AppLifecycleObserver(用于程序是否进入后台的监听) + AppLifecycleObserver appLifecycleObserver = AppLifecycleObserver(); + WidgetsBinding.instance.addObserver(appLifecycleObserver); + await _setCommonServices(); // 设置国际化信息 diff --git a/lib/talk/startChart/appLifecycle_observer.dart b/lib/talk/startChart/appLifecycle_observer.dart new file mode 100644 index 00000000..9f4dab42 --- /dev/null +++ b/lib/talk/startChart/appLifecycle_observer.dart @@ -0,0 +1,42 @@ +import 'package:flutter/widgets.dart'; +import 'package:star_lock/talk/startChart/start_chart_manage.dart'; + +class AppLifecycleObserver extends WidgetsBindingObserver { + @override + void didChangeAppLifecycleState(AppLifecycleState state) { + super.didChangeAppLifecycleState(state); + + // 打印应用程序生命周期状态 + print('AppLifecycleState: $state'); + + // 根据应用程序状态执行相应的操作 + if (state == AppLifecycleState.paused) { + // 应用程序进入后台 + onAppPaused(); + } else if (state == AppLifecycleState.resumed) { + // 应用程序恢复到前台 + onAppResumed(); + } else if (state == AppLifecycleState.detached) { + // 应用程序被杀死 + onAppDetached(); + } + } + + void onAppPaused() { + // 处理应用程序进入后台的逻辑 + print('App has entered the background.'); + StartChartManage().destruction(); + } + + void onAppResumed() { + // 处理应用程序恢复到前台的逻辑 + StartChartManage().init(); + print('App has resumed to the foreground.'); + } + + void onAppDetached() { + // 处理应用程序被杀死的逻辑 + StartChartManage().destruction(); + print('App has been detached.'); + } +}