플러터 안드로이드 최종 분석 원리

우선 검토 MainActivity,

상속 FlutterActivity

공급자 PluginRegistry, ViewFactory의 달성, FlutterActivity 코드, 상속 활동을 봐

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

개인 최종 FlutterActivityDelegate 대리자 = 새로운 FlutterActivityDelegate (이 이러한);
민간 최종 FlutterActivityEvents eventDelegate;
민간 최종 제공자 viewProvider;
민간 최종 PluginRegistry pluginRegistry;

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

보호 공간에서 onCreate (번들 savedInstanceState) {
super.onCreate (savedInstanceState);
this.eventDelegate.onCreate (savedInstanceState);
}

봐 위임 프록시 모드를 알고

객체가 생성자에서 할당을 초기화하는 동안 수명주기 onCrreate에서보기가, eventDelegate에 의해 생성된다,

FlutterActivityEvents는 인터페이스 클래스가 처음에 관계없이 클래스의 ActivityResultListener 수명주기, RequestPermissionsResultListener 권한 요청, ComponentCallbacks2을 상속합니다.

FlutterActivityDelegate는 FlutterActivityEvents 클래스를 달성하는 것입니다.

FlutterActivityDelegate 클래스 함수를 참조에서 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

공공 무효에서 onCreate (번들 savedInstanceState) {
경우 (VERSION.SDK_INT> = 21) {
윈도우 창 this.activity.getWindow = ();
window.addFlags (-2147483648);
window.setStatusBarColor (1073741824);
. window.getDecorView () setSystemUiVisibility (1280);
}

문자열 []에 args = getArgsFromIntent (this.activity.getIntent ());
FlutterMain.ensureInitializationComplete (this.activity.getApplicationContext ()에 args);
this.flutterView = this.viewFactory.createFlutterView (this.activity);
경우 (this.flutterView == NULL) {
FlutterNativeView nativeView this.viewFactory.createFlutterNativeView = ();
this.flutterView = 새로운 FlutterView (this.activity (속성 세트) NULL, nativeView);
this.flutterView.setLayoutParams (matchParent);
this.activity.setContentView (this.flutterView);
this.launchView this.createLaunchView = ();
만약 (! this.launchView = NULL) {
this.addLaunchView ();
}
}

경우 {(this.loadIntent (this.activity.getIntent ())!)
문자열 appBundlePath = FlutterMain.findAppBundlePath (this.activity.getApplicationContext ());
경우 (appBundlePath = 널!) {
this.runBundle (appBundlePath);
}
}
}

이상 6.0, 상태 표시 줄의 색상, 창 모드를 설정하면 안드로이드의 첫 번째 버전은 판단했다.

측 통신 단말 다트 로이드

1 다트 측 코드는 호출 로이드

1, 네이티브 코드는 안드로이드입니다

새로운 클래스 TestPlugin이 MethodChannel 클래스 내부 인터페이스 클래스 MethodCallHandler을 달성 만듭니다.

여기 로직 구현 될 필요가 코드를 작성하는 방법을 onMethodCall 다시 작성

마지막으로, 플러그인 바인딩.

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端 原理解析


추천

출처www.cnblogs.com/petewell/p/11444849.html