harmony_flutter_packageInfo(包基礎(chǔ)信息獲取)

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","");

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容