实时监听权限

master
王亚玲 2 years ago
parent d4eab235c9
commit b13c4b10cc

@ -26,32 +26,40 @@ class _HomePageState extends State<HomePage> with WidgetsBindingObserver{
@override @override
void initState() { void initState() {
super.initState(); super.initState();
//
WidgetsBinding.instance.addObserver(this);
_listenForPermissionStatus(); _listenForPermissionStatus();
setState(() { setState(() {});
}
}); @override
void dispose(){
super.dispose();
//
WidgetsBinding.instance.removeObserver(this);
} }
Future<void> _listenForPermissionStatus() async { Future<void> _listenForPermissionStatus() async {
sms=await Permission.sms.request().isGranted; sms=await Permission.sms.request().isGranted;
plone =await Permission.phone.request().isGranted; plone =await Permission.phone.request().isGranted;
setState(() {}); setState(() {});
} }
@override
void didChangeAppLifecycleState(AppLifecycleState state){ void didChangeAppLifecycleState(AppLifecycleState state){
super.didChangeAppLifecycleState(state); super.didChangeAppLifecycleState(state);
switch (state){ switch (state){
//
// UI
case AppLifecycleState.inactive: case AppLifecycleState.inactive:
print("1"); print("应用处于闲置状态,这种状态的应用应该假设他们可能在任何时候暂停 切换到后台会触发======");
break; break;
case AppLifecycleState.resumed: case AppLifecycleState.resumed:
print("2"); print("应用进入前台——————————");
_listenForPermissionStatus();
break; break;
case AppLifecycleState.paused: case AppLifecycleState.paused:
print("3"); print("应用处于不可见状态 后台======");
break; break;
case AppLifecycleState.detached: case AppLifecycleState.detached:
print("4"); print("当前页面即将退出======");
break; break;
} }
} }

Loading…
Cancel
Save