使用notificationManager及wantAgent实现功能 import notificationManager from '@ohos.notificationManager' import wantAgent from '@ohos.app.ability.wantAgent' @Entry @Component struct Index { @State message: string = '发布进度条通知' progressValue: number=0 async publicDownloadNotiication(){ // 导入模块notificationManager // notificationManager //行为意图 let wantInfo:wantAgent.WantAgentInfo={ wants:[ { bundleName:'com.example.myapplication', //跳转哪个应用 abilityName:'EntryAbility' //跳转应用后哪个ability } //使用大括号封装不同的功能 ], operationType:wantAgent.OperationType.START_ABILITY, //START_ABILITY 启动ability requestCode:0 } //创建实例对象 let wantInstance=await wantAgent.getWantAgent(wantInfo) let isSupport=notificationManager.isSupportTemplate('downloadTemplate') //当前系统是否支持 .then(()=>{ console.log('成功') }) .catch(()=>{ console.log('失败') }) if (!isSupport) { return } let template={ name:'downloadTemplate', //设置进度条 data:{ progressValue:this.progressValue, progressMaxValue:100 //设置一个最大值progressMaxValue } } let notificationRequest:notificationManager.NotificationRequest={ id:1, template:template, //加入进度条,由普通通知变为有进度条的 wantAgent:wantInstance, content:{ contentType:notificationManager.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT, //设置普通通知 normal:{ title:'下载中', text:'', additionalText:`${this.progressValue}%` //进度百分之多少,引号使用Tab键上边的那个波浪键 } } } notificationManager.publish(notificationRequest) } build() { Row() { Column() { Text(this.message) .fontSize(50) .fontWeight(FontWeight.Bold) .onClick(()=>{ // 添加一个定时器,模拟每过5s进度条增加20%的动态效果 this.publicDownloadNotiication() let taskid=setInterval(()=>{ if (this.progressValue>=80) { clearInterval(taskid) //clearInterval清除 } this.progressValue+=20 this.publicDownloadNotiication() },5000) //5s }) } .width('100%') } .height('100%') } }
实际效果图: