当我创建flutter插件时,默认情况下插件类中有两个方法:
override fun onAttachedToEngine(flutterPluginBinding: FlutterPlugin.FlutterPluginBinding)
和
fun registerWith(registrar: Registrar)
文件上的评论说:鼓励在onAttachedToEngine和registerWith之间共享逻辑,以保持它们的功能等效。根据用户的项目,只调用onAttachedToEngine或registerWith中的一个。onAttachedToEngine或registerWith必须在同一个类中定义
现在,我需要从这里开始另一个活动,使用
activity.startActivityForResult()
。可以使用registrar.activity()
在注册表中获取对活动的引用。如何在方法
onAttachedToEngine(flutterPluginBind: FlutterPlugin. FlutterPluginBind)
中执行此操作?
通过实现ActivityAware接口
https://www.codenong.com/jseb7df49fdfb1/
package com.example.fluttertoast
import android.app.Activity
import android.content.Context
import android.widget.Toast
import androidx.annotation.NonNull;
import io.flutter.embedding.engine.plugins.FlutterPlugin
import io.flutter.embedding.engine.plugins.activity.ActivityAware
import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel
import io.flutter.plugin.common.MethodChannel.MethodCallHandler
import io.flutter.plugin.common.MethodChannel.Result
import io.flutter.plugin.common.PluginRegistry.Registrar
/** FluttertoastPlugin */
class FluttertoastPlugin: FlutterPlugin, MethodCallHandler, ActivityAware {
private lateinit var channel : MethodChannel
private lateinit var activity:Activity
private lateinit var context: Context
override fun onAttachedToEngine(@NonNull flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) {
channel = MethodChannel(flutterPluginBinding.flutterEngine.dartExecutor, "fluttertoast")
channel.setMethodCallHandler(this)
this.context = flutterPluginBinding.applicationContext
}
companion object {
@JvmStatic
fun registerWith(registrar: Registrar) {
val channel = MethodChannel(registrar.messenger(), "fluttertoast")
channel.setMethodCallHandler(FluttertoastPlugin())
}
}
override fun onMethodCall(@NonNull call: MethodCall, @NonNull result: Result) {
if (call.method == "getPlatformVersion") {
Toast.makeText(activity,"Hello!",Toast.LENGTH_SHORT).show()
result.success("Android ${android.os.Build.VERSION.RELEASE}")
} else {
result.notImplemented()
}
}
override fun onDetachedFromEngine(@NonNull binding: FlutterPlugin.FlutterPluginBinding) {
channel.setMethodCallHandler(null)
}
override fun onDetachedFromActivity() {}
override fun onReattachedToActivityForConfigChanges(binding: ActivityPluginBinding) {
onAttachedToActivity(binding)
}
override fun onAttachedToActivity(binding: ActivityPluginBinding) {
this.activity = binding.activity
}
override fun onDetachedFromActivityForConfigChanges() {}
}
匿名用户
注:
您可以通过实现ActivityAware接口获得对activity的引用,但如果您使用setMethodCallHandler(…)
onAttachToEngine()方法onAttachToActivity()中的code>,并且您永远无法访问活动
看看下面的例子
不起作用的部分:在下面的示例中,从不调用onAttachToActivity()
class AndroidLongTaskPlugin : FlutterPlugin, ActivityAware {
private var activity: FlutterActivity? = null
override fun onAttachedToEngine(@NonNull flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) {
//activity is null here
//also onAttachToActivity will never be called because we are calling setMethodHandler here
channel = MethodChannel(binaryMessenger, CHANNEL_NAME)
channel.setMethodCallHandler { call, result ->
//our code
}
}
override fun onDetachedFromEngine(@NonNull binding: FlutterPlugin.FlutterPluginBinding) {
channel?.setMethodCallHandler(null)
}
override fun onAttachedToActivity(binding: ActivityPluginBinding) {
activity = binding.activity as FlutterActivity
}
//rest of the methods
}
下面是一个工作示例:
class MyPlugin : FlutterPlugin, ActivityAware {
private var activity: FlutterActivity? = null
private var binaryMessenger: BinaryMessenger? = null
override fun onAttachedToEngine(@NonNull flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) {
binaryMessenger = flutterPluginBinding.binaryMessenger
}
override fun onDetachedFromEngine(@NonNull binding: FlutterPlugin.FlutterPluginBinding) {
Log.d("DART/NATIVE", "onDetachedFromEngine")
channel?.setMethodCallHandler(null)
}
override fun onAttachedToActivity(binding: ActivityPluginBinding) {
Log.d("DART/NATIVE", "onAttachedToActivity")
activity = binding.activity as FlutterActivity
//here we have access to activity
//also make sure to setMethodCallHandler here
channel.setMethodCallHandler { call, result ->
//our code
}
}
//rest of the methods
}
在这里找到了解决方案
实现ActivityAware,其方法之一是
override fun onAttachedToActivity(binding: ActivityPluginBinding) {
this.activity = binding.activity;
}
我正在使用firebase云功能向特定用户发送通知。这是我从函数发送的有效负载。 我正在使用firebase_messaging(flutter package:https://pub.dartlang.org/packages/firebase_messaging)接收通知,并且我已经编写了onMessage、onLaunch和onResume方法的代码。 因此,当我使用Admin SDK发送消
所以…我得到了一个调用Api,其中还包括7天的预测。我设法显示了当前的天气(温度和图标),但如何获得未来4天的天气预报? 我的API:https://api.openweathermap.org/data/2.5/onecall?lat=lat 我的型号: 我的回答是: 响应对象: 来自OneCall API的JSON:
我正在Linux Ubuntu上配置Flatter SDK 我在文件中为和指定了,但我在运行时收到此错误:
将颤振集成到主机应用程序(docs)时,有几种方法可以实现,其中一种(最简单的)方法是通过颤振活动类在新活动中打开颤振。这样地: 传统上,对于Flutter侧的Android风格窗口,我们创建带有后退按钮的AppBar。 此AppBar后退按钮和Android系统后退按钮的行为必须相同:按下后退按钮时,前台活动必须关闭(完成)。 目前系统后退按钮确实关闭了,但如何从flutters AppBar后
我已经使用了< code >扩展的和< code >灵活的小部件,它们看起来工作起来是一样的。 扩展和灵活有什么区别?
我正在尝试使用图像选择器,但我无法获取太多。每当我调用Imagepicker时,它都会显示错误,因为在通道插件上找不到方法pickImage的实现。颤振io/图像选择器。请让我知道这个问题。 我更新了我的问题并添加了pubspec.yaml