我正在开发一个带有背景内容的Fflight插件.
最近,我遇到了一些关于android_alarm_manager插件中的Flutter android embedding的事情.
README
人中有一部分人说:
对于Flatter Android Embedding V1,必须为后台服务提供一个回调,以便在后台服务器上注册插件.
- Flutter android embeddingV1或V2到底是什么?
- 这两者有什么不同?
我正在开发一个带有背景内容的Fflight插件.
最近,我遇到了一些关于android_alarm_manager插件中的Flutter android embedding的事情.
README
人中有一部分人说:
对于Flatter Android Embedding V1,必须为后台服务提供一个回调,以便在后台服务器上注册插件.
Ffltter发布了新版本的Android Embedding.这是负责将Ffltter集成到Android应用程序中的Android代码.它包括像FlutterActivity
、FlutterFragment
、FlutterView
和FlutterEngine
这样的类.v2 Android嵌入包括对标准Android生命周期事件的支持,以及将Flutter 执行与Android UI分离,这是v1 Android嵌入所缺少的.在v2Android嵌入的开发过程中,很明显,现有的Flutter 插件API不足以处理v2Android嵌入的新功能.需要一个新的Android插件API.
在旧的v1Android嵌入中,所有插件都是在Android应用程序的一开始就初始化和配置的,而且只有一次Flutter 体验.在v2嵌入中,我们不假设插件何时初始化,每个FlutterEngine插件必须初始化一次.因此,所有用于安卓的Ffltter插件现在都必须支持实例化,而不是静电初始化,并且它们必须支持附加到FlutterEngine和从FlutterEngine分离.以下代码示例演示了旧的v1插件初始化实现和新的v2插件初始化过程之间的区别.
旧插件初始化
class MyOldPlugin {
public static void registerWith(PluginRegistrar registrar) {
// Obtain any references that the plugin requires from the
// registrar.
//
// This plugin is now considered "initialized" and "attached"
// to a Flutter experience.
}
}
新插件初始化
class MyNewPlugin implements FlutterPlugin {
public MyNewPlugin() {
// All Android plugin classes must support a no-args
// constructor. A no-arg constructor is provided by
// default without declaring one, but we include it here for
// clarity.
//
// At this point your plugin is instantiated, but it
// isn't attached to any Flutter experience. You should not
// attempt to do any work here that is related to obtaining
// resources or manipulating Flutter.
}
@override
public void onAttachedToFlutterEngine(FlutterPluginBinding binding) {
// Your plugin is now attached to a Flutter experience
// represented by the given FlutterEngine.
//
// You can obtain the associated FlutterEngine with
// binding.getFlutterEngine()
//
// You can obtain a BinaryMessenger with
// binding.getBinaryMessenger()
//
// You can obtain the Application context with
// binding.getApplicationContext()
//
// You cannot access an Activity here because this
// FlutterEngine is not necessarily displayed within an
// Activity. See the ActivityAware interface for more info.
}
@override
public void onDetachedFromFlutterEngine(FlutterPluginBinding binding) {
// Your plugin is no longer attached to a Flutter experience.
// You need to clean up any resources and references that you
// established in onAttachedToFlutterEngine().
}
}
此外,您的插件不能依赖于onAttachedToFlutterEngine()中的活动引用.仅仅因为您的插件附加到Flutter 体验,并不意味着Flutter 体验将在活动中展示.This is one of the most significant differences between the old and new plugin APIs美元.在旧的v1插件API中,插件作者可以依赖于活动立即和永久可用.这已经不是真的了.
有关更多信息,请参见https://medium.com/flutter/modern-flutter-plugin-development-4c3ee015cf5a