fix:增加app切换到后台或进入前台时的监听

This commit is contained in:
liyi 2024-12-23 17:12:42 +08:00
parent 3a2721c96e
commit eadd667511
2 changed files with 47 additions and 0 deletions

View File

@ -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();
//

View 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.');
}
}