简体   繁体   English

如何在颤振中注册应用程序侦听器以进行互联网连接事件?

[英]How to register an app listener in flutter for an internet connectivity event?

I have an app flutter that runs in Android and iOS.我有一个在 Android 和 iOS 上运行的应用程序颤振。

This app is used in areas without any internet connection.此应用程序用于没有任何互联网连接的地区。 When the user arrives at some place with internet connection, all the offline data must be sent to the servers.当用户到达某个有互联网连接的地方时,必须将所有离线数据发送到服务器。

Have an background task running and checking in loop for internet connections, or even a recurrent scheduled task, is not an option because:运行后台任务并检查互联网连接的循环,甚至是循环计划任务都不是一种选择,因为:

  • The moment between the use of the app in the field and the moment that the user will be in place with internet connection might take many hours.从在现场使用应用程序到用户使用 Internet 连接到位之间的那一刻可能需要几个小时。
  • Running threads in background will consume resources and draw the mobile battery.在后台运行线程会消耗资源并消耗手机电池。
  • Android/iOS might finish the background process and the data not be sent unless the user open the app. Android/iOS 可能会完成后台进程,除非用户打开应用程序,否则不会发送数据。

I'm wondering how to register a kind of listener, listening for "internet connectivity events" broadcasted by operating system when they happen and that will awake my app and trigger callback.我想知道如何注册一种侦听器,侦听操作系统广播的“互联网连接事件”,这将唤醒我的应用程序并触发回调。

Then, I don't have to keep an unnecessary background task running infinitely but I can still do the job when the mobile get connected in the internet, even having my app closed/sleeping.然后,我不必让不必要的后台任务无限运行,但是当移动设备连接到互联网时,即使我的应用程序关闭/休眠,我仍然可以完成这项工作。

How can I do it in flutter ?我怎么能在颤振中做到这一点?

There's a package that listens to connectivity status called connectivity_plus , you can subscribe to a stream like so:有一个包可以监听连接状态,称为connectivity_plus ,你可以像这样订阅一个流:

    import 'package:connectivity_plus/connectivity_plus.dart';

@override
initState() {
  super.initState();

  subscription = Connectivity().onConnectivityChanged.listen((ConnectivityResult result) {
    // Got a new connectivity status!
  })
}

// Be sure to cancel subscription after you are done
@override
dispose() {
  super.dispose();

  subscription.cancel();
}

However in the documentation, it states clearly that connectivity changes are no longer communicated to Android apps in the background starting with Android O. You should always check for connectivity status when your app is resumed.但是在文档中,它明确指出从 Android O 开始,连接更改不再在后台传达给 Android 应用程序。当您的应用程序恢复时,您应该始终检查连接状态。 The broadcast is only useful when your application is in the foreground.广播仅在您的应用程序处于前台时才有用。

You might wanna research that and see if it's possible to do so in the background.你可能想研究一下,看看是否有可能在后台这样做。 Here's a similar question which might help you out这是一个类似的问题,可能会帮助你

You can try background task and add a delay of your choice.您可以尝试后台任务并添加您选择的延迟。 That should work.那应该工作。 Please check this link.请检查此链接。 https://pub.dev/packages/flt_worker_nullsafety https://pub.dev/packages/flt_worker_nullsafety

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM