鸿蒙 DevEcoStudio:发布进度条通知

使用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%')
  }
}

实际效果图:

  • 16
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值