// Author: fengshengxiong // Date: 2024/6/25 // Description: 应用程序生命周期反应器 import 'dart:ui'; import 'package:flutter/material.dart'; import 'package:flutter_translate/ads/interstitial_ad_manage.dart'; import 'package:flutter_translate/common/utils/log_utils.dart'; class AppLifecycleReactor { AppLifecycleReactor._(); static final AppLifecycleReactor _instance = AppLifecycleReactor._(); factory AppLifecycleReactor() { return _instance; } AppLifecycleListener? appLifecycleListener; void listenToAppStateChanges() { appLifecycleListener = AppLifecycleListener( onStateChange: onStateChange, onResume: onResume, onInactive: onInactive, onHide: onHide, onShow: onShow, onPause: onPause, onRestart: onRestart, onDetach: onDetach, onExitRequested: onExitRequested, ); } /// 监听状态 onStateChange(AppLifecycleState state) { Log.d('app_state:$state'); } /// 可见,并且可以响应用户操作时的回调 /// 比如应用从后台调度到前台时,在 onShow() 后面 执行 /// 注意:这个回调,初始化时 不执行 onResume() { Log.d('---onResume'); } /// 可见,但无法响应用户操作时的回调 onInactive() { Log.d('---onInactive'); } /// 隐藏时的回调 onHide() { Log.d('---onHide'); } /// 显示时的回调,应用从后台调度到前台时 onShow() { Log.d('---onShow'); InterstitialAdManager().showAdIfReady(InterstitialAdManager().adIds[0]); } /// 暂停时的回调 onPause() { Log.d('---onPause'); } /// 暂停后恢复时的回调 onRestart() { Log.d('---onRestart'); } /// 这两个回调,不是所有平台都支持, /// 当退出 并将所有视图与引擎分离时的回调(IOS 支持,Android 不支持) onDetach() { Log.d('---onDetach'); } /// 在退出程序时,发出询问的回调(IOS、Android 都不支持) /// 响应 [AppExitResponse.exit] 将继续终止,响应 [AppExitResponse.cancel] 将取消终止。 Future onExitRequested() async { Log.d('---onExitRequested'); return AppExitResponse.exit; } }