您的位置:首页 > 房产 > 建筑 > 郑州seo顾问热狗hotdoger_防控措施持续优化_阳山网站seo_太原整站优化排名外包

郑州seo顾问热狗hotdoger_防控措施持续优化_阳山网站seo_太原整站优化排名外包

2024/12/23 10:58:26 来源:https://blog.csdn.net/majun2009/article/details/142629752  浏览:    关键词:郑州seo顾问热狗hotdoger_防控措施持续优化_阳山网站seo_太原整站优化排名外包
郑州seo顾问热狗hotdoger_防控措施持续优化_阳山网站seo_太原整站优化排名外包

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

  • 建立channel

        flutter代码:

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

        ohos代码:

private channel: MethodChannel | null = nullthis.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.notImplementedbreak;}}});

注意: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});@overrideWidget 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;@overrideState<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(() {});}@overrideWidget 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 = nullconfigureFlutterEngine(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.notImplementedbreak;}}});}
}
  • 效果展示

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com