安卓
package com.example.xcx_dashan_app; import io.flutter.embedding.android.FlutterActivity; import android.annotation.SuppressLint; import android.content.Intent; import android.os.Bundle; import io.flutter.embedding.engine.FlutterEngine; import androidx.annotation.NonNull; import io.flutter.plugin.common.MethodChannel; import io.flutter.plugins.GeneratedPluginRegistrant; import java.util.HashMap; import java.util.Map; // 导入Map类 import android.util.Log; public class MainActivity extends FlutterActivity { private static final String CHANNEL = "com.example.xcx_dashan_app/channel"; /** * Flutter 引擎配置器,用于配置 Flutter 引擎并设置通信通道 */ @Override public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine) { // 注册 Flutter 插件 GeneratedPluginRegistrant.registerWith(flutterEngine); // 创建一个新的方法通道,与 Dart 代码进行通信 new MethodChannel(flutterEngine.getDartExecutor().getBinaryMessenger(), CHANNEL) .setMethodCallHandler( (call, result) -> { // 处理方法调用 if (call.method.equals("launchApp")) { // 只调方法 launchApp(); result.success("App Launched"); } else if (call.method.equals("launchAppOne")) { // 传一个参数 String param1 = call.argument("param1"); launchApp1(param1); result.success("App Launched 1"); } else if (call.method.equals("launchAppTwo")) { // 传多个 String param1 = call.argument("param1"); String param2 = call.argument("param2"); launchApp2(param1, param2); result.success("App Launched 2"); } else if (call.method.equals("launchAppObject")) { // 传对象 launchApp3(call.arguments()); Map<String, Object> person = new HashMap<>(); // 返回一个对象 person.put("name", "John Doe"); person.put("age", 30); result.success(person); } else { // 如果收到其他方法调用,则返回未实现的错误给 Dart 代码 result.notImplemented(); } }); } @SuppressLint("LongLogTag") // 用于告诉编译器忽略指定代码段的特定警告或lint检查 private void launchApp() { System.out.println("--------------1访问成功--------------------"); Log.d(CHANNEL, "The value of number is:是否 "); System.out.println("--------------访问成功--------------------"); } private void launchApp1(String a) { System.out.println("--------访问成功----------一个参数----------" + a); } private void launchApp2(String a, String b) { System.out.println(b + "--------访问成功----------2个参数----------" + a); } private void launchApp3(Map<String, Object> arguments) { String name = (String) arguments.get("name"); int age = (int) arguments.get("age"); System.out.println(age + "---age--launchApp3---------1访问成功-------------------name-" + name); } }
flutter
class MyHomePage extends StatefulWidget { const MyHomePage({super.key}); @override State<MyHomePage> createState() => _MyHomePageState(); } class _MyHomePageState extends State<MyHomePage> { Future<void> launchAndroid() async { User user = new User("张三", 20); const platform = MethodChannel('com.example.xcx_dashan_app/channel'); try { var result = await platform.invokeMethod('launchAppObject', user.toJson()); // var result = await platform.invokeMethod('launchAppOne',{'param1': "这是一个参数"}); // var result = await platform.invokeMethod('launchAppOne',{'param1': "这是一个参数",'param2': "这是一个参数"}); print("结果$result"); } on PlatformException catch (e) { print("Failed to invoke method: '${e.message}'."); } } @override Widget build(BuildContext context) { return Container( child: Center( child: ElevatedButton( onPressed: () { launchAndroid(); }, child: const Text('Open Native App'), ), ), ); } }
标签:String,调安卓,call,result,param1,import,方法,flutter From: https://www.cnblogs.com/xbinbin/p/18157107