当前位置:   article > 正文

android调用flutter页面,flutter-android调用dart android原生启动flutter界面

android调用flutter 页面 返回值

package com.topway.cms;

import android.os.Bundle;

import android.text.TextUtils;

import com.topway.FlutterNativePlugin;

import com.topway.bean.MyEvent;

import com.topway.utils.MyLog;

import org.greenrobot.eventbus.EventBus;

import org.greenrobot.eventbus.Subscribe;

import org.greenrobot.eventbus.ThreadMode;

import io.flutter.app.FlutterActivity;

import io.flutter.plugin.common.EventChannel;

import io.flutter.plugins.GeneratedPluginRegistrant;

public class MainActivity extends FlutterActivity {

public static String CHANNEL = "com.native/openLogin";

EventChannel.EventSink mySink;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

GeneratedPluginRegistrant.registerWith(this);

FlutterNativePlugin.registerWith(this.registrarFor(FlutterNativePlugin.CHANNEL));

EventBus.getDefault().register(this);

native2Dart();

}

@Subscribe(threadMode = ThreadMode.MAIN)

public void onEventMainThread(MyEvent myEvent) {

if (TextUtils.equals("openLogin", myEvent.getType())) {

mySink.success("haha");

}

}

private void native2Dart() {

EventChannel eventChannel = new EventChannel(getFlutterView(), CHANNEL);

EventChannel.StreamHandler streamHandler = new EventChannel.StreamHandler() {

// 这个onListen是Flutter端开始监听这个channel时的回调,第二个参数 EventSink是用来传数据的载体。

@Override

public void onListen(Object o, EventChannel.EventSink eventSink) {

MyLog.log("onListen被调用" + o.toString());

mySink = eventSink;

}

// 对面不再接收

@Override

public void onCancel(Object o) {

MyLog.log("onCancel被调用");

}

};

eventChannel.setStreamHandler(streamHandler);

}

@Override

protected void onDestroy() {

super.onDestroy();

EventBus.getDefault().unregister(this);

}

}

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/寸_铁/article/detail/810307
推荐阅读
相关标签
  

闽ICP备14008679号