fix:增加app切换到后台或进入前台时的监听
This commit is contained in:
parent
3a2721c96e
commit
eadd667511
@ -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<void> main() async {
|
||||
FlutterBugly.postCatchedException(() async {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
|
||||
// 创建并注册 AppLifecycleObserver(用于程序是否进入后台的监听)
|
||||
AppLifecycleObserver appLifecycleObserver = AppLifecycleObserver();
|
||||
WidgetsBinding.instance.addObserver(appLifecycleObserver);
|
||||
|
||||
await _setCommonServices();
|
||||
|
||||
// 设置国际化信息
|
||||
|
||||
42
lib/talk/startChart/appLifecycle_observer.dart
Normal file
42
lib/talk/startChart/appLifecycle_observer.dart
Normal file
@ -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.');
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user