Flutter入门系列8---与Android和IOS做相互通讯的channel工具

Flutter这边也可以嵌入到安卓和ios中去,混合开发,就像嵌入RN一样。我们native和web的沟通是通过jsbridge,那么现在flutter和native的通讯就是Platform-Channel啦

起航

夸平台通讯,自然涉及到不同语言在类型的转换问题

现在列下官方demo的代码:

import 'package:flutter/material.dart';
import 'dart:async';
import 'package:flutter/services.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      title: 'Flutter Demo',
      home: new ChannelNewHomePage()
    );
  }
}

class ChannelNewHomePage extends StatefulWidget {

  @override
  State<StatefulWidget> createState() {
    return new _MyHomePageState();
  }
}

class _MyHomePageState extends State<ChannelNewHomePage > {

    //保存下,后面会用到的
  static const platform = const MethodChannel('samples.flutter.io/battery');

  // Get battery level.
  String _batteryLevel = 'Unknown battery level.';

  Future<void> _getBatteryLevel() async {
    String batteryLevel;
    try {
      final int result = await platform.invokeMethod('getBatteryLevel');
      batteryLevel = 'Battery level at $result % .';
    } on PlatformException catch (e) {
      batteryLevel = "Failed to get battery level: '${e.message}'.";
    }

    setState(() {
      _batteryLevel = batteryLevel;
    });
  }

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
        body: new Center(
          child: Column(
            children: [
              RaisedButton(
                child: Text('Get Battery Level'),
                onPressed: _getBatteryLevel,
              ),
              Text(_batteryLevel),
            ],
          ),
        ));
  }
}

这里需要注意下这个
static const platform = const MethodChannel('samples.flutter.io/battery');
我们传入后面的这个string,是用来做唯一的区分的。

接着贴下ACtivity代码

public class MainActivity extends FlutterActivity {    

    private static final String CHANNEL = "samples.flutter.io/battery";    
    //注意这个和前面在flutter里面写的要一样

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

        //或者直接通过Flutter.createView获取flutterView
        new MethodChannel(this.getFlutterView(), CHANNEL).setMethodCallHandler(
                new MethodChannel.MethodCallHandler() {
                    @Override
                    public void onMethodCall(MethodCall call, MethodChannel.Result result) {
                        if (call.method.equals("getBatteryLevel")) {
                            int batteryLevel = getBatteryLevel();
                            if (batteryLevel != -1) {
                                result.success(batteryLevel);
                            } else {
                                result.error("UNAVAILABLE", "Battery level not available.", null);
                            }
                        } else {
                            result.notImplemented();
                        }
                    }
                });
    }    

    private int getBatteryLevel() {
        int batteryLevel = -1;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            BatteryManager batteryManager = (BatteryManager) getSystemService(BATTERY_SERVICE);
            batteryLevel = batteryManager.getIntProperty(BatteryManager.BATTERY_PROPERTY_CAPACITY);
        } else {
            Intent intent = new ContextWrapper(getApplicationContext()).
                    registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
            batteryLevel = (intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1) * 100) /
                    intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
        }
        return batteryLevel;
    }   
}

上面没太多需要细说的,看下接口都懂了

反向掉用

channel 是允许双向通信的,Android /iOS 和 flutter的角色互换一下。

在Android里面写

new MethodChannel(this.getFlutterView(), CHANNEL).invokeMethod("launch","1");

接着我们在flutter里面做对应的接口处理

platform.setMethodCallHandler((MethodCall call) async {
  assert(call.method == 'launch');        
    // call.arguments;        
});

ref

https://flutter.io/docs/development/platform-integration/platform-channels

热评文章