鸿蒙开发相关知识(四)【数据持久化(用户首选项、关系型数据库)、通知(基础通知、进度条通知、通知意图)】

本文主要是介绍鸿蒙开发相关知识(四)【数据持久化(用户首选项、关系型数据库)、通知(基础通知、进度条通知、通知意图)】,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

文章目录

  • 一、数据持久化
    • 1、用户首选项
      • (1)语法说明
      • (2)完整代码示例
    • 2、关系型数据库
      • (1)初始化数据库
      • (2)增删改数据
      • (3)查询数据
      • (4)完整代码示例
  • 二、通知
    • 1、基础通知
      • (1)基础使用
      • (2)通知内容类型
      • (3)完整代码示例
    • 2、进度条通知
      • (1)基础使用
      • (3)完整代码示例
    • 3、通知意图
      • (1)基础使用
      • (2)完整代码示例


一、数据持久化

1、用户首选项

(1)语法说明

  • 为应用提供Key-Value键值型的数据处理能力,支持应用持久化轻量级数据,并对其修改和查询。
  • Key键为string类型,要求非空且长度不超过80个字节。
  • 如果Value值为string类型,可以为空,不为空时长度不超过8192个字节。
  • 建议存储的数据不超过一万条。
  • 导入用户首选项模块
import dataPreferences from '@ohos.data.preferences';
  • 要获取Preferences实例,读取指定文件
 dataPreferences.getPreferences(this.context, 'mystore', (err, preferences) => {if (err) {console.error(`Failed to get preferences. Code:${err.code},message:${err.message}`);return;}console.info('Succeeded in getting preferences.');// 进行相关数据操作})
  • 数据操作
  • 写入数据,如果已经存在则会覆盖,可利用.has() 判断是否存在
 preferences.put('startup', 'auto', (err) => {if (err) {console.error(`Failed to put data. Code:${err.code}, message:${err.message}`);return;}console.info('Succeeded in putting data.');})
  • 读取数据,如果值为null或者非默认值类型,则返回默认数据。
 preferences.get('startup', 'default', (err, val) => {if (err) {console.error(`Failed to get value of 'startup'. Code:${err.code}, message:${err.message}`);return;}console.info(`Succeeded in getting value of 'startup'. val: ${val}.`);})
  • 删除数据
  preferences.delete('startup', (err) => {if (err) {console.error(`Failed to delete the key 'startup'. Code:${err.code}, message:${err.message}`);return;}console.info("Succeeded in deleting the key 'startup'.");})
  • 数据持久化,应用存入数据到Preferences实例后,可以使用flush()方法实现数据持久化
preferences.flush((err) => {if (err) {console.error(`Failed to flush. Code:${err.code}, message:${err.message}`);return;}console.info('Succeeded in flushing.');})

(2)完整代码示例

  • ets/common/util/PreferencesUtil.ts
  • 加载实例、写入、获取、删除方法
import preferences from '@ohos.data.preferences';class PreferencesUtil{prefMap: Map<string, preferences.Preferences> = new Map()async loadPreference(context, name: string){try { // 加载preferenceslet pref = await preferences.getPreferences(context, name)this.prefMap.set(name, pref)console.log('testTag', `加载Preferences[${name}]成功`)} catch (e) {console.log('testTag', `加载Preferences[${name}]失败`, JSON.stringify(e))}}async putPreferenceValue(name: string, key: string, value: preferences.ValueType){if (!this.prefMap.has(name)) {console.log('testTag', `Preferences[${name}]尚未初始化!`)return}try {let pref = this.prefMap.get(name)// 写入数据await pref.put(key, value)// 刷盘await pref.flush()console.log('testTag', `保存Preferences[${name}.${key} = ${value}]成功`)} catch (e) {console.log('testTag', `保存Preferences[${name}.${key} = ${value}]失败`, JSON.stringify(e))}}async getPreferenceValue(name: string, key: string, defaultValue: preferences.ValueType){if (!this.prefMap.has(name)) {console.log('testTag', `Preferences[${name}]尚未初始化!`)return}try {let pref = this.prefMap.get(name)// 读数据let value = await pref.get(key, defaultValue)console.log('testTag', `读取Preferences[${name}.${key} = ${value}]成功`)return value} catch (e) {console.log('testTag', `读取Preferences[${name}.${key} ]失败`, JSON.stringify(e))}}
}const preferencesUtil = new PreferencesUtil()export default preferencesUtil as PreferencesUtil
  • ets/entryability/EntryAbility.ets
  • 应用启动时调用加载实例方法
import UIAbility from '@ohos.app.ability.UIAbility';
import PreferencesUtil from '../common/util/PreferencesUtil'export default class EntryAbility extends UIAbility {async onCreate(want, launchParam) {hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate running');// 加载Preferencesawait PreferencesUtil.loadPreference(this.context, 'MyPreferences')}
}
  • ets/pages/Index.ets
  • 页面出现前(aboutToAppear())获取实例并赋值
import IndexFontSizePanel from '../views/IndexFontSizePanel'
import PreferencesUtil from '../common/util/PreferencesUtil'@Entry
@Component
struct Index {@State message: string = '页面列表'@State showPanel: boolean = false@Provide fontSize: number = 16async aboutToAppear(){this.fontSize = await PreferencesUtil.getPreferenceValue('MyPreferences', 'IndexFontSize', 16) as number}build() {Column() {// 字体修改面板if(this.showPanel){IndexFontSizePanel().transition({translate: { y: 115 }})}}.width('100%').height('100%')}}
  • views/IndexFontSizePanel.ets
  • onChange 方法中修改实例值
import PreferencesUtil from '../common/util/PreferencesUtil'@Component
export default struct IndexFontSizePanel {@Consume fontSize: numberfontSizLabel: object = {14: '小',16: '标准',18: '大',20: '特大',}build() {Column() {Text(this.fontSizLabel[this.fontSize]).fontSize(20)Row({ space: 5 }) {Text('A').fontSize(14).fontWeight(FontWeight.Bold)Slider({min: 14,max: 20,step: 2,value: this.fontSize}).showSteps(true).trackThickness(6).layoutWeight(1).onChange(val => {// 修改字体大小this.fontSize = val// 写入PreferencesPreferencesUtil.putPreferenceValue('MyPreferences', 'IndexFontSize', val)})Text('A').fontSize(20).fontWeight(FontWeight.Bold)}.width('100%')}.width('100%').padding(15).backgroundColor('#fff1f0f0').borderRadius(20)}
}

2、关系型数据库

(1)初始化数据库

  • a、导入关系型数据库模块
import relationalStore from '@ohos.data.relationalStore';
  • b、初始化数据库表
  • rdb配置
const STORE_CONFIG = {name: 'RdbTest.db', // 数据库文件名securityLevel: relationalStore.SecurityLevel.S1 // 数据库安全级别
};
  • 初始化表的SQL
// 建表Sql语句
const SQL_CREATE_TABLE = 'CREATE TABLE IF NOT EXISTS EMPLOYEE (ID INTEGER PRIMARY KEY AUTOINCREMENT, NAME TEXT NOT NULL, AGE INTEGER, SALARY REAL, CODES BLOB)';
  • 获取rdb,执行SQL,后续的所有增删改查都是使用rdbStore对象
relationalStore.getRdbStore(this.context, STORE_CONFIG, (err, store) => {if (err) {console.error(`Failed to get RdbStore. Code:${err.code}, message:${err.message}`);return;}console.info(`Succeeded in getting RdbStore.`);store.executeSql(SQL_CREATE_TABLE); // 创建数据表// 这里执行数据库的增、删、改、查等操作});

(2)增删改数据

  • a.新增数据
const valueBucket = {'NAME': 'Lisa','AGE': 18,'SALARY': 100.5,'CODES': new Uint8Array([1, 2, 3, 4, 5])
};
//EMPLOYEE 数据表名
store.insert('EMPLOYEE', valueBucket, (err, rowId) => {if (err) {console.error(`Failed to insert data. Code:${err.code}, message:${err.message}`);return;}console.info(`Succeeded in inserting data. rowId:${rowId}`);
})
  • b.修改数据
const valueBucket = {'NAME': 'Rose','AGE': 22,
};
// 创建表'EMPLOYEE'的predicates
let predicates = new relationalStore.RdbPredicates('EMPLOYEE');
// 匹配表'EMPLOYEE'中'NAME'为'Lisa'的字段
predicates.equalTo('NAME', 'Lisa'); 
store.update(valueBucket, predicates, (err, rows) => {if (err) {console.error(`Failed to update data. Code:${err.code}, message:${err.message}`);return;}console.info(`Succeeded in updating data. row count: ${rows}`);
})
  • c.删除数据
let predicates = new relationalStore.RdbPredicates('EMPLOYEE');
predicates.equalTo('NAME', 'Lisa');
store.delete(predicates, (err, rows) => {if (err) {console.error(`Failed to delete data. Code:${err.code}, message:${err.message}`);return;}console.info(`Delete rows: ${rows}`);
})

(3)查询数据

  • a.查询数据、返回一个ResultSet结果集
let predicates = new relationalStore.RdbPredicates('EMPLOYEE');
predicates.equalTo('NAME', 'Rose');
let result = await store.query(predicates, ['ID', 'NAME', 'AGE', 'SALARY', 'CODES'])
  • b.解析结果
// 准备数组保存结果
let tasks:any[]=[]
//循环遍历结果集,判断结果是否遍历到最后一行
while(!result.isAtLastRow){//指针移动到下一行数据result.goToNextRow()//根据字段名获取字段index,从而获取字段值let id = result.getLong(result.getColumnIndex('ID'));let name = result.getString(result.getColumnIndex('NAME'));tasks.push({id,name})
}

(4)完整代码示例

  • ets/model/TaskModel.ets
import relationalStore from '@ohos.data.relationalStore';
import TaskInfo from '../viewmodel/TaskInfo';class TaskModel {private rdbStore: relationalStore.RdbStoreprivate tableName: string = 'TASK'/*** 初始化任务表*/initTaskDB(context){// 1.rdb配置const config = {name: 'MyApplication.db',securityLevel: relationalStore.SecurityLevel.S1}// 2.初始化SQL语句const sql = `CREATE TABLE IF NOT EXISTS TASK (ID INTEGER PRIMARY KEY AUTOINCREMENT,NAME TEXT NOT NULL,FINISHED bit)`// 3.获取rdbrelationalStore.getRdbStore(context, config, (err, rdbStore) => {if(err){console.log('testTag', '获取rdbStore失败!')return}// 执行SqlrdbStore.executeSql(sql)console.log('testTag', '创建task表成功!')// 保存rdbStorethis.rdbStore = rdbStore})}/*** 查询任务列表*/async getTaskList(){// 1.构建查询条件let predicates = new relationalStore.RdbPredicates(this.tableName)// 2.查询let result = await this.rdbStore.query(predicates, ['ID', 'NAME', 'FINISHED'])// 3.解析查询结果// 3.1.定义一个数组,组装最终的查询结果let tasks: TaskInfo[] = []// 3.2.遍历封装while(!result.isAtLastRow){// 3.3.指针移动到下一行result.goToNextRow()// 3.4.获取数据let id = result.getLong(result.getColumnIndex('ID'))let name = result.getString(result.getColumnIndex('NAME'))let finished = result.getLong(result.getColumnIndex('FINISHED'))// 3.5.封装到数组tasks.push({id, name, finished: !!finished})}console.log('testTag', '查询到数据:', JSON.stringify(tasks))return tasks}/*** 添加一个新的任务* @param name 任务名称* @returns 任务id*/addTask(name: string): Promise<number>{return this.rdbStore.insert(this.tableName, {name, finished: false})}/*** 根据id更新任务状态* @param id 任务id* @param finished 任务是否完成*/updateTaskStatus(id: number, finished: boolean) {// 1.要更新的数据let data = {finished}// 2.更新的条件let predicates = new relationalStore.RdbPredicates(this.tableName)predicates.equalTo('ID', id)// 3.更新操作return this.rdbStore.update(data, predicates)}/*** 根据id删除任务* @param id 任务id*/deleteTaskById(id: number){// 1.删除的条件let predicates = new relationalStore.RdbPredicates(this.tableName)predicates.equalTo('ID', id)// 2.删除操作return this.rdbStore.delete(predicates)}
}let taskModel = new TaskModel();export default taskModel as TaskModel;
  • ets/entryability/EntryAbility.ets
  • 应用启动时调用初始化
import UIAbility from '@ohos.app.ability.UIAbility';
import hilog from '@ohos.hilog';
import window from '@ohos.window';
import TaskModel from '../model/TaskModel';export default class EntryAbility extends UIAbility {async onCreate(want, launchParam) {// 初始化任务表TaskModel.initTaskDB(this.context)}
}
  • 页面调用方法查询、添加数据
import TaskModel from '../../model/TaskModel'// 总任务数量@Link totalTask: number@Link finishTask: number// 任务数组@State tasks: TaskInfo[] = []aboutToAppear(){// 查询任务列表console.log('testTag', '初始化组件,查询任务列表')TaskModel.getTaskList().then(tasks => {this.tasks = tasks// 更新任务状态this.handleTaskChange()})}handleTaskChange(){// 1.更新任务总数量this.totalTask = this.tasks.length// 2.更新已完成任务数量this.finishTask = this.tasks.filter(item => item.finished).length}
  handleAddTask(name: string){// 1.新增任务TaskModel.addTask(name).then(id => {console.log('testTag', '处理新增任务: ', name)// 回显到数组页面this.tasks.push(new TaskInfo(id, name))// 2.更新任务完成状态this.handleTaskChange()// 3.关闭对话框this.dialogController.close()}).catch(error => console.log('testTag', '新增任务失败:', name, JSON.stringify(error)))}
@Builder DeleteButton(index: number, id: number){Button(){Image($r('app.media.ic_public_delete_filled')).fillColor(Color.White).width(20)}.width(40).height(40).type(ButtonType.Circle).backgroundColor(Color.Red).margin(5).onClick(() => {// 删除任务TaskModel.deleteTaskById(id).then(() => {this.tasks.splice(index, 1)console.log('testTag', `尝试删除任务,index: ${index}`)this.handleTaskChange()}).catch(error => console.log('testTag', '删除任务失败,id = ', id, JSON.stringify(error)))})}

二、通知

1、基础通知

  • 应用可以通过通知接口发送通知消息,提醒用户关注应用中变化。
  • 用户可以在通知栏查看和操作通知内容

(1)基础使用

  • 导入notification模块
import notificationManager from '@ohos.notificationManager';
  • 构建通知请求
let request: notificationManager.NotificationRequest = {id: 1, content: {// 通知内容:...}},showDeliveryTime: true, // 是否显示分发时间deliveryTime: new Date().getTime(), // 通知发送时间groupName: 'wechat', // 组通知名称slotType: notify.SlotType.SOCIAL_COMMUNICATION  // 通道类型...   //其它属性查看相关文档
}
  • 发布通知
notificationManager.publish(request, (err) => {if (err) {console.error(`Failed to publish notification. Code is ${err.code}, message is ${err.message}`);return;}console.info('Succeeded in publishing notification.');
});
  • 取消通知
// 取消 id=10 的通知
notificationManager.cancel(10)
// 取消当前应用所有通知
notificationManager.cancelAll()

(2)通知内容类型

  • 普通文本类型
let notificationRequest = {id: 1,content: {contentType: notificationManager.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT, // 普通文本类型通知normal: {title: '通知标题',text: '通知内容详情',additionalText: '通知附加内容',}}
}
  • 长文本类型
let notificationRequest = {id: 1,content: {contentType: notificationManager.ContentType.NOTIFICATION_CONTENT_LONG_TEXT, // 长文本类型通知longText: {title: '通知标题',text: '通知内容详情',additionalText: '通知附加内容',longText: '通知中的长文本、很长很长。。。',briefText: '通知概要总结',expandedTitle: '通知展开时的标题',}}
}
  • 多行文本类型
let notificationRequest = {id: 1,content: {contentType: NotificationManager.ContentType.NOTIFICATION_CONTENT_MULTILINE, // 多行文本类型通知multiLine: {title: '通知标题',text: '通知内容详情',additionalText: '通知附加内容',briefText: '通知概要总结',longTitle: '展开时的标题,有多行,我很宽',lines: ['第一行', '第二行', '第三行', '第四行'],}}
}
  • 图片类型
// 需要获取图片PixelMap信息
let imagePixelMap: PixelMap = undefined; 
let notificationRequest: notificationManager.NotificationRequest = {id: 1,content: {contentType: notificationManager.ContentType.NOTIFICATION_CONTENT_PICTURE,picture: {title: '通知标题',text: '通知内容详情',additionalText: '通知附加内容',briefText: '通知概要总结',expandedTitle: '通知展开时的标题',picture: imagePixelMap}}
};
  • 获取图片PixelMap信息
async aboutToAppear() {// 获取资源管理器let rm = getContext(this).resourceManager;// 读取图片let file = await rm.getMediaContent($r('app.media.watchGT4'))// 创建PixelMapimage.createImageSource(file.buffer).createPixelMap().then(value => this.pixel = value).catch(reason => console.log('testTag', '加载图片异常', JSON.stringify(reason)))
}

(3)完整代码示例

import notify from '@ohos.notificationManager'
import image from '@ohos.multimedia.image'@Entry
@Component
struct NotificationPage {// 全局任务ididx: number = 100// 图象pixel: PixelMapasync aboutToAppear() {// 获取资源管理器let rm = getContext(this).resourceManager;// 读取图片let file = await rm.getMediaContent($r('app.media.watchGT4'))// 创建PixelMapimage.createImageSource(file.buffer).createPixelMap().then(value => this.pixel = value).catch(reason => console.log('testTag', '加载图片异常', JSON.stringify(reason)))}build() {Column({space: 20}) {Button(`发送normalText通知`).onClick(() => this.publishNormalTextNotification())Button(`发送longText通知`).onClick(() => this.publishLongTextNotification())Button(`发送multiLine通知`).onClick(() => this.publishMultiLineNotification())Button(`发送Picture通知`).onClick(() => this.publishPictureNotification())}.width('100%').height('100%').padding(5).backgroundColor('#f1f2f3')}//通知普通文本publishNormalTextNotification() {let request: notify.NotificationRequest = {id: this.idx++,content: {contentType: notify.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT,normal: {title: '通知标题' + this.idx,text: '通知内容详情',additionalText: '通知附加内容'}},showDeliveryTime: true,deliveryTime: new Date().getTime(),groupName: 'wechat',slotType: notify.SlotType.SOCIAL_COMMUNICATION}this.publish(request)}//通知长文本publishLongTextNotification() {let request: notify.NotificationRequest = {id: this.idx++,content: {contentType: notify.ContentType.NOTIFICATION_CONTENT_LONG_TEXT,longText: {title: '通知标题' + this.idx,text: '通知内容详情',additionalText: '通知附加内容',longText: '通知中的长文本,我很长,我很长,我很长,我很长,我很长,我很长,我很长',briefText: '通知概要和总结',expandedTitle: '通知展开时的标题' + this.idx}}}this.publish(request)}//通知多行文本publishMultiLineNotification() {let request: notify.NotificationRequest = {id: this.idx++,content: {contentType: notify.ContentType.NOTIFICATION_CONTENT_MULTILINE,multiLine: {title: '通知标题' + this.idx,text: '通知内容详情',additionalText: '通知附加内容',briefText: '通知概要和总结',longTitle: '展开时的标题,我很宽,我很宽,我很宽',lines: ['第一行','第二行','第三行','第四行',]}}}this.publish(request)}//通知图片类型publishPictureNotification() {let request: notify.NotificationRequest = {id: this.idx++,content: {contentType: notify.ContentType.NOTIFICATION_CONTENT_PICTURE,picture: {title: '通知标题' + this.idx,text: '通知内容详情',additionalText: '通知附加内容',briefText: '通知概要和总结',expandedTitle: '展开后标题' + this.idx,picture: this.pixel}}}this.publish(request)}// 发送文本private publish(request: notify.NotificationRequest) {notify.publish(request).then(() => console.log('notify test', '发送通知成功')).then(reason => console.log('notify test', '发送通知失败', JSON.stringify(reason)))}
}

2、进度条通知

  • 进度条通知会展示一个动态的进度条,主要用于文件下载、长任务处理的进度显示。

(1)基础使用

  • 判断当前系统是否支持进度条模板
NotificationManager.isSupportTemplate('downloadTemplate').then((data) => {console.info(`[ANS] isSupportTemplate success`);let isSupportTpl: boolean = data; // isSupportTpl的值为true表示支持支持downloadTemplate模板类通知,false表示不支持// ...
}).catch((err) => {console.error(`[ANS] isSupportTemplate failed, error[${err}]`);
});
  • 通知模板
  • 模板名称,必须是 downloadTemplate
let template = {name:'downloadTemplate',data: {title: '标题:',fileName: 'music.mp4', // 文件名progressValue: 30, //进度条当前值progressMaxValue:100, // 进度条最大值}
}
  • 通知请求
let notificationRquest = {id: 1,slotType: notify.SlotType.OTHER_TYPES,template: template, //进度条模板content: {contentType: notify.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT,normal: {title: template.data.title + template.data.fileName,text: "sendTemplate",additionalText: "30%"}},deliveryTime: new Date().getTime(),showDeliveryTime: true
}
  • 发送通知,与基础通知相同

(3)完整代码示例

import notify from '@ohos.notificationManager'
import promptAction from '@ohos.promptAction'enum DownloadState {NOT_BEGIN = '未开始',DOWNLOADING = '下载中',PAUSE = '已暂停',FINISHED = '已完成',
}@Component
export default struct DownloadCard {// 下载进度@State progressValue: number = 0progressMaxValue: number = 100// 任务状态@State state: DownloadState = DownloadState.NOT_BEGIN// 下载的文件名filename: string = '圣诞星.mp4'// 模拟下载的任务的idtaskId: number = -1// 通知idnotificationId: number = 999isSupport: boolean = falseasync aboutToAppear(){// 1.判断当前系统是否支持进度条模板this.isSupport = await notify.isSupportTemplate('downloadTemplate')}build() {Row({ space: 10 }) {Image($r('app.media.ic_files_video')).width(50)Column({ space: 5 }) {Row() {Text(this.filename)Text(`${this.progressValue}%`).fontColor('#c1c2c1')}.width('100%').justifyContent(FlexAlign.SpaceBetween)Progress({value: this.progressValue,total: this.progressMaxValue,})Row({ space: 5 }) {Text(`${(this.progressValue * 0.43).toFixed(2)}MB`).fontSize(14).fontColor('#c1c2c1')Blank()if (this.state === DownloadState.NOT_BEGIN) {Button('开始').downloadButton().onClick(() => this.download())} else if (this.state === DownloadState.DOWNLOADING) {Button('取消').downloadButton().backgroundColor('#d1d2d3').onClick(() => this.cancel())Button('暂停').downloadButton().onClick(() => this.pause())} else if (this.state === DownloadState.PAUSE) {Button('取消').downloadButton().backgroundColor('#d1d2d3').onClick(() => this.cancel())Button('继续').downloadButton().onClick(() => this.download())} else {Button('打开').downloadButton().onClick(() => this.open())}}.width('100%')}.layoutWeight(1)}.width('100%').borderRadius(20).padding(15).backgroundColor(Color.White)}cancel() {// 取消定时任务if(this.taskId > 0){clearInterval(this.taskId);this.taskId = -1}// 清理下载任务进度this.progressValue = 0// 标记任务状态:未开始this.state = DownloadState.NOT_BEGIN// 取消通知notify.cancel(this.notificationId)}download() {// 清理旧任务if(this.taskId > 0){clearInterval(this.taskId);}// 开启定时任务,模拟下载this.taskId = setInterval(() => {// 判断任务进度是否达到100if(this.progressValue >= 100){// 任务完成了,应该取消定时任务clearInterval(this.taskId)this.taskId = -1// 并且标记任务状态为已完成this.state = DownloadState.FINISHED// 发送通知this.publishDownloadNotification()return}// 模拟任务进度变更this.progressValue += 2// 发送通知this.publishDownloadNotification()}, 500)// 标记任务状态:下载中this.state = DownloadState.DOWNLOADING}pause() {// 取消定时任务if(this.taskId > 0){clearInterval(this.taskId);this.taskId = -1}// 标记任务状态:已暂停this.state = DownloadState.PAUSE// 发送通知this.publishDownloadNotification()}open() {promptAction.showToast({message: '功能未实现'})}publishDownloadNotification(){// 1.判断当前系统是否支持进度条模板if(!this.isSupport){// 当前系统不支持进度条模板return}// 2.准备进度条模板的参数let template = {name: 'downloadTemplate',data: {progressValue: this.progressValue,progressMaxValue: this.progressMaxValue}}let request: notify.NotificationRequest = {id: this.notificationId,template: template,content: {contentType: notify.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT,normal: {title: this.filename + ':  ' + this.state,text: '',additionalText: this.progressValue + '%'}}}// 3.发送通知notify.publish(request).then(() => console.log('test', '通知发送成功')).catch(reason => console.log('test', '通知发送失败!', JSON.stringify(reason)))}
}@Extend(Button) function downloadButton() {.width(75).height(28).fontSize(14)
}

3、通知意图

  • 我们可以给通知或其中的按钮设置的行为意图(Want
  • 从而实现拉起应用组件或发布公共事件等能力。

(1)基础使用

  • 导入模块
import NotificationManager from '@ohos.notificationManager';
import wantAgent from '@ohos.app.ability.wantAgent';
  • 意图行为信息
let wantAgentInfo = {wants: [{deviceId: '',bundleName: 'com.example.test',abilityName: 'com.example.test.MainAbility',action: '',entities: [],uri: '',parameters: {}}],operationType: wantAgent.OperationType.START_ABILITY,requestCode: 0,wantAgentFlags:[wantAgent.WantAgentFlags.CONSTANT_FLAG]
}
  • 创建wantAgent实例
 // 用于保存创建成功的WantAgent对象,后续使用其完成触发的动作。
let wantAgentObj = null;wantAgent.getWantAgent(wantAgentInfo, (err, data) => {if (err) {console.error('[WantAgent]getWantAgent err=' + JSON.stringify(err));} else {console.info('[WantAgent]getWantAgent success');wantAgentObj = data;}
});
  • 通知请求
let notificationRequest = {content: {// ....},id: 1,label: 'TEST',wantAgent: wantAgentObj,
}

(2)完整代码示例

import wantAgent, { WantAgent } from '@ohos.app.ability.wantAgent'
import promptAction from '@ohos.promptAction'@Component
export default struct DownloadCard {// 存放wantAgent实例wantAgentInstance: WantAgentasync aboutToAppear(){// 1.判断当前系统是否支持进度条模板this.isSupport = await notify.isSupportTemplate('downloadTemplate')// 2.创建拉取当前应用的行为意图// 2.1.创建wantInfo信息let wantInfo: wantAgent.WantAgentInfo = {wants: [{bundleName: 'com.example.myapplication',abilityName: 'EntryAbility',}],requestCode: 0,operationType: wantAgent.OperationType.START_ABILITY,wantAgentFlags: [wantAgent.WantAgentFlags.CONSTANT_FLAG]}// 2.2.创建wantAgent实例this.wantAgentInstance = await wantAgent.getWantAgent(wantInfo)}build() {···· // 同进度条通知代码示例}open() {promptAction.showToast({message: '功能未实现'})}publishDownloadNotification(){// 1.判断当前系统是否支持进度条模板if(!this.isSupport){// 当前系统不支持进度条模板return}// 2.准备进度条模板的参数let template = {name: 'downloadTemplate',data: {progressValue: this.progressValue,progressMaxValue: this.progressMaxValue}}let request: notify.NotificationRequest = {id: this.notificationId,template: template,// 通知意图wantAgent: this.wantAgentInstance,content: {contentType: notify.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT,normal: {title: this.filename + ':  ' + this.state,text: '',additionalText: this.progressValue + '%'}}}// 3.发送通知notify.publish(request).then(() => console.log('test', '通知发送成功')).catch(reason => console.log('test', '通知发送失败!', JSON.stringify(reason)))}
}@Extend(Button) function downloadButton() {.width(75).height(28).fontSize(14)
}

这篇关于鸿蒙开发相关知识(四)【数据持久化(用户首选项、关系型数据库)、通知(基础通知、进度条通知、通知意图)】的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/764762

相关文章

Spring Security基于数据库验证流程详解

Spring Security 校验流程图 相关解释说明(认真看哦) AbstractAuthenticationProcessingFilter 抽象类 /*** 调用 #requiresAuthentication(HttpServletRequest, HttpServletResponse) 决定是否需要进行验证操作。* 如果需要验证,则会调用 #attemptAuthentica

Java架构师知识体认识

源码分析 常用设计模式 Proxy代理模式Factory工厂模式Singleton单例模式Delegate委派模式Strategy策略模式Prototype原型模式Template模板模式 Spring5 beans 接口实例化代理Bean操作 Context Ioc容器设计原理及高级特性Aop设计原理Factorybean与Beanfactory Transaction 声明式事物

大模型研发全揭秘:客服工单数据标注的完整攻略

在人工智能(AI)领域,数据标注是模型训练过程中至关重要的一步。无论你是新手还是有经验的从业者,掌握数据标注的技术细节和常见问题的解决方案都能为你的AI项目增添不少价值。在电信运营商的客服系统中,工单数据是客户问题和解决方案的重要记录。通过对这些工单数据进行有效标注,不仅能够帮助提升客服自动化系统的智能化水平,还能优化客户服务流程,提高客户满意度。本文将详细介绍如何在电信运营商客服工单的背景下进行

基于MySQL Binlog的Elasticsearch数据同步实践

一、为什么要做 随着马蜂窝的逐渐发展,我们的业务数据越来越多,单纯使用 MySQL 已经不能满足我们的数据查询需求,例如对于商品、订单等数据的多维度检索。 使用 Elasticsearch 存储业务数据可以很好的解决我们业务中的搜索需求。而数据进行异构存储后,随之而来的就是数据同步的问题。 二、现有方法及问题 对于数据同步,我们目前的解决方案是建立数据中间表。把需要检索的业务数据,统一放到一张M

这15个Vue指令,让你的项目开发爽到爆

1. V-Hotkey 仓库地址: github.com/Dafrok/v-ho… Demo: 戳这里 https://dafrok.github.io/v-hotkey 安装: npm install --save v-hotkey 这个指令可以给组件绑定一个或多个快捷键。你想要通过按下 Escape 键后隐藏某个组件,按住 Control 和回车键再显示它吗?小菜一碟: <template

关于数据埋点,你需要了解这些基本知识

产品汪每天都在和数据打交道,你知道数据来自哪里吗? 移动app端内的用户行为数据大多来自埋点,了解一些埋点知识,能和数据分析师、技术侃大山,参与到前期的数据采集,更重要是让最终的埋点数据能为我所用,否则可怜巴巴等上几个月是常有的事。   埋点类型 根据埋点方式,可以区分为: 手动埋点半自动埋点全自动埋点 秉承“任何事物都有两面性”的道理:自动程度高的,能解决通用统计,便于统一化管理,但个性化定

Hadoop企业开发案例调优场景

需求 (1)需求:从1G数据中,统计每个单词出现次数。服务器3台,每台配置4G内存,4核CPU,4线程。 (2)需求分析: 1G / 128m = 8个MapTask;1个ReduceTask;1个mrAppMaster 平均每个节点运行10个 / 3台 ≈ 3个任务(4    3    3) HDFS参数调优 (1)修改:hadoop-env.sh export HDFS_NAMENOD

使用SecondaryNameNode恢复NameNode的数据

1)需求: NameNode进程挂了并且存储的数据也丢失了,如何恢复NameNode 此种方式恢复的数据可能存在小部分数据的丢失。 2)故障模拟 (1)kill -9 NameNode进程 [lytfly@hadoop102 current]$ kill -9 19886 (2)删除NameNode存储的数据(/opt/module/hadoop-3.1.4/data/tmp/dfs/na

异构存储(冷热数据分离)

异构存储主要解决不同的数据,存储在不同类型的硬盘中,达到最佳性能的问题。 异构存储Shell操作 (1)查看当前有哪些存储策略可以用 [lytfly@hadoop102 hadoop-3.1.4]$ hdfs storagepolicies -listPolicies (2)为指定路径(数据存储目录)设置指定的存储策略 hdfs storagepolicies -setStoragePo

Hadoop集群数据均衡之磁盘间数据均衡

生产环境,由于硬盘空间不足,往往需要增加一块硬盘。刚加载的硬盘没有数据时,可以执行磁盘数据均衡命令。(Hadoop3.x新特性) plan后面带的节点的名字必须是已经存在的,并且是需要均衡的节点。 如果节点不存在,会报如下错误: 如果节点只有一个硬盘的话,不会创建均衡计划: (1)生成均衡计划 hdfs diskbalancer -plan hadoop102 (2)执行均衡计划 hd