android端:
package com.example.ingeek_deviceInfo;
import androidx.annotation.NonNull;
import io.flutter.embedding.engine.plugins.FlutterPlugin;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugin.common.MethodChannel.Result;
import io.flutter.plugin.common.PluginRegistry.Registrar;
/** IngeekDeviceInfoPlugin */
public class IngeekDeviceInfoPlugin implements FlutterPlugin, MethodCallHandler {
/// The MethodChannel that will the communication between Flutter and native Android
///
/// This local reference serves to register the plugin with the Flutter Engine and unregister it
/// when the Flutter Engine is detached from the Activity
private MethodChannel channel;
private FlutterPluginBinding flutterPluginBinding;
@Override
public void onAttachedToEngine(@NonNull FlutterPluginBinding flutterPluginBinding) {
channel = new MethodChannel(flutterPluginBinding.getBinaryMessenger(), "ingeek_deviceInfo");
channel.setMethodCallHandler(this);
this.flutterPluginBinding = flutterPluginBinding;
}
@Override
public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
if (call.method.equals("getPlatformVersion")) {
result.success("Android " + android.os.Build.VERSION.RELEASE);
} else if (call.method.equals("getDeviceId")){
result.success("getDeviceId");
}
}
@Override
public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {
channel.setMethodCallHandler(null);
}
}
IOS端:
import Flutter
import UIKit
public class SwiftIngeekDeviceInfoPlugin: NSObject, FlutterPlugin {
public static func register(with registrar: FlutterPluginRegistrar) {
let channel = FlutterMethodChannel(name: "ingeek_deviceInfo", binaryMessenger: registrar.messenger())
let instance = SwiftIngeekDeviceInfoPlugin()
registrar.addMethodCallDelegate(instance, channel: channel)
}
public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
if("getPlatformVersion" == call.method){
result("iOS " + UIDevice.current.systemVersion)
}else if ("getDeviceId" == call.method){
result(UIDevice.current.identifierForVendor)
}
}
}
Flutter端:
import 'dart:async';
import 'package:flutter/services.dart';
class IngeekDeviceInfo {
static const MethodChannel _channel =
const MethodChannel('ingeek_deviceInfo');
static Future<String> get platformVersion async {
final String version = await _channel.invokeMethod('getPlatformVersion');
return version;
}
static Future<String> get getDeviceId async {
final String deviceId = await _channel.invokeMethod('getDeviceId');
return deviceId;
}
}
网友评论