flutter android end analytical principle

First look MainActivity,

Inheritance FlutterActivity

Look FlutterActivity Code, inheritance Activity, achieve Provider, PluginRegistry, ViewFactory

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

private final FlutterActivityDelegate delegate = new FlutterActivityDelegate(this, this);
private final FlutterActivityEvents eventDelegate;
private final Provider viewProvider;
private final PluginRegistry pluginRegistry;

public FlutterActivity() {
this.eventDelegate = this.delegate;
this.viewProvider = this.delegate;
this.pluginRegistry = this.delegate;
}

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.eventDelegate.onCreate(savedInstanceState);
}

A look delegate knew proxy mode

In the life cycle onCrreate, the view is created by eventDelegate, while the object is initialized assignment in the constructor,

FlutterActivityEvents is an interface class inherits ActivityResultListener life cycle, RequestPermissionsResultListener permission requests, ComponentCallbacks2 first regardless of the class.

FlutterActivityDelegate is to achieve FlutterActivityEvents class.

See FlutterActivityDelegate class function onCreate

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30

public void onCreate(Bundle savedInstanceState) {
if (VERSION.SDK_INT >= 21) {
Window window = this.activity.getWindow();
window.addFlags(-2147483648);
window.setStatusBarColor(1073741824);
window.getDecorView().setSystemUiVisibility(1280);
}

String[] args = getArgsFromIntent(this.activity.getIntent());
FlutterMain.ensureInitializationComplete(this.activity.getApplicationContext(), args);
this.flutterView = this.viewFactory.createFlutterView(this.activity);
if (this.flutterView == null) {
FlutterNativeView nativeView = this.viewFactory.createFlutterNativeView();
this.flutterView = new FlutterView(this.activity, (AttributeSet)null, nativeView);
this.flutterView.setLayoutParams(matchParent);
this.activity.setContentView(this.flutterView);
this.launchView = this.createLaunchView();
if (this.launchView != null) {
this.addLaunchView();
}
}

if (!this.loadIntent(this.activity.getIntent())) {
String appBundlePath = FlutterMain.findAppBundlePath(this.activity.getApplicationContext());
if (appBundlePath != null) {
this.runBundle(appBundlePath);
}
}
}

First version of the android made a judgment, when more than 6.0, set the status bar color, and windowed mode.

side communication terminal and dart android

1, dart end side code calls android

1, the native code is android

Create a new class TestPlugin achieve MethodChannel class internal interface class MethodCallHandler.

Rewrite onMethodCall method, write code that need to be implemented in logic here

Finally, the plug-in bindings.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34

public class TestPlugin implements MethodChannel.MethodCallHandler {

public static final String CHANNEL = "plugin/test";

static MethodChannel channel;

// 上下文
private Activity activity;

private TestPlugin(Activity activity) {
this.activity = activity;
}


public static void registerWith(PluginRegistry.Registrar registrar) {
channel = new MethodChannel(registrar.messenger(), CHANNEL);
TestPlugin plugin = new TestPlugin(registrar.activity());
// 在此通道上接受方法调用的回调
channel.setMethodCallHandler(plugin);
}

@Override
public void onMethodCall(MethodCall methodCall, MethodChannel.Result result) {

if(methodCall.method.equals("test")) {
Toast.makeText(activity.getApplicationContext(), "测试dart调用android原生插件", Toast.LENGTH_SHORT).show();

result.success("调用成功");
}
// 当未找到该函数
result.notImplemented();
}
}

在MainActivity中绑定

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

public class MainActivity extends FlutterActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
GeneratedPluginRegistrant.registerWith(this);

registerPlugin(this);

}


private void registerPlugin(PluginRegistry registry) {
TestPlugin.registerWith(registry.registrarFor(TestPlugin.CHANNEL));
}

}

2、在dart中调用该原生方法

1
2
3
4
5
6
7

static const _platform = const MethodChannel('plugin/test');

_toast() {
/// 调用原生的方法
_platform.invokeMethod('test');
}

一个原生插件调用就完成了。

原文:大专栏  flutter android端 原理解析


Guess you like

Origin www.cnblogs.com/petewell/p/11444849.html