HarmonyOS 5 通知与语音能力开发实战:从消息推送到智能语音交互

news/2025/9/24 17:43:29/文章来源:https://www.cnblogs.com/xpzll/p/19109659

一、通知系统开发全面解析

1. 通知权限与基础配置

module.json5中声明通知和语音相关权限:

{"module": {"requestPermissions": [{"name": "ohos.permission.NOTIFICATION_CONTROLLER","reason": "$string:notification_permission_reason","usedScene": {"abilities": ["MainAbility"],"when": "always"}},{"name": "ohos.permission.MICROPHONE","reason": "$string:microphone_permission_reason","usedScene": {"abilities": ["MainAbility"],"when": "always"}},{"name": "ohos.permission.VIBRATE","reason": "$string:vibrate_permission_reason","usedScene": {"abilities": ["MainAbility"],"when": "always"}}]}
}

2. 通知通道(NotificationSlot)创建

创建不同类型的通知通道以适应不同场景:

import notificationManager from '@ohos.notificationManager';
import { BusinessError } from '@ohos.base';class NotificationService {// 初始化通知通道async initializeNotificationSlots(): Promise<void> {try {// 1. 紧急警报通道(高优先级)const emergencySlot: notificationManager.NotificationSlot = {id: 'emergency_slot',name: '紧急警报',type: notificationManager.SlotType.ALARM,level: notificationManager.SlotLevel.LEVEL_HIGH,vibrationEnabled: true,vibrationValues: [1000, 1000, 1000], // 震动模式soundEnabled: true,sound: 'system://alarm_alert.mp3', // 系统警报音bypassDnd: true, // 绕过免打扰模式description: '重要安全警报和紧急通知'};await notificationManager.addSlot(emergencySlot);// 2. 消息通知通道(中等优先级)const messageSlot: notificationManager.NotificationSlot = {id: 'message_slot',name: '消息通知',type: notificationManager.SlotType.SOCIAL_COMMUNICATION,level: notificationManager.SlotLevel.LEVEL_DEFAULT,vibrationEnabled: true,vibrationValues: [500, 500], // 短震动soundEnabled: true,sound: 'system://message_alert.mp3',bypassDnd: false,description: '聊天消息和社交通知'};await notificationManager.addSlot(messageSlot);// 3. 普通通知通道(低优先级)const normalSlot: notificationManager.NotificationSlot = {id: 'normal_slot',name: '普通通知',type: notificationManager.SlotType.OTHER_TYPES,level: notificationManager.SlotLevel.LEVEL_LOW,vibrationEnabled: false,soundEnabled: false,bypassDnd: false,description: '一般应用通知和状态更新'};await notificationManager.addSlot(normalSlot);console.info('所有通知通道初始化成功');} catch (error) {console.error(`通知通道初始化失败: ${(error as BusinessError).message}`);}}
}

3. 发送不同类型通知

实现多种场景的通知发送功能:

class NotificationSender {private notificationId: number = 0;// 发送基本文本通知async sendBasicNotification(title: string, content: string, slotId: string = 'normal_slot'): Promise<boolean> {try {const notificationRequest: notificationManager.NotificationRequest = {id: this.notificationId++,slotId: slotId,content: {contentType: notificationManager.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT,normal: {title: title,text: content,additionalText: '来自我的应用'}},deliveryTime: Date.now(), // 立即发送autoDeletedTime: Date.now() + 3600000, // 1小时后自动删除tapDismissed: true // 点击后消失};await notificationManager.publish(notificationRequest);console.info(`通知发送成功: ${title}`);return true;} catch (error) {console.error(`通知发送失败: ${(error as BusinessError).message}`);return false;}}// 发送带动作按钮的通知async sendActionableNotification(title: string,content: string,actions: notificationManager.NotificationActionButton[]): Promise<boolean> {try {const notificationRequest: notificationManager.NotificationRequest = {id: this.notificationId++,slotId: 'message_slot',content: {contentType: notificationManager.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT,normal: {title: title,text: content}},actionButtons: actions,// 设置点击意图(跳转到特定页面)intent: {bundleName: 'com.example.myapp',abilityName: 'DetailAbility',parameters: { notificationId: this.notificationId.toString() }}};await notificationManager.publish(notificationRequest);return true;} catch (error) {console.error(`可操作通知发送失败: ${(error as BusinessError).message}`);return false;}}// 发送进度通知(用于下载、上传等场景)async sendProgressNotification(title: string,progress: number,maxProgress: number = 100): Promise<void> {try {const notificationRequest: notificationManager.NotificationRequest = {id: 1001, // 固定ID用于更新同一通知slotId: 'normal_slot',content: {contentType: notificationManager.ContentType.NOTIFICATION_CONTENT_PROGRESS,progress: {title: title,progressValue: progress,progressMaxValue: maxProgress,statusText: `${progress}% 完成`}},isOngoing: true // 进行中通知,不会被轻易清除};await notificationManager.publish(notificationRequest);// 完成后更新为完成状态if (progress >= maxProgress) {setTimeout(async () => {await this.sendBasicNotification(title, '任务已完成', 'normal_slot');await notificationManager.cancel(1001);}, 2000);}} catch (error) {console.error(`进度通知发送失败: ${(error as BusinessError).message}`);}}
}

4. 通知管理与交互处理

实现通知的生命周期管理和用户交互处理:

@Component
struct NotificationHandler {@State activeNotifications: notificationManager.NotificationRequest[] = [];private notificationListener: notificationManager.NotificationSubscriber | null = null;aboutToAppear() {this.setupNotificationListener();}// 设置通知监听器private setupNotificationListener(): void {try {this.notificationListener = {onNotificationPublished: (notification: notificationManager.NotificationRequest) => {console.info('通知已发布:', notification.id);this.updateActiveNotifications();},onNotificationCancelled: (id: number, slotId: string, reason: number) => {console.info(`通知已取消: ${id}, 原因: ${reason}`);this.updateActiveNotifications();},onNotificationUpdated: (notification: notificationManager.NotificationRequest) => {console.info('通知已更新:', notification.id);this.updateActiveNotifications();}};notificationManager.subscribe(this.notificationListener);} catch (error) {console.error(`通知监听器设置失败: ${(error as BusinessError).message}`);}}// 更新活动通知列表private async updateActiveNotifications(): Promise<void> {try {this.activeNotifications = await notificationManager.getActiveNotifications();} catch (error) {console.error(`获取活动通知失败: ${(error as BusinessError).message}`);}}// 处理通知点击事件handleNotificationClick(notificationId: number): void {console.info(`通知被点击: ${notificationId}`);// 根据通知ID执行相应操作switch (notificationId) {case 1001:router.push({ url: 'pages/DownloadPage' });break;case 1002:router.push({ url: 'pages/MessageDetail' });break;default:router.push({ url: 'pages/NotificationCenter' });}}aboutToDisappear() {if (this.notificationListener) {notificationManager.unsubscribe(this.notificationListener);}}build() {// 通知处理UI组件}
}

二、语音识别(ASR)开发实战

1. 语音识别服务初始化

配置和初始化语音识别功能:

import speechRecognizer from '@ohos.speechRecognizer';
import { BusinessError } from '@ohos.base';class SpeechRecognitionService {private recognizer: speechRecognizer.SpeechRecognizer | null = null;@State isListening: boolean = false;@State recognitionResult: string = '';@State confidence: number = 0;// 初始化语音识别器async initializeRecognizer(): Promise<boolean> {try {this.recognizer = speechRecognizer.createSpeechRecognizer();// 设置识别参数const config: speechRecognizer.RecognizerConfig = {language: 'zh-CN', // 中文普通话country: 'CN',punctuation: true, // 启用标点符号mode: speechRecognizer.RecognizeMode.FREE_FORM, // 自由格式识别audioSource: speechRecognizer.AudioSource.MICROPHONE // 麦克风输入};await this.recognizer.setConfig(config);// 设置识别结果回调this.recognizer.on('result', (result: speechRecognizer.RecognizerResult) => {this.recognitionResult = result.text;this.confidence = result.confidence;console.info(`识别结果: ${result.text}, 置信度: ${result.confidence}`);});// 设置错误回调this.recognizer.on('error', (error: BusinessError) => {console.error(`语音识别错误: ${error.message}`);this.isListening = false;});console.info('语音识别器初始化成功');return true;} catch (error) {console.error(`语音识别器初始化失败: ${(error as BusinessError).message}`);return false;}}// 开始语音识别async startRecognition(): Promise<void> {if (!this.recognizer) {await this.initializeRecognizer();}try {await this.recognizer.start();this.isListening = true;console.info('语音识别开始');} catch (error) {console.error(`启动语音识别失败: ${(error as BusinessError).message}`);}}// 停止语音识别async stopRecognition(): Promise<void> {if (this.recognizer && this.isListening) {try {await this.recognizer.stop();this.isListening = false;console.info('语音识别停止');} catch (error) {console.error(`停止语音识别失败: ${(error as BusinessError).message}`);}}}// 销毁识别器async destroyRecognizer(): Promise<void> {if (this.recognizer) {await this.stopRecognition();this.recognizer.destroy();this.recognizer = null;}}
}

2. 实时语音识别组件

创建支持实时语音识别的UI组件:

@Component
struct VoiceRecognitionComponent {private speechService: SpeechRecognitionService = new SpeechRecognitionService();@State recognizedText: string = '点击麦克风开始说话...';@State isRecording: boolean = false;@State confidenceLevel: number = 0;aboutToAppear() {this.speechService.initializeRecognizer();}// 切换录音状态async toggleRecording(): Promise<void> {if (this.isRecording) {await this.speechService.stopRecognition();this.isRecording = false;} else {await this.speechService.startRecognition();this.isRecording = true;// 监听识别结果this.speechService.onResult((result: string, confidence: number) => {this.recognizedText = result;this.confidenceLevel = confidence;});}}build() {Column() {// 语音波形动画if (this.isRecording) {VoiceWaveAnimation().height(80).margin({ bottom: 20 })}// 识别结果展示Text(this.recognizedText).fontSize(18).textAlign(TextAlign.Center).margin({ bottom: 16 }).minHeight(100).width('90%')// 置信度指示器if (this.confidenceLevel > 0) {Text(`置信度: ${(this.confidenceLevel * 100).toFixed(1)}%`).fontSize(14).fontColor('#666').margin({ bottom: 20 })}// 录音按钮Button(this.isRecording ? '停止录音' : '开始语音识别').width(200).height(60).backgroundColor(this.isRecording ? '#FF5252' : '#2196F3').onClick(() => {this.toggleRecording();}).margin({ bottom: 30 })// 语音指令示例if (!this.isRecording) {Column() {Text('试试说:').fontSize(16).fontWeight(FontWeight.Bold).margin({ bottom: 8 })Text('• "打开设置"').fontSize(14).opacity(0.7)Text('• "发送消息给张三"').fontSize(14).opacity(0.7)Text('• "今天天气怎么样"').fontSize(14).opacity(0.7)}.padding(16).backgroundColor('#F5F5F5').borderRadius(12)}}.width('100%').height('100%').padding(20).alignItems(HorizontalAlign.Center)}
}

三、语音合成(TTS)开发实战

1. 语音合成服务配置

实现文本到语音的转换功能:

import textToSpeech from '@ohos.textToSpeech';
import { BusinessError } from '@ohos.base';class TextToSpeechService {private ttsEngine: textToSpeech.TtsEngine | null = null;@State isSpeaking: boolean = false;@State availableVoices: textToSpeech.VoiceInfo[] = [];@State currentVoice: string = '';// 初始化TTS引擎async initializeTts(): Promise<boolean> {try {this.ttsEngine = textToSpeech.createTtsEngine();// 获取可用语音列表this.availableVoices = await this.ttsEngine.getVoices();this.currentVoice = this.availableVoices[0]?.voiceId || '';// 设置TTS参数const ttsConfig: textToSpeech.TtsConfig = {voiceId: this.currentVoice, // 默认语音speed: 1.0, // 语速 (0.5-2.0)pitch: 1.0, // 音调 (0.5-2.0)volume: 0.8, // 音量 (0.0-1.0)audioStreamType: textToSpeech.AudioStreamType.STREAM_MUSIC};await this.ttsEngine.setConfig(ttsConfig);console.info('TTS引擎初始化成功');return true;} catch (error) {console.error(`TTS引擎初始化失败: ${(error as BusinessError).message}`);return false;}}// 播放语音async speak(text: string, config?: textToSpeech.TtsConfig): Promise<void> {if (!this.ttsEngine) {await this.initializeTts();}try {if (config) {await this.ttsEngine.setConfig(config);}this.isSpeaking = true;await this.ttsEngine.speak(text);// 监听播放完成this.ttsEngine.on('finish', () => {this.isSpeaking = false;console.info('语音播放完成');});// 监听错误事件this.ttsEngine.on('error', (error: BusinessError) => {this.isSpeaking = false;console.error(`语音播放错误: ${error.message}`);});} catch (error) {console.error(`语音播放失败: ${(error as BusinessError).message}`);}}// 停止播放async stop(): Promise<void> {if (this.ttsEngine && this.isSpeaking) {try {await this.ttsEngine.stop();this.isSpeaking = false;} catch (error) {console.error(`停止语音播放失败: ${(error as BusinessError).message}`);}}}// 暂停播放async pause(): Promise<void> {if (this.ttsEngine && this.isSpeaking) {try {await this.ttsEngine.pause();this.isSpeaking = false;} catch (error) {console.error(`暂停语音播放失败: ${(error as BusinessError).message}`);}}}// 恢复播放async resume(): Promise<void> {if (this.ttsEngine && !this.isSpeaking) {try {await this.ttsEngine.resume();this.isSpeaking = true;} catch (error) {console.error(`恢复语音播放失败: ${(error as BusinessError).message}`);}}}
}

2. 语音朗读组件

创建支持多种语音设置的朗读组件:

@Component
struct TextToSpeechComponent {private ttsService: TextToSpeechService = new TextToSpeechService();@State textToRead: string = '';@State isPlaying: boolean = false;@State speechSpeed: number = 1.0;@State speechPitch: number = 1.0;@State selectedVoice: string = '';aboutToAppear() {this.ttsService.initializeTts();}// 朗读文本async readText(): Promise<void> {if (this.textToRead.trim() === '') {prompt.showToast({ message: '请输入要朗读的文本' });return;}const config: textToSpeech.TtsConfig = {voiceId: this.selectedVoice || undefined,speed: this.speechSpeed,pitch: this.speechPitch,volume: 0.8};if (this.isPlaying) {await this.ttsService.stop();this.isPlaying = false;} else {await this.ttsService.speak(this.textToRead, config);this.isPlaying = true;}}build() {Column() {// 文本输入区域TextArea({ text: this.textToRead, placeholder: '输入要朗读的文本...' }).height(150).width('90%').margin({ bottom: 20 }).onChange((value: string) => {this.textToRead = value;})// 语音设置Column() {Text('语音设置').fontSize(16).fontWeight(FontWeight.Bold).margin({ bottom: 12 })// 语速调节Row() {Text('语速:').width(80)Slider({ value: this.speechSpeed, min: 0.5, max: 2.0, step: 0.1 }).layoutWeight(1).onChange((value: number) => {this.speechSpeed = value;})Text(this.speechSpeed.toFixed(1)).width(40)}.margin({ bottom: 12 })// 音调调节Row() {Text('音调:').width(80)Slider({ value: this.speechPitch, min: 0.5, max: 2.0, step: 0.1 }).layoutWeight(1).onChange((value: number) => {this.speechPitch = value;})Text(this.speechPitch.toFixed(1)).width(40)}.margin({ bottom: 12 })}.width('90%').padding(16).backgroundColor('#F8F9FA').borderRadius(12).margin({ bottom: 20 })// 控制按钮Button(this.isPlaying ? '停止朗读' : '开始朗读').width(200).height(50).backgroundColor(this.isPlaying ? '#FF5252' : '#4CAF50').onClick(() => {this.readText();}).margin({ bottom: 20 })// 状态指示if (this.isPlaying) {LoadingIndicator().size({ width: 30, height: 30 }).margin({ bottom: 10 })Text('朗读中...').fontSize(14).fontColor('#666')}}.width('100%').height('100%').padding(20).alignItems(HorizontalAlign.Center)}
}

四、通知与语音的协同应用

1. 语音播报通知内容

实现收到通知后自动语音播报的功能:

class NotificationVoiceReader {private ttsService: TextToSpeechService;private notificationService: NotificationService;constructor() {this.ttsService = new TextToSpeechService();this.notificationService = new NotificationService();this.setupNotificationListener();}// 设置通知监听器private setupNotificationListener(): void {notificationManager.subscribe({onNotificationPublished: async (notification: notificationManager.NotificationRequest) => {// 只播报高优先级通知if (notification.slotId === 'emergency_slot' || notification.slotId === 'message_slot') {const title = notification.content.normal?.title || '';const text = notification.content.normal?.text || '';await this.readNotification(title, text);}}});}// 朗读通知内容private async readNotification(title: string, content: string): Promise<void> {const fullText = `收到新通知: ${title}. ${content}`;await this.ttsService.speak(fullText, {speed: 1.0,pitch: 1.1, // 稍高音调以提高可听性volume: 0.9});}// 语音控制通知管理async handleVoiceCommand(command: string): Promise<void> {const lowerCommand = command.toLowerCase();if (lowerCommand.includes('阅读通知')) {await this.readAllNotifications();} else if (lowerCommand.includes('清除通知')) {await notificationManager.cancelAll();await this.ttsService.speak('已清除所有通知');} else if (lowerCommand.includes('打开通知中心')) {router.push({ url: 'pages/NotificationCenter' });}}// 朗读所有未读通知private async readAllNotifications(): Promise<void> {try {const notifications = await notificationManager.getActiveNotifications();if (notifications.length === 0) {await this.ttsService.speak('没有未读通知');return;}await this.ttsService.speak(`您有${notifications.length}条未读通知`);for (const notification of notifications) {const title = notification.content.normal?.title || '';const text = notification.content.normal?.text || '';await this.readNotification(title, text);await new Promise(resolve => setTimeout(resolve, 1000)); // 暂停1秒}} catch (error) {console.error(`朗读通知失败: ${(error as BusinessError).message}`);}}
}

2. 语音助手集成示例

创建完整的语音助手组件,集成通知和语音能力:

@Component
struct VoiceAssistant {private speechRecognition: SpeechRecognitionService = new SpeechRecognitionService();private ttsService: TextToSpeechService = new TextToSpeechService();private notificationReader: NotificationVoiceReader = new NotificationVoiceReader();@State isAssistantActive: boolean = false;@State commandHistory: string[] = [];aboutToAppear() {this.initializeServices();}// 初始化服务async initializeServices(): Promise<void> {await this.speechRecognition.initializeRecognizer();await this.ttsService.initializeTts();// 设置语音识别回调this.speechRecognition.onResult((result: string, confidence: number) => {if (confidence > 0.7) { // 置信度阈值this.processVoiceCommand(result);this.commandHistory = [result, ...this.commandHistory.slice(0, 9)]; // 保留最近10条}});}// 处理语音指令private async processVoiceCommand(command: string): Promise<void> {console.info(`处理语音指令: ${command}`);if (command.includes('通知') || command.includes('消息')) {await this.notificationReader.handleVoiceCommand(command);} else if (command.includes('时间')) {const now = new Date();const timeText = `现在时间是${now.getHours()}点${now.getMinutes()}分`;await this.ttsService.speak(timeText);} else if (command.includes('天气')) {await this.ttsService.speak('正在查询天气信息...');// 这里可以集成天气API} else {await this.ttsService.speak(`已执行命令: ${command}`);}}// 切换助手状态async toggleAssistant(): Promise<void> {if (this.isAssistantActive) {await this.speechRecognition.stopRecognition();await this.ttsService.stop();this.isAssistantActive = false;} else {await this.speechRecognition.startRecognition();await this.ttsService.speak('语音助手已启动,请说出您的指令');this.isAssistantActive = true;}}build() {Column() {// 助手状态显示Text(this.isAssistantActive ? '语音助手运行中...' : '点击启动语音助手').fontSize(18).fontWeight(FontWeight.Bold).margin({ bottom: 20 })// 主控制按钮Button(this.isAssistantActive ? '停止助手' : '启动语音助手').width(250).height(60).backgroundColor(this.isAssistantActive ? '#FF5252' : '#2196F3').onClick(() => {this.toggleAssistant();}).margin({ bottom: 30 })// 指令历史if (this.commandHistory.length > 0) {Column() {Text('最近指令').fontSize(16).fontWeight(FontWeight.Bold).margin({ bottom: 12 })ForEach(this.commandHistory, (command: string) => {Text(`• ${command}`).fontSize(14).textAlign(TextAlign.Start).width('90%').margin({ bottom: 8 })})}.width('90%').padding(16).backgroundColor('#F8F9FA').borderRadius(12)}// 快速指令示例Column() {Text('试试说:').fontSize(16).fontWeight(FontWeight.Bold).margin({ bottom: 8 })Text('• "阅读我的通知"').fontSize(14).opacity(0.7)Text('• "现在几点了"').fontSize(14).opacity(0.7)Text('• "清除所有通知"').fontSize(14).opacity(0.7)}.margin({ top: 20 })}.width('100%').height('100%').padding(20).alignItems(HorizontalAlign.Center)}
}

⚡ 五、性能优化与最佳实践

1. 资源管理与性能优化

语音服务资源管理

class ResourceOptimizer {private static activeTtsInstances: number = 0;private static readonly MAX_TTS_INSTANCES: number = 3;// 智能TTS实例管理static async getTtsInstance(): Promise<textToSpeech.TtsEngine> {if (this.activeTtsInstances >= this.MAX_TTS_INSTANCES) {throw new Error('达到TTS实例数量限制');}const tts = textToSpeech.createTtsEngine();this.activeTtsInstances++;// 设置销毁钩子tts.on('destroy', () => {this.activeTtsInstances--;});return tts;}// 语音识别资源优化static optimizeRecognitionConfig(): speechRecognizer.RecognizerConfig {return {language: 'zh-CN',mode: speechRecognizer.RecognizeMode.FREE_FORM,audioSource: speechRecognizer.AudioSource.MICROPHONE,bufferSize: 8192, // 优化缓冲区大小sampleRate: 16000, // 适当采样率encoding: speechRecognizer.AudioEncoding.ENCODING_PCM_16BIT};}
}

2. 电量与网络优化

后台服务优化策略

class PowerOptimization {// 低电量模式检测static isLowPowerMode(): boolean {const batteryLevel = deviceInfo.getBatteryLevel();return batteryLevel < 20;}// 自适应语音质量static getAdaptiveTtsConfig(): textToSpeech.TtsConfig {const isLowPower = this.isLowPowerMode();return {speed: isLowPower ? 1.2 : 1.0, // 低电量时稍快语速pitch: 1.0,volume: 0.8,audioStreamType: isLowPower ? textToSpeech.AudioStreamType.STREAM_VOICE_CALL : // 低功耗模式textToSpeech.AudioStreamType.STREAM_MUSIC // 正常模式};}// 网络状态感知的通知发送static async sendNetworkAwareNotification(notification: notificationManager.NotificationRequest): Promise<void> {const networkType = network.getNetworkType();// 在移动网络下优化通知内容if (networkType === network.NetworkType.NETWORK_MOBILE) {// 简化通知内容,减少数据使用if (notification.content.normal) {notification.content.normal.text = notification.content.normal.text.substring(0, 100) + '...';}}await notificationManager.publish(notification);}
}

通过掌握这些通知和语音开发技术,你可以在HarmonyOS应用中创建智能、高效的人机交互体验,从基本的消息推送到复杂的语音交互,全面提升应用的用户体验。

需要参加鸿蒙认证的请点击 鸿蒙认证链接

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/916037.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Keithley 万用表里测电阻分成两种模式

Keithley 万用表里测电阻分成两种模式前面板操作(以常见型号为例)开机,确认表正常自检通过。在 功能选择区(Function/Measure 按键区):找到 Ω(Ohms / RES) 按键,按下后进入电阻测量模式。如果你的型号没有单…

HarmonyOS 5 Native与ArkTS混合开发实战:跨语言高性能组件开发

一、混合开发架构与原理 1. 混合开发核心架构 HarmonyOS混合开发采用分层设计:ArkTS层:UI组件和业务逻辑,使用声明式开发范式 Node-API桥接层:提供类型安全的跨语言通信机制 Native层:C/C++高性能计算和系统级功能…

实战:基于HarmonyOS 5构建分布式聊天通讯应用

1 HarmonyOS 5分布式通信基础 HarmonyOS 5为聊天通讯应用开发带来了革命性的分布式能力,让开发者能够构建真正跨设备、无缝协同的通信体验。本节将介绍如何利用HarmonyOS 5的分布式架构和API 12+特性来打造高性能聊天…

Java-Eclipse使用-多维数组的使用

Java-Eclipse使用-多维数组的使用Day08 1.多维数组的打印方式 2.多维数组的实操使用 3.多维数组的实操使用 4.多维数组的实操使用 明天会继续进行更新,二维数组的打印不太理解。

中国建设银行官方网站 认证网站运营和seo的区别

大家想必都知道&#xff0c;数组和链表的搜索操作的时间复杂度都是O(N)的&#xff0c;在数据量大的时候是非常耗时的。对于数组来说&#xff0c;我们可以先排序&#xff0c;然后使用二分搜索&#xff0c;就能够将时间复杂度降低到O(logN)&#xff0c;但是有序数组的插入是一个O…

HarmonyOS 5 动画开发实战:从基础动效到高级交互动画

🎯 一、HarmonyOS动画系统概述 HarmonyOS提供了强大而灵活的动画系统,支持多种动画类型和交互效果:动画类型 适用场景 核心API属性动画 组件尺寸、位置、透明度等属性变化 animateTo(), Animation()转场动画 组件出…

HarmonyOS 5 高级动效实战:粒子系统、路径动画与物理动效开发

一、粒子系统动画开发 粒子动画通过大量微小元素的运动创造复杂视觉效果,如雨雪、火焰、爆炸等自然现象。 1. 基础粒子系统实现 import particle from @ohos.graphics.particle; import { BusinessError } from @ohos…

从范德蒙德矩阵聊开去.

范德蒙德矩阵 : \(\bm{V}=\left[ \begin{array}{ccc}1 & x_0 & x_0^2 & \cdots & x_0^{n-1} \\1 & x_1 & x_1^2 & \cdots & x_1^{n-1} \\1 & x_2 & x_2^2 & \cdots &…

全新 CloudPilot AI:嵌入 Kubernetes 的 SRE Agent,降本与韧性双提升!

全新 CloudPilot AI:嵌入 Kubernetes 的 SRE Agent,降本与韧性双提升!在生成式 AI 的浪潮下,计算资源,尤其是支撑所有服务运行与调度的 CPU 资源,已经从单纯的成本项目,升级为驱动创新速度与竞争力的底层基石。…

HarmonyOS 5 动画性能优化深度解析:从原理到实践

一、HarmonyOS动画系统架构与渲染原理 1. 动画系统核心架构 HarmonyOS的动画系统采用分层设计,包含三个关键层级:UI组件层:基于ArkUI的声明式动画API(如animateTo) 动画引擎层:负责插值计算和时间轴管理 渲染管线…

容桂网站制作公司系统类小说

类ReentrantLock具有完全互斥排他的效果&#xff0c;即同一时间只有一个线程在执行ReentrantLock.lock()后面的代码。这样虽然保证了线程的安全性&#xff0c;但是效率低下。JDK提供了ReentrantReadWriteLock读写锁&#xff0c;使用它可以加快效率&#xff0c;在某些不需要操作…

vue3 + antd +ts cron 选择器使用

https://github.com/shiyzhang/shiyzhang-cron shiyzhang-cron组件 使用方法:npm i shiyzhangcron 或 pnpm i shiyzhangcron 给ts添加类型声明文件在项目根目录下创建 types 文件夹 在 types 文件夹中创建 shiyzhangc…

HarmonyOS 5 性能优化全攻略:从启动加速到内存管理

🚀 一、启动速度优化 应用启动是用户体验的第一印象,优化启动速度至关重要。 1. 冷启动时延优化 通过精简初始化流程和资源预加载,可将启动时间缩短30%-50%。 // LaunchOptimization.ets import TaskPool from @oh…

#字符串执行函数——eval()、exec()和compile()详解

字符串执行函数——eval()、exec()和compile()详解 字符串类型代码的执行函数有三个,都是Python的内置函数。 eval() 执行字符串类型的代码,并返回最终结果。 exec() 执行字符串类型的代码。 #### compile() 将字符…

HarmonyOS 5 网络编程与数据存储实战:从RESTful API到本地持久化

🌐 一、网络编程基础与权限配置 HarmonyOS提供了强大的网络能力,支持HTTP/HTTPS、WebSocket、Socket等协议。 1. 权限声明与配置 在 module.json5中声明网络权限和加密通信权限: {"module": {"requ…

视觉传达设计网站深圳 德 网站建设

主要内容 自定义函数式接口函数式编程常用函数式接口 第一章 函数式接口 概念 函数式接口在java中指的是:有且只有一个抽象方法的接口 函数式接口,即适用于函数式编程场景的接口.而java中共的函数式编程体现就是Lambda,所以函数式接口就是可以适用于lambda使用的接口.只有…

OceanBase 向量数据库使用指南

OceanBase 向量数据库使用指南为了大家更好地使用 OceanBase 向量数据库, OceanBase 中负责研发向量能力的一众研发同学共同为大家写了这篇《OceanBase 向量数据库使用指南》,推荐各位老师收藏本文,以备不时之需。A…

【光照】[环境光ambient]以UnityURP为例

【从UnityURP开始探索游戏渲染】专栏-直达环境光的基本处理流程 $Cambient$‌环境光采集‌:获取场景环境光照信息 ‌环境光遮蔽‌:计算环境光遮挡关系 ‌环境光反射‌:根据材质属性反射环境光 ‌环境光混合‌:与其…

浅谈当前时代下大学生的就业择业及人生规划

浅谈当前时代下大学生的就业择业及人生规划: 叠甲阶段: 我不是专业的人生规划师,也不是手握各大厂资源和offer的人脉大佬。我只是一个在芸芸大学生中的其中一个小透明。眼界与资源都具有局限性。各位佬,同学权当汲…

网站备案一般要多久pr免费模板网站

转载自Thrift在Windows及Linux平台下的安装和使用示例 thrift介绍 Apache Thrift 是 Facebook 实现的一种高效的、支持多种编程语言的RPC(远程服务调用)框架。 本文主要目的是分别介绍在Windows及Linux平台下的Thrift安装步骤&#xff0c;以及实现一个简单的demo演示Thrif…