一.MethodChannel

1.flutter端代码

  • 创建MethodChannel
  • 接收ohos端传递过来的状态值
static const MethodChannel _channel = const MethodChannel('dev.fluttercommunity.plus/package_info');

//获取所有参数
    static Future<PackageInfo> fromPlatform() async {
    if (_fromPlatform != null) {
      return _fromPlatform!;
    }

    final platformData = await PackageInfoPlatform.instance.getAll();
    _fromPlatform = PackageInfo(
      appName: platformData.appName,
      packageName: platformData.packageName,
      version: platformData.version,
      buildNumber: platformData.buildNumber,
      buildSignature: platformData.buildSignature,
      installerStore: platformData.installerStore,
    );
    return _fromPlatform!;
  }

2.ohos端代码

  • 继承FlutterPlugin实现onAttachedToEngine方法
  • 创建MethodChannel实例device_util
  • setMethodCallHandler
  • 通过result回传参数
let bundleFlags =  bundleManager.BundleFlag.GET_BUNDLE_INFO_WITH_APPLICATION | bundleManager.BundleFlag.GET_BUNDLE_INFO_WITH_SIGNATURE_INFO

const TAG:string ="PackageInfoPlugin"

const CHANNEL_NAME = "dev.fluttercommunity.plus/package_info";
export class PackageInfoPlugin implements FlutterPlugin,MethodCallHandler{


  getUniqueClassName(): string {
    return "PackageInfoPlugin";
  }


  private methodChannel: MethodChannel | null = null;

  private applicationContext: Context | null = null;

  onAttachedToEngine(binding: FlutterPluginBinding): void {
    Log.d(TAG,'onAttachedToEngine packageInfo plugin')
    this.applicationContext =binding.getApplicationContext();
    this.methodChannel = new MethodChannel(binding.getBinaryMessenger(), CHANNEL_NAME);
    this.methodChannel.setMethodCallHandler(this);

  }

  onDetachedFromEngine(binding: FlutterPluginBinding): void {
    Log.d(TAG,'onDetachedFromEngine packageInfo plugin')
    this.applicationContext = null;
    this.methodChannel?.setMethodCallHandler(null);
    this.methodChannel = null;
  }

  onMethodCall(call: MethodCall, result: MethodResult): void {
    Log.d(TAG,'onMethodCall packageInfo plugin1')
    try {
             if (call.method == "getAll") {

               const bundleManage = bundleManager.getBundleInfoForSelfSync(bundleFlags);
               const appInfo = bundleManage.appInfo;

               const infoMap = new Map<string,string>();
               const buildSignature = bundleManage.signatureInfo.fingerprint;
               const appName = this.applicationContext?.resourceManager.getStringSync(appInfo.labelId) ?? '';
               infoMap.set("appName", appName);
               Log.d(TAG,'onMethodCall packageInfo plugin appName '+ appName);
               infoMap.set("packageName",bundleManage.name);
               Log.d(TAG,'onMethodCall packageInfo plugin packageName '+bundleManage.name)
               infoMap.set("version",bundleManage.versionName);
               Log.d(TAG,'onMethodCall packageInfo plugin version '+bundleManage.versionName)
               infoMap.set("buildNumber",bundleManage.versionCode.toString());
               Log.d(TAG,'onMethodCall packageInfo plugin buildNumber '+bundleManage.versionCode.toString())
               if (buildSignature != null){
                 infoMap.set("buildSignature",buildSignature);
                 Log.d(TAG,'onMethodCall packageInfo plugin buildSignature '+buildSignature)
               }
              infoMap.set("installerStore","");

               result.success(infoMap);
            } else {
               result.notImplemented()
             }
    } catch(err){
      result.error("Name not found", err.message, null)
    }
  }

通过@ohos.bundle.bundleManager获取相对应得参数实现

const infoMap = new Map<string,string>();
               const buildSignature = bundleManage.signatureInfo.fingerprint;
               const appName = this.applicationContext?.resourceManager.getStringSync(appInfo.labelId) ?? '';
               infoMap.set("appName", appName);
               Log.d(TAG,'onMethodCall packageInfo plugin appName '+ appName);
               infoMap.set("packageName",bundleManage.name);
               Log.d(TAG,'onMethodCall packageInfo plugin packageName '+bundleManage.name)
               infoMap.set("version",bundleManage.versionName);
               Log.d(TAG,'onMethodCall packageInfo plugin version '+bundleManage.versionName)
               infoMap.set("buildNumber",bundleManage.versionCode.toString());
               Log.d(TAG,'onMethodCall packageInfo plugin buildNumber '+bundleManage.versionCode.toString())
               if (buildSignature != null){
                 infoMap.set("buildSignature",buildSignature);
                 Log.d(TAG,'onMethodCall packageInfo plugin buildSignature '+buildSignature)
               }
              infoMap.set("installerStore","");