HarmonyOS NEXT开发实战:在Native侧实现进度通知功能

介绍

本示例通过模拟下载场景介绍如何将Native的进度信息实时同步到ArkTS侧。

效果图预览

使用说明

  1. 点击“Start Download“按钮后,Native侧启动子线程模拟下载任务
  2. Native侧启动子线程模拟下载,并通过Arkts的回调函数将进度信息实时传递到Arkts侧

实现思路

  1. 前端进度条使用Progress绘制
    Progress({ value: this.progress, total: 100, type: ProgressType.Ring })
        .width($r("app.integer.progress_size"))
        .height($r("app.integer.progress_size"))
        .animation({ duration: NativeProgressNotifyConstants.PROGRESS_ANIMATION_DURATION, curve: Curve.Ease })
        .style({ strokeWidth: 15 })
  2. JS侧调用Native侧方法,并传入用于接收下载进度的回调函数,在该回调函数中更改状态变量
    naitiveprogressnotify.startDownload((data: number) => {
        this.progress = data;
        console.info("[NativeProgressNotify]progress:" + this.progress);
    })
  3. Naitive侧使用std::thread创建子线程执行模拟下载的任务
    std::thread downloadThread(downloadTask, asyncContext);
    downloadThread.detach();
  4. Native侧模拟下载的线程中,每100ms执行一次uv_queue_work;向eventloop事件堆栈push异步任务。
     while (context && context->progress < 100) {
         context->progress += 1;
         napi_acquire_threadsafe_function(context->tsfn);
         napi_call_threadsafe_function(context
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值