如何解决颤振如何记录用户在应用程序中花费的时间?
我已经开发了应用,并且客户希望我将用户花费的总时间存储在应用内
我该如何实现
我曾尝试在颤动中使用this App_usage package,但显示出星际活动错误
如果你们有什么解决办法
请告诉我
在此先感谢:)
解决方法
具有一些变量,可以跟踪应用程序的开始时间和结束/暂停时间,并保持差异。您必须将其与应用程序生命周期挂钩,以侦听诸如暂停/恢复应用程序之类的事件。 (例如How to handle onPause/onResume in Flutter App?)
类似这样的东西:
class AppLifecycleReactor extends StatefulWidget {
const AppLifecycleReactor({Key key}) : super(key: key);
@override
_AppLifecycleReactorState createState() => _AppLifecycleReactorState();
}
class _AppLifecycleReactorState extends State<AppLifecycleReactor>
with WidgetsBindingObserver {
DateTime startTime;
@override
void initState() {
super.initState();
WidgetsBinding.instance.addObserver(this);
}
@override
void dispose() {
WidgetsBinding.instance.removeObserver(this);
super.dispose();
}
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
if (state == AppLifecycleState.resumed) {
startTime = DateTime.now();
}
if (state == AppLifecycleState.detached ||
state == AppLifecycleState.paused) {
var usageTime = DateTime.now().difference(startTime);
// do whatever with the usageTime
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),body: MyContent(),);
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。