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);
}
}