HarmonyOS next之harmony_flutter_packageInfo(包基礎(chǔ)信息獲取)
一.MethodChannel
1.flutter端代碼
創(chuàng)建MethodChannel
接收ohos端傳遞過來的狀態(tài)值
? static const MethodChannel _channel = const MethodChannel('dev.fluttercommunity.plus/package_info');
//獲取所有參數(shù)
?? 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實(shí)現(xiàn)onAttachedToEngine方法
創(chuàng)建MethodChannel實(shí)例device_util
setMethodCallHandler
通過result回傳參數(shù)
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獲取相對應(yīng)得參數(shù)實(shí)現(xiàn)
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","");