flutter和android原生通信的理解(1)
flutter和android原生通信的理解(2)
android原生的 flutter api
FlutterActivity
public class FlutterActivity extends Activity {
//...
protected FlutterActivityAndFragmentDelegate delegate;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
//...
delegate = new FlutterActivityAndFragmentDelegate(this);
delegate.onAttach(this);
delegate.onActivityCreated(savedInstanceState);
//...
}
@Override
protected void onStart() {
super.onStart();
lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_START);
delegate.onStart();
}
@Override
protected void onResume() {
super.onResume();
//...
delegate.onResume();
}
FlutterFragment
public class FlutterFragment extends Fragment {
//...
FlutterActivityAndFragmentDelegate delegate;
//...
@Override
public void onAttach(@NonNull Context context) {
super.onAttach(context);
delegate = new FlutterActivityAndFragmentDelegate(this);
delegate.onAttach(context);
}
@Nullable
@Override
public View onCreateView(
LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return delegate.onCreateView(inflater, container, savedInstanceState);
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
delegate.onActivityCreated(savedInstanceState);
}
@Override
public void onStart() {
super.onStart();
delegate.onStart();
}
//...
FlutterActivity 和 FlutterFragment 类都有一个FlutterActivityAndFragmentDelegate delegate 对象;
final class FlutterActivityAndFragmentDelegate {
private FlutterEngine flutterEngine;
private FlutterView flutterView;
void onAttach(@NonNull Context context) {
//..
if (flutterEngine == null) {
setupFlutterEngine();
}
}
void setupFlutterEngine() {
//...
flutterEngine =new FlutterEngine(
host.getContext(),
host.getFlutterShellArgs().toArray(),
/*automaticallyRegisterPlugins=*/ false);
//...
}
View onCreateView(
LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
if (host.getRenderMode() == RenderMode.surface) {
FlutterSurfaceView flutterSurfaceView =
new FlutterSurfaceView(
host.getActivity(), host.getTransparencyMode() == TransparencyMode.transparent);
//..
// Create the FlutterView that owns the FlutterSurfaceView.
flutterView = new FlutterView(host.getActivity(), flutterSurfaceView);
} else {
FlutterTextureView flutterTextureView = new FlutterTextureView(host.getActivity());
//..
// Create the FlutterView that owns the FlutterTextureView.
flutterView = new FlutterView(host.getActivity(), flutterTextureView);
}
// Add listener to be notified when Flutter renders its first frame.
flutterView.addOnFirstFrameRenderedListener(flutterUiDisplayListener);
flutterSplashView = new FlutterSplashView(host.getContext());
//..
flutterSplashView.displayFlutterViewWithSplash(flutterView, host.provideSplashScreen());
Log.v(TAG, "Attaching FlutterEngine to FlutterView.");
flutterView.attachToFlutterEngine(flutterEngine);
return flutterSplashView;
}
- FlutterView 是android原生view的封装;
持有flutterSurfaceView对象,flutterTextureView对象;
Paints a Flutter UI on a {@link android.view.Surface}.
用来绘制Flutter UI,当然绘制需要借助flutterEngine
- FlutterEngine flutterEngine 是 安卓原生平台 封装的接口
为了调用Flutter层Engine;
FlutterEngine
public class FlutterEngine {
//都在FlutterEngine 中创建
@NonNull private final FlutterJNI flutterJNI;
@NonNull private final FlutterRenderer renderer;
@NonNull private final DartExecutor dartExecutor;
//..
public FlutterEngine(
@NonNull Context context,
@NonNull FlutterLoader flutterLoader,
@NonNull FlutterJNI flutterJNI,
@NonNull PlatformViewsController platformViewsController,
@Nullable String[] dartVmArgs,
boolean automaticallyRegisterPlugins) {
this.flutterJNI = flutterJNI;
//..
attachToJni();
this.dartExecutor = new DartExecutor(flutterJNI, context.getAssets());
this.dartExecutor.onAttachedToJNI();
// TODO(mattcarroll): FlutterRenderer is temporally coupled to attach(). Remove that coupling if
// possible.
this.renderer = new FlutterRenderer(flutterJNI);
//..
}
- FlutterJNI flutterJNI;是android原生 调用底层c++的 native接口
- DartExecutor dartExecutor;
是android原生 调用 Fluter层dart代码的 接口,具体实现肯定离不开FlutterJNI;
DartExecutor
public class DartExecutor {
//..
@NonNull private final FlutterJNI flutterJNI;
@NonNull private final DartMessenger dartMessenger;
@NonNull private final BinaryMessenger binaryMessenger;//发送消息
@Nullable private IsolateServiceIdListener isolateServiceIdListener;//接收消息
private final BinaryMessenger.BinaryMessageHandler isolateChannelMessageHandler =
new BinaryMessenger.BinaryMessageHandler() {
@Override
public void onMessage(ByteBuffer message, final BinaryReply callback) {
isolateServiceId = StringCodec.INSTANCE.decodeMessage(message);
if (isolateServiceIdListener != null) {
isolateServiceIdListener.onIsolateServiceIdAvailable(isolateServiceId);
}
}
};
public DartExecutor(@NonNull FlutterJNI flutterJNI, @NonNull AssetManager assetManager) {
this.flutterJNI = flutterJNI;
//..
this.dartMessenger = new DartMessenger(flutterJNI);
dartMessenger.setMessageHandler("flutter/isolate", isolateChannelMessageHandler);
this.binaryMessenger = new DefaultBinaryMessenger(dartMessenger);
}
//...
/** @deprecated Use {@link #getBinaryMessenger()} instead. */
@Deprecated
@Override
@UiThread
public void send(@NonNull String channel, @Nullable ByteBuffer message) {
binaryMessenger.send(channel, message);
}
/**
* Returns a {@link BinaryMessenger} that can be used to send messages to, and receive messages
* from, Dart code that this {@code DartExecutor} is executing.
*/
@NonNull
public BinaryMessenger getBinaryMessenger() {
return binaryMessenger;
}
}
- DartMessenger dartMessenger;信使,用来跟 Flutter层 通信
- BinaryMessenger binaryMessenger; 通过信使 对Flutter层 发送消息
- IsolateServiceIdListener isolateServiceIdListener; 通过信使 接收 来自Flutter层 的消息
Flutter plugin 在android原生的注册
关键是注册MethodChannel 通过FluuterJNI 到底层c++;
@Override
public void onAttachedToEngine(FlutterPluginBinding flutterPluginBinding) {
channel = new MethodChannel(flutterPluginBinding.getFlutterEngine().getDartExecutor(), "flutter_datafinder");
channel.setMethodCallHandler(this);
//..
}
//或者,差异在于Flutter 版本1.12前后 注册插件的方法不同;
public static void registerWith(Registrar registrar) {
final MethodChannel channel = new MethodChannel(registrar.messenger(), "flutter_datafinder");
channel.setMethodCallHandler(new FlutterDataFinderPlugin());
//
application = (Application) registrar.context().getApplicationContext();
}
为 MethodChannel 传入 信使的发送对象 BinaryMessenger messenger;
channel.setMethodCallHandler() 为channel设置MethodCallHandler对象;然后MethodCallHandler 包装成为 信使的接收对象,来接收来自Flutter层的消息;
网友评论