From b13c4b10cc47ba30142a078b714227ecaa264dbe Mon Sep 17 00:00:00 2001 From: wylyl22 <2373073266@qq.com> Date: Tue, 23 Aug 2022 08:56:45 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=9E=E6=97=B6=E7=9B=91=E5=90=AC=E6=9D=83?= =?UTF-8?q?=E9=99=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/ui/home/home_page.dart | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/lib/ui/home/home_page.dart b/lib/ui/home/home_page.dart index 788f22f..9a2ff13 100644 --- a/lib/ui/home/home_page.dart +++ b/lib/ui/home/home_page.dart @@ -26,32 +26,40 @@ class _HomePageState extends State with WidgetsBindingObserver{ @override void initState() { super.initState(); + //页面初始化的时候,添加一个状态的监听者 + WidgetsBinding.instance.addObserver(this); _listenForPermissionStatus(); - setState(() { - - }); - + setState(() {}); + } + @override + void dispose(){ + super.dispose(); + //页面销毁时移出监听者 + WidgetsBinding.instance.removeObserver(this); } - Future _listenForPermissionStatus() async { sms=await Permission.sms.request().isGranted; plone =await Permission.phone.request().isGranted; setState(() {}); } + @override void didChangeAppLifecycleState(AppLifecycleState state){ super.didChangeAppLifecycleState(state); switch (state){ + //应用状态处于闲置状态,并且没有用户的输入事件, + // 注意:这个状态切换到 前后台 会触发,所以流程应该是先冻结窗口,然后停止UI case AppLifecycleState.inactive: - print("1"); + print("应用处于闲置状态,这种状态的应用应该假设他们可能在任何时候暂停 切换到后台会触发======"); break; case AppLifecycleState.resumed: - print("2"); + print("应用进入前台——————————"); + _listenForPermissionStatus(); break; case AppLifecycleState.paused: - print("3"); + print("应用处于不可见状态 后台======"); break; case AppLifecycleState.detached: - print("4"); + print("当前页面即将退出======"); break; } }