赞
踩
本示例使用[@ohos.WorkSchedulerExtensionAbility] 、[@ohos.net.http]、[@ohos.notification] 、[@ohos.bundle]、[@ohos.fileio] 等接口,实现了设置后台任务、下载更新包 、保存更新包、发送通知 、安装更新包实现升级的功能。
使用说明
hdc shell aa test -b ohos.samples.workschedulerextensionability -m entry_test -s unittest OpenHarmonyTestRunner -s class ActsAbilityTest -s timeout 150000
- entry/src/main/ets/
- |---Application
- | |---MyAbilityStage.ets // 入口文件
- |---feature
- | |---WorkSchedulerSystem.ets // 封装各个功能接口
- |---MainAbility
- | |---MainAbility.ets // 请求权限
- |---pages
- | |---Index.ets // 首页
- |---util
- | |---Logger.ets // 日志文件
- |---WorkSchedulerAbility
- | |---WorkSchedulerAbility.ets // 延时任务触发后的回调
鸿蒙next开发知识更新在:gitee.com/li-shizhen-skin/harmony-os/blob/master/README.md
- /*
- * Copyright (c) 2023-2024 Huawei Device Co., Ltd.
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-
-
- import fs from '@ohos.file.fs';
-
- import notificationManager from '@ohos.notificationManager';
-
- import Notification from '@ohos.notification';
-
- import bundle from '@ohos.bundle.installer';
-
- import account from '@ohos.account.osAccount';
-
- import workScheduler from '@ohos.resourceschedule.workScheduler';
-
- import http from '@ohos.net.http';
-
- import { Logger } from '../utils/Logger';
-
-
-
- const FILE_NAME = '/UpdateWorkScheduler.hap';
-
- const BUNDLE_NAMES = ['ohos.samples.workschedulerextensionability'];
-
- const INSTALL_PARAMETER = 1;
-
-
-
- export namespace WorkSchedulerSystem {
-
- /**
- * Store the file to the specified directory.
- *
- * @param pathDir Path to save the file.
- * @param content The contents of the file to be saved.
- */
-
- export function saveFile(pathDir: string, content: ArrayBuffer): void {
-
- try {
-
- let filePath = pathDir + FILE_NAME;
-
- let fd = fs.openSync(filePath, 0o2 | 0o100).fd;
-
- fs.writeSync(fd, content);
-
- fs.closeSync(fd);
-
- } catch (err) {
-
- Logger.error(`saveFile failed, code is ${err.code}, message is ${err.message}`);
-
- }
-
- }
-
-
-
- /**
- * Sending a Notification.
- *
- * @param bundleName Check the name of the application that has permission.
- * @permission ohos.permission.NOTIFICATION_CONTROLLER
- */
-
- export async function handleNotification(bundleName: string): Promise<void> {
-
- await notificationManager.requestEnableNotification();
-
- Notification.subscribe({
-
- onConsume: (data) => {
-
- if (data.request.content.normal.text === 'isReady') {
-
- AppStorage.SetOrCreate('isShowDialog', true);
-
- }
-
- }
-
- }, {
-
- bundleNames: BUNDLE_NAMES
-
- })
-
- }
-
-
-
- /**
- * Publishes a notification of the specified content.
- *
- * @param title Title of Notice.
- * @param text Content of Notification Text.
- * @param additionalText Additional text.
- * @permission ohos.permission.NOTIFICATION_CONTROLLER
- */
-
- export function publishNotification(title: string, text: string, additionalText: string): void {
-
- notificationManager.publish({
-
- content: {
-
- contentType: Notification.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT,
-
- normal: {
-
- title,
-
- text,
-
- additionalText
-
- }
-
- }
-
- })
-
- }
-
-
-
- /**
- * Install the application package in the specified path.
- *
- * @param filePath An array of paths to hold the installation package.
- * @permission ohos.permission.INSTALL_BUNDLE
- */
-
- export async function installBundle(filePath: Array<string>): Promise<void> {
-
- try {
-
- let bundleInstall = await bundle.getBundleInstaller();
-
- let userId = await account.getAccountManager().getOsAccountLocalIdFromProcess();
-
- bundleInstall.install(filePath, {
-
- userId: userId,
-
- installFlag: INSTALL_PARAMETER,
-
- isKeepData: false
-
- }, (status, statusMessage) => {
-
- Logger.info(`installBundle filepath is ${filePath}`);
-
- Logger.info(`installBundle code is ${status.code}, message is ${JSON.stringify(statusMessage)}`);
-
- })
-
- } catch (err) {
-
- Logger.error(`installBundle failed, code is ${err.code}, message is ${err.message}`);
-
- }
-
- }
-
-
-
- /**
- * Register the delayed task and pass the parameters.
- *
- * @param version Current application version.
- * @param bundleName The name of the application package for which the task needs to be registered.
- * @param filePath Storage address of the application package.
- */
-
- export async function startUpdateSample(version: string, bundleName: string, filePath: string): Promise<void> {
-
- try {
-
- let workInfo = {
-
- workId: 1,
-
- bundleName: bundleName,
-
- abilityName: 'WorkSchedulerAbility',
-
- networkType: workScheduler.NetworkType.NETWORK_TYPE_WIFI,
-
- parameters: {
-
- version: version,
-
- filePath: filePath
-
- }
-
- };
-
- workScheduler.startWork(workInfo);
-
- }
-
- catch (err) {
-
- Logger.error(`startWork failed, code is ${err.code}, message is ${err.message}`);
-
- }
-
- }
-
-
-
- /**
- * Register the delayed task and pass the parameters.
- *
- * @param url Url of the application package.
- * @permission ohos.permission.INTERNET
- */
-
- export async function getNewHap(url: string): Promise<http.HttpResponse> {
-
- try {
-
- return await http.createHttp().request(
-
- url,
-
- {
-
- expectDataType: http.HttpDataType.ARRAY_BUFFER
-
- });
-
- } catch (err) {
-
- Logger.error(`get result failed, code is ${err.code}, message is ${err.message}`);
-
- }
-
- }
-
- }
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
而网上有关鸿蒙的开发资料非常的少,假如你想学好鸿蒙的应用开发与系统底层开发。你可以参考这份资料,少走很多弯路,节省没必要的麻烦。由两位前阿里高级研发工程师联合打造的《鸿蒙NEXT星河版OpenHarmony开发文档》里面内容包含了(ArkTS、ArkUI开发组件、Stage模型、多端部署、分布式应用开发、音频、视频、WebGL、OpenHarmony多媒体技术、Napi组件、OpenHarmony内核、Harmony南向开发、鸿蒙项目实战等等)鸿蒙(Harmony NEXT)技术知识点
如果你是一名Android、Java、前端等等开发人员,想要转入鸿蒙方向发展。可以直接领取这份资料辅助你的学习。下面是鸿蒙开发的学习路线图。
高清完整版请点击→《鸿蒙NEXT星河版开发学习文档》
针对鸿蒙成长路线打造的鸿蒙学习文档。话不多说,我们直接看详细资料鸿蒙(OpenHarmony )学习手册(共计1236页)与鸿蒙(OpenHarmony )开发入门教学视频,帮助大家在技术的道路上更进一步。
《鸿蒙 (OpenHarmony)开发学习视频》
《鸿蒙生态应用开发V2.0白皮书》
《鸿蒙 (OpenHarmony)开发基础到实战手册》
获取这份鸿蒙星河版学习资料,请点击→《鸿蒙NEXT星河版开发学习文档》
OpenHarmony北向、南向开发环境搭建
《鸿蒙开发基础》
ArkTS语言
安装DevEco Studio
运用你的第一个ArkTS应用
ArkUI声明式UI开发
.……
《鸿蒙开发进阶》
Stage模型入门
网络管理
数据管理
电话服务
分布式应用开发
通知与窗口管理
多媒体技术
安全技能
任务管理
WebGL
国际化开发
应用测试
DFX面向未来设计
鸿蒙系统移植和裁剪定制
……
《鸿蒙开发实战》
ArkTS实践
UIAbility应用
网络案例
……
获取这份鸿蒙星河版学习资料,请点击→《鸿蒙NEXT星河版开发学习文档》
鸿蒙—作为国家主力推送的国产操作系统。部分的高校已经取消了安卓课程,从而开设鸿蒙课程;企业纷纷跟进启动了鸿蒙研发。
并且鸿蒙是完全具备无与伦比的机遇和潜力的;预计到年底将有 5,000 款的应用完成原生鸿蒙开发,未来将会支持 50 万款的应用。那么这么多的应用需要开发,也就意味着需要有更多的鸿蒙人才。鸿蒙开发工程师也将会迎来爆发式的增长,学习鸿蒙势在必行!
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。