当前位置: 首页 > article >正文

鸿蒙harmonyos next flutter通信之MethodChannel获取设备信息

        本文将通过MethodChannel获取设备信息,以此来演练MethodChannel用法。

  • 建立channel

        flutter代码:

MethodChannel methodChannel = MethodChannel("com.xmg.test");

        ohos代码:

private channel: MethodChannel | null = null

this.channel = new MethodChannel(flutterEngine.dartExecutor.getBinaryMessenger(), 'com.xmg.test')

this.channel.setMethodCallHandler({
      onMethodCall(call: MethodCall, result: MethodResult): void {
        switch (call.method) {
          case 'getDeviceInfo':
            result.success(deviceInfo.displayVersion);
            break;
          default:
            result.notImplemented
            break;
        }
      }
    });

注意:channelName必须一致。这里都叫com.xmg.test

  • flutter侧调用
    void _getDeviceInfo() async{
        deviceInfo = await methodChannel.invokeMethod('getDeviceInfo');
        setState(() {
        });
      }

  • 完整代码

        flutter完整代码:

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

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const MyHomePage(title: 'MethodChannel获取设备信息'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});
  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  MethodChannel methodChannel = MethodChannel("com.xmg.test");
  String deviceInfo = "";

  void _getDeviceInfo() async{
    deviceInfo = await methodChannel.invokeMethod('getDeviceInfo');
    setState(() {
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
              deviceInfo,
            ),
            ElevatedButton(onPressed: () {
              _getDeviceInfo();
            }, child: Text("获取设备版本号"))
          ],
        ),
      ),
    );
  }
}

        ohos完整代码:

import { FlutterAbility, FlutterEngine, MethodCall, MethodChannel, MethodResult } from '@ohos/flutter_ohos';
import { GeneratedPluginRegistrant } from '../plugins/GeneratedPluginRegistrant';
import deviceInfo from '@ohos.deviceInfo'

export default class EntryAbility extends FlutterAbility {
  private channel: MethodChannel | null = null

  configureFlutterEngine(flutterEngine: FlutterEngine) {
    super.configureFlutterEngine(flutterEngine)
    GeneratedPluginRegistrant.registerWith(flutterEngine)

    this.channel = new MethodChannel(flutterEngine.dartExecutor.getBinaryMessenger(), 'com.xmg.test')
    this.channel.setMethodCallHandler({
      onMethodCall(call: MethodCall, result: MethodResult): void {
        switch (call.method) {
          case 'getDeviceInfo':
            result.success(deviceInfo.displayVersion);
            break;
          default:
            result.notImplemented
            break;
        }
      }
    });
  }
}
  • 效果展示


http://www.kler.cn/a/325820.html

相关文章:

  • python画图|3D errorbars基础教程
  • TDSQL 免密码登录
  • 海量数据面试题
  • 汽车资讯新篇章:Spring Boot技术启航
  • 【Nginx从入门到精通】03 、安装部署-让虚拟机可以联网
  • 利用Python爬虫获取淘宝店铺详情
  • 【Python】Curdling:Python 包管理的高效工具
  • 基于STM32与OpenCV的物料搬运机械臂设计流程
  • 卷积神经网络 循环神经网络
  • 定时任务上云改造方案
  • 【MySQL】常见的SQL优化方式(二)
  • 设计模式 命令模式(Command Pattern)
  • MySQL 索引最左匹配原则详解
  • Element-plus安装及其基础组件使用
  • Python轴承故障诊断 (四)基于EMD-CNN的故障分类
  • 用Python集成免费IP归属地查询API
  • 语言的重定向
  • python select interpreter vscode 配置
  • 深度学习模型可视化工具 Netron 使用教程
  • 主流的消息队列
  • ACM第三次考核题解
  • Docker命令---查看所有创建的容器
  • [Linux]僵尸进程,孤儿进程,环境变量
  • 代码随想录算法训练营Day16
  • Threejs绘制圆锥体
  • 1.8 软件业务测试