如何在Flutter中将应用程序从后台带到前景 [英] How to bring an app from background to foreground in flutter

查看:66
本文介绍了如何在Flutter中将应用程序从后台带到前景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个正在管理音频呼叫的应用程序.当调用添加项并且应用程序在后台运行时,我需要将应用程序置于前台状态.我尝试使用导航器.推送但没有任何结果.

I have an app that is managing audio calls. When a call is made to the add and the app is running in the background I need to bring the app in the foreground state. I tried to use Navigator. push but without any result.

推荐答案

您可以使用软件包 bringtoforeground .就其版本而言,它还处于早期阶段,但是可以正常工作.

You can use the package bringtoforeground. It's fairly in the early stages with respect to its version but it works.

iOS

但这仅适用于android ,您要记住,背景上的iOS应用已被破坏.你可以读这个吗 在此处查看详细信息

But this only works on android, you have to keep in mind that iOS apps that are on the background are destroyed. you can read this do see details here

Android

因此,此实现仅适用于Android.

So this implementation will only work on Android.

此程序包的最好之处在于,您可以将它与Firebase Cloud Messaging(FCM)或其他任何程序一起使用.

The best thing with this package is that you can use it with Firebase Cloud Messaging (FCM) or any other for that matter.

这是他们的示例,Bringtoforeground.bringAppToForeground();这是用于将应用程序置于前台的代码.

This is their example, Bringtoforeground.bringAppToForeground(); this is the piece of code you use to bring your app to the foreground.

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'),
        ),
      ),
    );
  }
}

这篇关于如何在Flutter中将应用程序从后台带到前景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆