487570509 和 219980721 总是更新失败,只有 1255272278 成功private globalListenerSetup: boolean = false;
private initializeServices(): void {
if (!this.widgetDataManager) {
this.widgetDataManager = new WidgetDataManager();
hilog.info(0x0000, TAG, 'WidgetDataManager initialized');
}
// 设置全局状态监听器(只设置一次)
if (!this.globalListenerSetup) {
this.setupGlobalStateListener();
this.globalListenerSetup = true;
}
}
private setupGlobalStateListener(): void {
try {
const avSessionListener = AvSessionWidgetListener.getInstance();
avSessionListener.addStateListener((data: WidgetData) => {
hilog.info(0x0000, TAG, `Heanup widget global listener received state update: isPlaying=${data.playState.isPlaying}, title=${data.currentSong.title}`);
// 更新所有活跃的卡片
this.updateAllWidgetsWithData(data);
});
hilog.info(0x0000, TAG, 'Heanup widget global state listener setup successfully');
} catch (error) {
hilog.error(0x0000, TAG, `Heanup widget failed to setup global listener: ${error}`);
}
}
private updateAllWidgetsWithData(data: WidgetData): void {
this.formSizeMap.forEach((size: WidgetSize, formId: string) => {
try {
// 根据卡片尺寸适配数据
const adaptedData = this.layoutManager.adaptDataForSize(data, size);
hilog.info(0x0000, TAG, `Heanup widget ${formId} calling formProvider.updateForm with isPlaying=${adaptedData.isPlaying}`);
// 直接使用系统API更新卡片
const formData = formBindingData.createFormBindingData(adaptedData);
formProvider.updateForm(formId, formData).then(() => {
hilog.info(0x0000, TAG, `Heanup widget ${formId} updated successfully: isPlaying=${adaptedData.isPlaying}, title=${adaptedData.songTitle}`);
}).catch((error) => {
hilog.error(0x0000, TAG, `Heanup widget ${formId} update failed: ${error}`);
});
} catch (error) {
hilog.error(0x0000, TAG, `Heanup widget ${formId} process error: ${error}`);
}
});
}
updateAllWidgetsData() 调用替换为 updateAllWidgetsWithData(currentState)updateAllWidgetsData 方法487570509 和 219980721 持续失败修复已完成,可以进行测试验证。