Skip to content
Menu
(◍ ´꒳` ◍)

  • Blog
  • Front Page
(◍ ´꒳` ◍)

鸿蒙47前台 Service

By admin on 2021年10月8日2021年10月8日

前台 Service 一般情况下,Service 都是在后台运行的,后台 Service 的优先级都是比较低的,当资源 不足时,系统有可能回收正在运行的后台 Service。 在一些场景下(如播放音乐),用户希望应用能够一直保持运行,此时就需要使用前台 Service。前台 Service 会始终保持正在运行的图标在系统状态栏显示。 使用前台 Service 并不复杂,开发者只需在 Service 创建的方法里,调用 keepBackgroundRunning()将 Service 与通知绑定。调用 keepBackgroundRunning()方法前 需要在配置文件中声明 ohos.permission.KEEP_BACKGROUND_RUNNING 权限,同时还需要 在配置文件中添加对应的 backgroundModes 参数。在 onStop()方法中调用 cancelBackgroundRunning()方法可停止前台 Service。 使用前台 Service 的 onStart()代码示例如下

// 创建通知,其中 1005 为 notification通知Id NotificationRequest请求 request = new NotificationRequest(1005); NotificationRequest.NotificationNormal正常Content内容 content = new NotificationRequest.NotificationNormalContent(); content.setTitle(“title”).setText(“text”); NotificationRequest.NotificationContent notificationContent = new NotificationRequest.NotificationContent(content); request.setContent(notificationContent); // 绑定通知,1005 为创建通知时传入的 notificationId keepBackground地Running(1005, request);

在配置文件中,module > abilities 字段下对当前 Service 做如下配置: { “name”: “.ServiceAbility”, “type”: “service”, “visible”: true, “backgroundModes”: [“dataTransfer”转移, “location”位置] }

Category: 鸿蒙

文章导航

鸿蒙46Service Ability 生命周期
鸿蒙48Data Ability 基本概念

Related Posts

Docker进阶:redis集群3主3从003实战哈希槽分布式

9月 22, 2023
Read More

鸿蒙51实现 UserDataAbilit

10月 10, 2021
Read More

git分支

9月 2, 2023
Read More

发表回复 取消回复

您的电子邮箱地址不会被公开。 必填项已用*标注

近期文章

  • 实施002
  • 实施SQL001
  • 泛微oa001-字段
  • 硬件前端-海康威视摄像头
  • 使用Docker搭建Weblogic服务001

近期评论

    归档

    • 2023年10月
    • 2023年9月
    • 2023年8月
    • 2023年5月
    • 2023年4月
    • 2023年3月
    • 2021年11月
    • 2021年10月
    • 2021年9月
    • 2021年8月
    • 2021年5月

    分类

    • C
    • docker
    • iptable
    • ISIS
    • java
    • k8s
    • liunx
    • OpenCV py
    • ospf
    • solr
    • zabbix
    • 劳动法
    • 周杰伦
    • 格林法则
    • 等保
    • 鸿蒙

    其他操作

    • 登录
    • 条目feed
    • 评论feed
    • WordPress.org
    ©2025 (◍ ´꒳` ◍) | WordPress Theme by Superb WordPress Themes