我想知道能不能查杀掉这个应用程序.
比方说,在聊天应用程序中,当用户使用onWillPop离开聊天室时,我可以获得时间戳.
但如果用户直接从聊天室关闭了这款应用,它就不会被激活.那么有没有办法检测到这一点呢?
或者有什么建议以不同的方式获取时间戳吗?
我想知道能不能查杀掉这个应用程序.
比方说,在聊天应用程序中,当用户使用onWillPop离开聊天室时,我可以获得时间戳.
但如果用户直接从聊天室关闭了这款应用,它就不会被激活.那么有没有办法检测到这一点呢?
或者有什么建议以不同的方式获取时间戳吗?
另见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个
2019年11月4日,随着这pull request的出现,枚举AppLifecycleState.suspending
更名为AppLifecycleState.detached
.如果您在1.12之前的版本中使用Flutter ,则仍必须使用AppLifecycleState.suspending
.