我想知道是否可以检测杀死该应用程序。
假设在聊天应用程序中,我能够使用 onWillPop 获取用户离开聊天室时的时间戳。
但是,如果用户直接从聊天室终止该应用程序,则该应用程序不会被启动。那么有没有办法检测到呢?
或者有什么建议以不同的方式获取时间戳?
也可以看看https://flutter.io/flutter-for-android/#how-do-i-listen-to-android-activity-lifecycle-events 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...');
}));
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)
}
Edit
有了这个拉取请求 https://github.com/flutter/engine/pull/119132019 年 11 月 4 日,枚举AppLifecycleState.suspending
被重命名为AppLifecycleState.detached
。如果您使用的是 1.12 之前版本的 Flutter,则仍然必须使用AppLifecycleState.suspending
.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)