FlutterPlugin 获取上下文Activity

Posted 安果移不动

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了FlutterPlugin 获取上下文Activity相关的知识,希望对你有一定的参考价值。

我直接贴代码了 不讲武德了 添加这个 

ActivityAware 可以获取Activity

直接就可以获取 

Context
package com.anguomob.flutter_plugin_anguo

import android.app.Activity
import android.content.Context
import androidx.annotation.NonNull
import com.anguomob.flutter_plugin_anguo.utils.AppUtils
import com.anguomob.flutter_plugin_anguo.utils.FlutterUtils

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

/** FlutterPluginAnguoPlugin */
class FlutterPluginAnguoPlugin: FlutterPlugin, MethodCallHandler, ActivityAware {
  /// The MethodChannel that will the communication between Flutter and native Android
  ///
  /// This local reference serves to register the plugin with the Flutter Engine and unregister it
  /// when the Flutter Engine is detached from the Activity
  private lateinit var channel : MethodChannel


  private lateinit var context: Context
  private lateinit var activity: Activity

  override fun onAttachedToEngine(@NonNull flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) {
    channel = MethodChannel(flutterPluginBinding.binaryMessenger, "anguo")
    channel.setMethodCallHandler(this)
    context = flutterPluginBinding.applicationContext
  }

  override fun onMethodCall(@NonNull call: MethodCall, @NonNull result: Result) {

    if (call.method != null) {
     
    } else {
      result.notImplemented();
    }
  }

  override fun onDetachedFromEngine(@NonNull binding: FlutterPlugin.FlutterPluginBinding) {
    channel.setMethodCallHandler(null)
  }

  override fun onAttachedToActivity(p0: ActivityPluginBinding) {

  }

  override fun onDetachedFromActivityForConfigChanges() {

  }

  override fun onReattachedToActivityForConfigChanges(p0: ActivityPluginBinding) {
    activity = p0.activity;
  }

  override fun onDetachedFromActivity() {

  }
}

 

以上是关于FlutterPlugin 获取上下文Activity的主要内容,如果未能解决你的问题,请参考以下文章

Flutter实战之FlutterPlugin的应用一步步构建FlutterToast

Flutter实战之FlutterPlugin的应用一步步构建FlutterToast

PluginApplicationException:无法应用插件类“FlutterPlugin”

Ambiguous method call. Both findViewById (int) in AppCompatActivity and findViewById (int) in Activi

在颤振插件中获取活动参考

Activiti 流程部署方式 activi 动态部署(高级源代码篇)