我想知道是否可以检测杀死的应用程序。让我们说在聊天应用程序中,我可以得到时间戳时,用户离开聊天室使用onWillPop。但如果用户杀死了应用程序直接从聊天室,它不会被解雇。那么有没有办法检测到这一点?或者任何建议,以获得时间戳不同的方式?
w1jd8yoj1#
另请参见https://flutter.io/flutter-for-android/#how-do-i-listen-to-android-activity-lifecycle-events你可以监听非活动、暂停和断开的状态。这可能有点太早,但通常来说,太早和频繁地做一些清理总比什么都不做好:
WidgetsBinding.instance.addObserver(LifecycleEventHandler( detachedCallBack: () async => widget.appController.persistState(), resumeCallBack: () async { _log.finest('resume...'); }));
WidgetsBinding.instance.addObserver(LifecycleEventHandler(
detachedCallBack: () async => widget.appController.persistState(),
resumeCallBack: () async {
_log.finest('resume...');
}));
class LifecycleEventHandler extends WidgetsBindingObserver { LifecycleEventHandler({this.resumeCallBack, this.detachedCallBack}); final FutureVoidCallback resumeCallBack; final FutureVoidCallback detachedCallBack;// @override// Future<bool> didPopRoute()// @override// void didHaveMemoryPressure() @override Future<void> didChangeAppLifecycleState(AppLifecycleState state) async { switch (state) { case AppLifecycleState.inactive: case AppLifecycleState.paused: case AppLifecycleState.detached: await detachedCallBack(); break; case AppLifecycleState.resumed: await resumeCallBack(); break; } _log.finest('''============================================================= $state============================================================='''); }// @override// void didChangeLocale(Locale locale)// @override// void didChangeTextScaleFactor()// @override// void didChangeMetrics();// @override// Future<bool> didPushRoute(String route)}
class LifecycleEventHandler extends WidgetsBindingObserver {
LifecycleEventHandler({this.resumeCallBack, this.detachedCallBack});
final FutureVoidCallback resumeCallBack;
final FutureVoidCallback detachedCallBack;
// @override
// Future<bool> didPopRoute()
// void didHaveMemoryPressure()
@override
Future<void> didChangeAppLifecycleState(AppLifecycleState state) async {
switch (state) {
case AppLifecycleState.inactive:
case AppLifecycleState.paused:
case AppLifecycleState.detached:
await detachedCallBack();
break;
case AppLifecycleState.resumed:
await resumeCallBack();
}
_log.finest('''
=============================================================
$state
''');
// void didChangeLocale(Locale locale)
// void didChangeTextScaleFactor()
// void didChangeMetrics();
// Future<bool> didPushRoute(String route)
编辑
在2019年11月4日的pull request中,枚举AppLifecycleState.suspending被重命名为AppLifecycleState.detached。如果您使用的Flutter版本早于1.12,您必须仍然使用AppLifecycleState.suspending。
AppLifecycleState.suspending
AppLifecycleState.detached
1条答案
按热度按时间w1jd8yoj1#
另请参见https://flutter.io/flutter-for-android/#how-do-i-listen-to-android-activity-lifecycle-events
你可以监听非活动、暂停和断开的状态。这可能有点太早,但通常来说,太早和频繁地做一些清理总比什么都不做好:
编辑
在2019年11月4日的pull request中,枚举
AppLifecycleState.suspending
被重命名为AppLifecycleState.detached
。如果您使用的Flutter版本早于1.12,您必须仍然使用AppLifecycleState.suspending
。