• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# 任务延时调度
2
3### 介绍
4
5本示例使用[@ohos.WorkSchedulerExtensionAbility](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/reference/apis/js-apis-WorkSchedulerExtensionAbility.md)
6、[@ohos.net.http](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/reference/apis/js-apis-http.md)
7、[@ohos.notification](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/reference/apis/js-apis-notification.md)
8、[@ohos.bundle](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/reference/apis/js-apis-Bundle.md)
9、[@ohos.fileio](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/reference/apis/js-apis-fileio.md) 等接口,实现了设置后台任务、下载更新包
10、保存更新包、发送通知 、安装更新包实现升级的功能。
11
12### 效果预览
13
14|当前版本首页                             |提示弹窗                                 |最新版本                                |
15|---------------------------------------|---------------------------------------|---------------------------------------|
16|![image](screenshots/device/oldVersion.png)|![image](screenshots/device/dialog.png)|![image](screenshots/device/newVersion.png)|
17
18使用说明
19
201. 未连接wifi状态下进入应用;
212. 进入首页后连接wifi;
223. 后台判断版本号后会下载最新的升级包,并在页面中给出弹窗询问是否安装,点击“确定”按钮;
234. 应用会安装已经下载的升级包,实现版本更新,安装后会回到设备桌面,此时点击应用图标,可以看到版本已经是最新版了。
24
25### 工程目录
26```
27entry/src/main/ets/
28|---Application
29|   |---MyAbilityStage.ets                  // 入口文件
30|---feature
31|   |---WorkSchedulerSystem.ets             // 封装各个功能接口
32|---MainAbility
33|   |---MainAbility.ets                     // 请求权限
34|---pages
35|   |---Index.ets                           // 首页
36|---util
37|   |---Logger.ets                          // 日志文件
38|---WorkSchedulerAbility
39|   |---WorkSchedulerAbility.ets            // 延时任务触发后的回调
40```
41
42### 具体实现
43
44* 设置延时任务、下载更新包、保存更新包、发送通知、安装更新包的功能接口都封装在WorkSchedulerSystem中,
45源码参考:[WorkSchedulerSystem.ets](https://gitee.com/openharmony/applications_app_samples/blob/master/code/BasicFeature/TaskManagement/WorkScheduler/entry/src/main/ets/feature/WorkSchedulerSystem.ets)
46
47  * 设置延时任务:在运行示例时会在[MainAbility.ets](https://gitee.com/openharmony/applications_app_samples/blob/master/code/BasicFeature/TaskManagement/WorkScheduler/entry/src/main/ets/MainAbility/MainAbility.ets)
48  通过WorkSchedulerSystem.startUpdateSample()方法调用workScheduler.startWork()建立任务;
49  * 下载更新包:当任务条件满足后,会在[WorkSchedulerAbility.ets](https://gitee.com/openharmony/applications_app_samples/blob/master/code/BasicFeature/TaskManagement/WorkScheduler/entry/src/main/ets/WorkSchedulerAbility/WorkSchedulerAbility.ets)
50  通过WorkSchedulerSystem.getNewHap()方法调用http.createHttp().request()接口下载我们需要的文件;
51  * 保存更新包:通过WorkSchedulerSystem.saveFile()来实现,受限调用fileio.openSync()创建文件,然后调用fileio.writeSync()将下载的内容写入指定文件内;
52  * 发送通知:在[WorkSchedulerAbility.ets](https://gitee.com/openharmony/applications_app_samples/blob/master/code/BasicFeature/TaskManagement/WorkScheduler/entry/src/main/ets/WorkSchedulerAbility/WorkSchedulerAbility.ets)
53  中通过WorkSchedulerSystem.publishNotification()方法,调用Notification.publish()接口发送指定内容的信息;
54  * 接收通知:在[MainAbility.ets](https://gitee.com/openharmony/applications_app_samples/blob/master/code/BasicFeature/TaskManagement/WorkScheduler/entry/src/main/ets/MainAbility/MainAbility.ets)
55  中通过WorkSchedulerSystem.handleNotification()方法调用Notification.subscribe()接口获取信息,根据信息内容决定是否提示用户升级;
56  * 安装更新包:在[WorkSchedulerAbility.ets](https://gitee.com/openharmony/applications_app_samples/blob/master/code/BasicFeature/TaskManagement/WorkScheduler/entry/src/main/ets/WorkSchedulerAbility/WorkSchedulerAbility.ets)
57  通过WorkSchedulerSystem.installBundle()方法实现,首先调用bundle.getBundleInstaller()获取Installer对象,然后调用bundleInstall.install()接口实现装包,完成升级。
58
59### 相关权限
60
61[ohos.permission.INTERNET](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/security/permission-list.md#ohospermissioninternet)
62
63[ohos.permission.INSTALL_BUNDLE](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/security/permission-list.md#ohospermissioninstall_bundle)
64
65[ohos.permission.NOTIFICATION_CONTROLLER](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/security/permission-list.md#ohospermissionnotification_controller)
66
67### 依赖
68
69不涉及。
70
71### 约束与限制
72
731. 本示例仅支持标准系统上运行。
74
752. 本示例仅支持API9版本SDK,本示例涉及使用系统接口:@ohos.bundle,需要手动替换Full
76  SDK才能编译通过,具体操作可参考[替换指南](https://docs.openharmony.cn/pages/v3.2/zh-cn/application-dev/quick-start/full-sdk-switch-guide.md/)77
783. 本示例需要使用DevEco Studio 3.1 Canary1 (Build Version: 3.1.0.100, built on November 3, 2022)才可编译运行。
79
804. 本示例所配置的权限ohos.permission.INSTALL_BUNDLEohos.permission.NOTIFICATION_CONTROLLER为system_core级别(
81  相关权限级别可通过[权限定义列表](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/security/permission-list.md)查看),需要手动配置对应级别的权限签名(具体操作可查看[自动化签名方案](https://developer.harmonyos.com/cn/docs/documentation/doc-guides/ohos-auto-configuring-signature-information-0000001271659465))82
83### 下载
84
85如需单独下载本工程,执行如下命令:
86
87    ```
88    git init
89    git config core.sparsecheckout true
90    echo code/BasicFeature/TaskManagement/WorkScheduler/ > .git/info/sparse-checkout
91    git remote add origin https://gitee.com/openharmony/applications_app_samples.git
92    git pull origin master
93    ```
94