harmony_flutter_packageInfo(包基础信息获取)
HarmonyOS next之harmony_flutter_packageInfo(包基础信息获取)
一.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","");
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
· SQL Server 2025 AI相关能力初探