flutter - 如何在颤动中将应用程序从后台带到前台
问题描述
我有一个管理音频呼叫的应用程序。当调用 add 并且应用程序在后台运行时,我需要将应用程序置于前台状态。我尝试使用导航器。推但没有任何结果。
解决方案
您可以使用包bringtoforeground。就其版本而言,它还处于早期阶段,但它确实有效。
iOS
但这仅适用于 android,您必须记住,后台的 iOS 应用程序已被破坏。您可以阅读此 内容,请在此处查看详细信息
安卓
所以这个实现只适用于Android。
这个包最好的地方是你可以将它与 Firebase Cloud Messaging (FCM) 或其他任何东西一起使用。
这是他们的示例,Bringtoforeground.bringAppToForeground();
这是您用来将应用程序置于前台的代码。
import 'dart:async';
import 'package:bringtoforeground/bringtoforeground.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
void main() => runApp(MyApp());
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
String _platformVersion = 'Unknown';
@override
void initState() {
super.initState();
initPlatformState();
}
// Platform messages are asynchronous, so we initialize in an async method.
Future<void> initPlatformState() async {
String platformVersion;
// Platform messages may fail, so we use a try/catch PlatformException.
try {
Timer.periodic(Duration(seconds: 10), (t) {
Bringtoforeground.bringAppToForeground(); //This is the only thing that matters
});
} on PlatformException {
platformVersion = 'Failed to get platform version.';
}
// If the widget was removed from the tree while the asynchronous platform
// message was in flight, we want to discard the reply rather than calling
// setState to update our non-existent appearance.
if (!mounted) return;
setState(() {
_platformVersion = platformVersion;
});
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Plugin example app'),
),
body: Center(
child: Text('Running on: $_platformVersion\n'),
),
),
);
}
}
推荐阅读
- scala - WriterT 还是 ReaderWriterStateT?
- javascript - Json 返回 null JavaScript
- date - spring boot 测试 h2 data.sql 错误的语言环境日期格式
- apache-kafka - Apache Kafka Connect 根据记录值路由消息
- python - 如何在 Python 中使用正则表达式从字符串中删除两个“下划线数字”子字符串?
- mapbox - 自定义矢量切片与 mapbox 中由 geojson 源制作的切片不匹配
- spring-boot - 如何解决spring-data-neo4j和Grails 3.3.6之间的spring bean冲突
- python - Python & Json:多行输入数据到 Json
- r - 卷曲包未安装
- javascript - 印度货币的正则表达式模式进行验证