当前位置:网站首页>Self-study HarmonyOS application development (56) - Use Service to ensure that the application runs continuously in the background
Self-study HarmonyOS application development (56) - Use Service to ensure that the application runs continuously in the background
2022-07-30 00:54:00 【51CTO】
秒表程序的功能当然是计时,但是Harmony应用的默认动作是切到后台之后程序会退出,无法实现连续计时.首先来看效果视频:
创建Service类
首先创建一个Ability的子类StopWatchService:
关于重写的几个方法的详细信息,请参照文后链接【创建Service】;startForground和cancelBackgroundRunning方法用于开启和关闭前台Service模式,具体说明请参照文后链接【前台Service】,这里不再一一赘述.
注册Service类
修改config.json文件,增加StopWatchService相关内容.
最主要的是type信息.
启动Service
在需要启动StopWatchService的画面中调用下面的startLocalService方法即可启动Service.两个常量就是我们启动StopWatchService时需要的参数.
具体说明请参照文后链接【启动Service】.
参考代码
完整代码可以从以下链接下载:
https://github.com/xueweiguo/Harmony/tree/master/StopWatch
参考资料:
Service Ability基本概念
https://developer.harmonyos.com/cn/docs/documentation/doc-
guides/ability-service-concept-0000000000044457
创建Service
前台Service
启动Service
作者著作介绍
《实战Python设计模式》是作者去年3月份出版的技术书籍,该书利用Python 的标准GUI 工具包tkinter,通过可执行的示例对23 个设计模式逐个进行说明.这样一方面可以使读者了解真实的软件开发工作中每个设计模式的运用场景和想要解决的问题;另一方面通过对这些问题的解决过程进行说明,让读者明白在编写代码时如何判断使用设计模式的利弊,并合理运用设计模式.

对设计模式感兴趣而且希望随学随用的读者通过本书可以快速跨越从理解到运用的门槛;希望学习Python GUI 编程的读者可以将本书中的示例作为设计和开发的参考;使用Python 语言进行图像分析、数据处理工作的读者可以直接以本书中的示例为基础,迅速构建自己的系统架构.
觉得本文有帮助?请分享给更多人.
关注微信公众号【面向对象思考】轻松学习每一天!
面向对象开发,面向对象思考!

边栏推荐
- 循环神经网络(RNN)
- STM32 - OLED display experiment
- MySql的初识感悟,以及sql语句中的DDL和DML和DQL的基本语法
- 华为“天才少年”稚晖君又出新作,从零开始造“客制化”智能键盘
- 软考 --- 数据库(5)数据库控制
- Navicat for mysql破解版安装
- 基于TNEWS‘ 今日头条中文新闻(短文本)分类
- Weekly recommended short video: What is R&D efficiency?It can achieve anti "involution"?
- Chinese semantic matching
- How to realize the frame selection of objects in canvas (6)
猜你喜欢

3 tips for using hot events to create press releases?A must-see for self-media people

string replace spaces

Worthington优化技术:细胞定量

机器人的运动范围

利用热点事件来创作软文的3大技巧?自媒体人必看

Detailed explanation of nacos cluster configuration

自学HarmonyOS应用开发(56)- 用Service保证应用在后台持续运行

工厂模式

How to increase account weight?3 ways to operate your own media to help you get more revenue

Mysql internal and external connections
随机推荐
重新定义分析 - EventBridge 实时事件分析平台发布
Worthington dissociating enzyme: detailed analysis of neutral protease (dispase)
Replace the executable file glibc version of the one
Low dropout linear regulator MPQ2013A-AEC1 brand MPS domestic replacement
Selenium上传文件有多少种方式?不信你有我全
[Flutter] Flutter preloading of mixed development solves the problem of slow page loading for the first time
百度智能云章淼:详解企业级七层负载均衡开源软件BFE
Since the media how to write a short video title?Three hot style title, let your video gain more traffic
Douyin short video traffic acquisition strategy, mastering these will definitely be a hit
【MySQL系列】MySQL数据库基础
[Experience] Experience Summary - Lessons Learned
Worthington解离酶:胰蛋白酶及常见问题
Navicat for mysql破解版安装
遇到bug的解决办法,测试再也不背锅了
Print linked list from end to beginning
自媒体短视频怎么提高播放量?从这三个方面入手
Introduction to Worthington Elastase & Hyaluronidase
【集训DAY16】KC‘s Can 【动态规划】
docker安装redis集群(含部署脚本)
中文语义匹配