- import
import android.content.Context;
import io.flutter.embedding.engine.plugins.FlutterPlugin;
import io.flutter.plugin.common.BinaryMessenger;
- implements添加FlutterPlugin
- 添加以下代码,酌情修改
static MethodChannel methodChannel;
private Context applicationContext;
@Override
public void onAttachedToEngine(@NonNull FlutterPlugin.FlutterPluginBinding binding) {
onAttachedToEngine(binding.getApplicationContext(), binding.getBinaryMessenger());
}
private void onAttachedToEngine(Context applicationContext, BinaryMessenger messenger) {
this.applicationContext = applicationContext;
channel = new MethodChannel(messenger, "xx");
channel.setMethodCallHandler(this);
}
@Override
public void onDetachedFromEngine(@NonNull FlutterPlugin.FlutterPluginBinding binding) {
applicationContext = null;
channel.setMethodCallHandler(null);
channel = null;
}
- 修改registerWith
final XXPlugin instance = new XXPlugin(registrar.activity());
registrar.addActivityResultListener(instance);
instance.onAttachedToEngine(registrar.context(), registrar.messenger());
- android/app/src/main/AndroidManifest.xml在application标签中添加以下内容
<meta-data android:name="flutterEmbedding" android:value="2" />
- 大功告成