【问题标题】:How to call flutter module to android activity in flutter如何在flutter中调用flutter模块到android活动
【发布时间】:2021-03-30 11:23:52
【问题描述】:

我已经成功地将flutter 模块集成到我的原生安卓应用程序中。但我想调用颤振模块到android的模块第二个活动。

例如。 - Android 模块有两个活动,第一个是 MainActivty,另一个是第二个活动。当用户点击 MainActivity 的屏幕按钮 flutter 模块再次打开时,用户点击 Flutter 的模块浮动按钮,然后 SecondActivity 将被打开。

我怎样才能做到这一点,请帮助我。

这是 go to android 的颤振代码:

  InkWell(
            onTap: () {
              _showNativeView();
            },
            child:Container(),
          )

这是android原生类代码:

public class MainActivity extends FlutterActivity {

public static final String FLUTTER_TO_ANDROID = "flutterToandroid";


@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    GeneratedPluginRegistrant.registerWith(new FlutterEngine(this));

    // this is for open customer application with staging url
    new MethodChannel(getFlutterEngine().getDartExecutor().getBinaryMessenger(), FLUTTER_TO_ANDROID).setMethodCallHandler(new MethodChannel.MethodCallHandler() {
        @Override
        public void onMethodCall(@NonNull MethodCall methodCall, @NonNull MethodChannel.Result result) {

            if (methodCall.method.equals("showNativeView")) {
                String messagetoflutter = showNativeView();
                result.success(messagetoflutter);
            } else {
                result.notImplemented();
            }

        }
    });
}

String showNativeView() {

    try {



       Intent i = newIntent(MainActivity.this,SecondActivity.class);
        startActivity(i);

        Toast.makeText(this, "Done, you are Correct",Toast.LENGTH_SHORT).show();

    } catch (Exception e) {
        Toast.makeText(this, "Application not found on this device", Toast.LENGTH_SHORT).show();
    }
    return "okay";
}

}

当调用 showNativeView 方法时,它会转到 SecondActivity。

【问题讨论】:

  • 请在此处发布您的相关代码
  • @Quicklearner 我有现有的安卓应用,我想使用颤振打开现有应用的活动。

标签: java android flutter


【解决方案1】:

首先你应该创建一个 FlutterMethodChannel 然后 平台Channel调用方法

String response = "";

try {
    final String result = await platform.invokeMethod('helloFromNativeCode');
   
 response = result;

  } on PlatformException catch (e) {

    response = "Failed to Invoke: '${e.message}'.";
  }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-05-12
    • 2022-07-23
    • 2023-01-17
    • 1970-01-01
    • 1970-01-01
    • 2019-10-26
    • 2019-06-19
    相关资源
    最近更新 更多