主入口文件
package com.example.flutter_plus
import android.os.Bundle
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine
//主入口
class MainActivity: FlutterActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
//引用
CustomGeneratedPluginRegistrant.registerWith(FlutterEngine(this))
}
}
注冊(cè)文件
package com.example.flutter_plus
import androidx.annotation.Keep
import io.flutter.Log
import io.flutter.embedding.engine.FlutterEngine
@Keep
object CustomGeneratedPluginRegistrant {
private const val tag = "CustomGeneratedPluginRegistrant"
fun registerWith(flutterEngine: FlutterEngine) {
try {
flutterEngine.plugins.add(ImesBasePlugins())
} catch (e: Exception) {
Log.e(tag, "Error registering plugin wall_e_app.plugin, com.example.wall_e_app.plugin.ImesBasePlugins", e)
}
}
}
實(shí)現(xiàn)文件
package com.example.flutter_plus
import android.content.Context
import android.widget.Toast
import androidx.annotation.NonNull
import io.flutter.embedding.engine.plugins.FlutterPlugin
import io.flutter.plugin.common.BinaryMessenger
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
///基礎(chǔ)庫(kù)插件
class ImesBasePlugins : MethodCallHandler, FlutterPlugin {
///屬性 - 0.1
private lateinit var methodChannel: MethodChannel
private lateinit var applicationContext: Context
///新入口 - 開始 0.2
override fun onAttachedToEngine(@NonNull flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) {
setup(flutterPluginBinding.binaryMessenger, flutterPluginBinding.applicationContext)
}
///設(shè)置 0.3
private fun setup(messenger: BinaryMessenger, context: Context) {
applicationContext = context
methodChannel = MethodChannel(messenger, "imes_base_plugins_wall_e_outer_network")
methodChannel.setMethodCallHandler(this)
}
///出口 - 結(jié)束 0.5
override fun onDetachedFromEngine(@NonNull binding: FlutterPlugin.FlutterPluginBinding) {
methodChannel.setMethodCallHandler(null)
}
///flutter->Android 0.4
override fun onMethodCall(@NonNull methodCall: MethodCall, @NonNull result: Result) {
when (methodCall.method) {
"showToast" -> {
Toast.makeText(applicationContext, methodCall.argument("data") ?: "", Toast.LENGTH_SHORT).show()
}
}
}
}