当前位置: 首页> 财经> 金融 > 鸿蒙 DevEcoStudio:发布进度条通知

鸿蒙 DevEcoStudio:发布进度条通知

时间:2025/7/11 14:28:49来源:https://blog.csdn.net/m0_74411635/article/details/139107949 浏览次数:0次
使用notificationManager及wantAgent实现功能import notificationManager from '@ohos.notificationManager'
import wantAgent from '@ohos.app.ability.wantAgent'
@Entry
@Component
struct Index {@State message: string = '发布进度条通知'progressValue: number=0async publicDownloadNotiication(){// 导入模块notificationManager// notificationManager//行为意图let wantInfo:wantAgent.WantAgentInfo={wants:[{bundleName:'com.example.myapplication',  //跳转哪个应用abilityName:'EntryAbility'  //跳转应用后哪个ability} //使用大括号封装不同的功能],operationType:wantAgent.OperationType.START_ABILITY,  //START_ABILITY 启动abilityrequestCode: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+=20this.publicDownloadNotiication()},5000)  //5s})}.width('100%')}.height('100%')}
}

实际效果图:

关键字:鸿蒙 DevEcoStudio:发布进度条通知

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: