赞
踩
本文描述的后台任务特指应用或业务模块处于后台(无可见界面)时,有需要继续执行或者后续执行的业务。对这些应用不可见但要继续或者将要执行的业务动作,为避免后台任务调度和管控对业务执行的影响,HarmonyOS将后台任务分为三种类型:
退到后台的应用有不可中断且短时间能完成的任务时,可以使用短时任务机制,该机制允许应用在后台短时间内完成任务,保障应用业务运行不受后台生命周期管理的影响。
短时任务的使用需要遵从如下约束和规则:
长驻任务类型给用户能直观感知到的且需要一直在后台运行的业务提供后台运行生命周期的保障。比如,业务需要在后台播放声音,或者需要在后台持续导航定位等,此类用户可以感知到的后台业务行为,可以通过使用长驻任务对应的后台模式保障业务在后台的运行,支撑应用完成在后台的业务。
HarmonyOS提供了十种后台模式,供需要在后台做长驻任务的业务使用,具体的后台模式类型如下:
长驻任务后台模式 | 英文名 | 描述 |
---|---|---|
数据传输 | data-transfer | 通过网络/对端设备进行数据下载、备份、分享、传输等业务 |
播音 | audio-playback | 音频输出业务 |
录音 | audio-recording | 音频输入业务 |
画中画 | picture-in-picture | 画中画、小窗口播放视频业务 |
音视频通话 | voip | 音视频电话,VoIP业务 |
导航/位置更新 | location | 定位、导航业务 |
蓝牙设备连接及传输 | bluetooth-interaction | 蓝牙扫描、连接、传输业务 |
WLAN设备连接及传输 | wifi-interaction | WLAN扫描、连接、传输业务 |
屏幕抓取 | screen-fetch | 录屏、截屏业务 |
多设备互联 | multiDeviceConnection | 多设备互联,分布式调度和迁移等业务 |
业务根据需要选择对应的后台模式以后,会在应用的config.json文件中新创建的ServiceAbility组件下生成对应选择的后台模式配置,如下图所示:
说明
只有ServiceAbility才有对应的后台模式类型选择和配置。
调用keepBackgroundRunning()方法前需要在配置文件中声明ohos.permission.KEEP_BACKGROUND_RUNNING权限。
完成对应的后台业务以后,在销毁服务的方法中调用cancelBackgroundRunning()方法,即可停止使用长驻任务。
请参考:前台Service。
托管任务是系统提供的一种后台代理机制。通过系统提供的代理API接口,用户可以把任务(如后台下载、定时提醒、后台非持续定位)交由系统托管。
- <?xml version="1.0" encoding="utf-8"?>
- <DirectionalLayout
- xmlns:ohos="http://schemas.huawei.com/res/ohos"
- ohos:height="match_parent"
- ohos:width="match_parent"
- ohos:alignment="center"
- ohos:orientation="vertical">
-
- <Button
- ohos:id="$+id:btn_start"
- ohos:height="match_content"
- ohos:width="match_content"
- ohos:background_element="#A306F52E"
- ohos:layout_alignment="horizontal_center"
- ohos:text="启动服务"
- ohos:text_size="40vp"
- />
- <Button
- ohos:id="$+id:btn_stop"
- ohos:height="match_content"
- ohos:width="match_content"
- ohos:background_element="#A3235DEE"
- ohos:layout_alignment="horizontal_center"
- ohos:text="停止服务"
- ohos:text_size="40vp"
- />
- <Button
- ohos:id="$+id:btn_connect"
- ohos:height="match_content"
- ohos:width="match_content"
- ohos:background_element="#A306F52E"
- ohos:layout_alignment="horizontal_center"
- ohos:text="连接服务"
- ohos:text_size="40vp"
- />
- <Button
- ohos:id="$+id:btn_disconnect"
- ohos:height="match_content"
- ohos:width="match_content"
- ohos:background_element="#A3235DEE"
- ohos:layout_alignment="horizontal_center"
- ohos:text="断开连接服务"
- ohos:text_size="40vp"
- />
- <Button
- ohos:id="$+id:btn_foreground"
- ohos:height="match_content"
- ohos:width="match_content"
- ohos:background_element="#A306F52E"
- ohos:layout_alignment="horizontal_center"
- ohos:text="启动前台service"
- ohos:text_size="40vp"
- />
- <Button
- ohos:id="$+id:btn_foreground_stop"
- ohos:height="match_content"
- ohos:width="match_content"
- ohos:background_element="#A3235DEE"
- ohos:layout_alignment="horizontal_center"
- ohos:text="停止后台服务"
- ohos:text_size="40vp"
- />
- <TextField
- ohos:id="$+id:text_filed_info"
- ohos:height="300vp"
- ohos:background_element="#FC0A84EF"
- ohos:text="信息显示区域"
- ohos:width="350vp"
- ohos:hint=""
- ohos:margin="2vp"
- ohos:text_size="20vp"/>
- <Clock
- ohos:id="$+id:clock"
- ohos:height="match_content"
- ohos:width="match_content"
- ohos:background_element="#FF80EF66"
- ohos:layout_alignment="left"
-
- ohos:text_size="20vp"
- />
-
-
-
- </DirectionalLayout>
创建服务类
- package com.example.serviceability;
-
- import ohos.aafwk.ability.Ability;
- import ohos.aafwk.content.Intent;
- import ohos.event.notification.NotificationRequest;
- import ohos.rpc.IRemoteObject;
- import ohos.hiviewdfx.HiLog;
- import ohos.hiviewdfx.HiLogLabel;
-
- public class ForegroundServiceAbility extends Ability {
- private static final HiLogLabel LABEL_LOG = new HiLogLabel(3, 0xD001100, "Demo");
-
- @Override
- public void onStart(Intent intent) {
- HiLog.error(LABEL_LOG, "ForegroundServiceAbility::onStart");
- NotificationRequest.NotificationNormalContent content3 = new NotificationRequest.NotificationNormalContent()
- .setTitle("测试hap应用")
- .setText("该service将会常留后台");
- NotificationRequest.NotificationContent notificationContent = new NotificationRequest.NotificationContent(content3);
- NotificationRequest request3 = new NotificationRequest(1001).setContent(notificationContent);
- keepBackgroundRunning(1001,request3);
- super.onStart(intent);
-
- }
-
- @Override
- public void onBackground() {
- super.onBackground();
- HiLog.info(LABEL_LOG, "ForegroundServiceAbility::onBackground");
- }
-
- @Override
- public void onStop() {
- super.onStop();
- HiLog.info(LABEL_LOG, "ForegroundServiceAbility::onStop");
- }
-
- @Override
- public void onCommand(Intent intent, boolean restart, int startId) {
- }
-
- @Override
- public IRemoteObject onConnect(Intent intent) {
- return null;
- }
-
- @Override
- public void onDisconnect(Intent intent) {
- }
- }
- package com.example.serviceability;
-
- import ohos.aafwk.ability.Ability;
- import ohos.aafwk.ability.LocalRemoteObject;
- import ohos.aafwk.content.Intent;
- import ohos.rpc.IRemoteObject;
- import ohos.hiviewdfx.HiLog;
- import ohos.hiviewdfx.HiLogLabel;
-
- public class ServiceAbility extends Ability {
- private static final HiLogLabel LABEL_LOG = new HiLogLabel(3, 0xD001100, "Demo");
-
- @Override
- public void onStart(Intent intent) {
- HiLog.error(LABEL_LOG, "ServiceAbility::onStart");
- super.onStart(intent);
-
-
- }
-
- @Override
- public void onBackground() {
- super.onBackground();
- HiLog.info(LABEL_LOG, "ServiceAbility::onBackground");
- }
-
- @Override
- public void onStop() {
- super.onStop();
- HiLog.info(LABEL_LOG, "ServiceAbility::onStop");
- }
-
- @Override
- public void onCommand(Intent intent, boolean restart, int startId) {
- }
-
- @Override
- public IRemoteObject onConnect(Intent intent) {
- HiLog.info(LABEL_LOG,"OnConnect");
- return new MyRemoteObject();
-
- }
-
- @Override
- public void onDisconnect(Intent intent) {
- HiLog.info(LABEL_LOG,"断开服务");
- }
- public class MyRemoteObject extends LocalRemoteObject
- {
- public MyRemoteObject()
- {
- HiLog.info(LABEL_LOG,"my remoteobject被创建");
- }
- //自定义方法,控制service的
- public String manipulateService()
- {
- HiLog.info(LABEL_LOG,"自定义方法,控制service的");
- return "exec_value666";
- }
- }
-
- }
- package com.example.serviceability.slice;
-
- import com.example.serviceability.ForegroundServiceAbility;
- import com.example.serviceability.ResourceTable;
- import com.example.serviceability.ServiceAbility;
- import ohos.aafwk.ability.AbilitySlice;
- import ohos.aafwk.ability.IAbilityConnection;
- import ohos.aafwk.content.Intent;
- import ohos.aafwk.content.Operation;
- import ohos.agp.components.Button;
- import ohos.agp.components.Clock;
- import ohos.agp.components.Component;
- import ohos.agp.components.TextField;
- import ohos.bundle.ElementName;
- import ohos.rpc.IRemoteObject;
-
- public class MainAbilitySlice extends AbilitySlice {
- TextField textField;
- Clock clock2;
- @Override
- public void onStart(Intent intent) {
- super.onStart(intent);
- super.setUIContent(ResourceTable.Layout_ability_main);
-
- textField = (TextField) findComponentById(ResourceTable.Id_text_filed_info);
- clock2 = (Clock) findComponentById(ResourceTable.Id_clock);
- clock2.setFormatIn24HourMode("yyyy-MM-dd HH:mm:ss");
-
-
-
- Button btn_start =(Button) findComponentById(ResourceTable.Id_btn_start);
- Button btn_stop =(Button) findComponentById(ResourceTable.Id_btn_stop);
- Button btn_con =(Button) findComponentById(ResourceTable.Id_btn_connect);
- Button btn_discon =(Button) findComponentById(ResourceTable.Id_btn_disconnect);
- Button btn_foreground = (Button) findComponentById(ResourceTable.Id_btn_foreground);
- Button btn_foreground_stop = (Button) findComponentById(ResourceTable.Id_btn_foreground_stop);
- btn_foreground_stop.setClickedListener(new Component.ClickedListener() {
- @Override
- public void onClick(Component component) {
- StopForegoundService();
- textField.append(clock2.getText()+"已停止后台服务\n");
- }
- });
- btn_foreground.setClickedListener(new Component.ClickedListener() {
- @Override
- public void onClick(Component component) {
- StartForegoundService();
- textField.append(clock2.getText()+"启动后台服务\n");
- }
- });
- btn_start.setClickedListener(new Component.ClickedListener() {
- @Override
- public void onClick(Component component) {
- StartService();
- }
- });
- btn_stop.setClickedListener(new Component.ClickedListener() {
- @Override
- public void onClick(Component component) {
- StopService();
- }
- });
- btn_con.setClickedListener(new Component.ClickedListener() {
- @Override
- public void onClick(Component component) {
- ConnectService();
- }
- });
- btn_discon.setClickedListener(new Component.ClickedListener() {
- @Override
- public void onClick(Component component) {
- disconnectAbility(connection3);
- textField.append(clock2.getText()+"已断开服务\n");
- }
- });
-
- }
- public void StartService()
- {
- Intent intent1 = new Intent();
- Operation operation = new Intent.OperationBuilder()
- .withDeviceId("")
- .withBundleName("com.example.serviceability")
- .withAbilityName("com.example.serviceability.serviceability")
- .build();
- intent1.setOperation(operation);
- startAbility(intent1);
- }
- public void StopService()
- {
- Intent intent1 = new Intent();
- Operation operation = new Intent.OperationBuilder()
- .withDeviceId("")
- .withBundleName("com.example.serviceability")
- .withAbilityName("com.example.serviceability.serviceability")
- .build();
- intent1.setOperation(operation);
- stopAbility(intent1);
- }
- //连接服务
- public void ConnectService()
- {
- Intent intent1 = new Intent();
- Operation operation = new Intent.OperationBuilder()
- .withDeviceId("")
- .withBundleName("com.example.serviceability")
- .withAbilityName("com.example.serviceability.ServiceAbility")
- .build();
- intent1.setOperation(operation);
- connectAbility(intent1,connection3);
- }
-
- @Override
- public void onActive() {
- super.onActive();
- }
-
- @Override
- public void onForeground(Intent intent) {
- super.onForeground(intent);
- }
- //连接远程的Service的IAbilityConnection对象
- private IAbilityConnection connection3 = new IAbilityConnection() {
- //通过远程对象操纵Service
- @Override
- public void onAbilityConnectDone(ElementName elementName, IRemoteObject iRemoteObject, int i) {
- ServiceAbility.MyRemoteObject object3 = (ServiceAbility.MyRemoteObject) iRemoteObject;
- String aa = object3.manipulateService();
- textField.append(clock2.getText()+" 服务信息:"+aa);
- }
-
- @Override
- public void onAbilityDisconnectDone(ElementName elementName, int i) {
-
- }
- };
- public void StartForegoundService()
- {
- Intent intent1 = new Intent();
- Operation operation = new Intent.OperationBuilder()
- .withDeviceId("")
- .withBundleName("com.example.serviceability")
- .withAbilityName(ForegroundServiceAbility.class.getName())
- .build();
- intent1.setOperation(operation);
- startAbility(intent1);
- }
- public void StopForegoundService()
- {
- Intent intent1 = new Intent();
- Operation operation = new Intent.OperationBuilder()
- .withDeviceId("")
- .withBundleName("com.example.serviceability")
- .withAbilityName(ForegroundServiceAbility.class.getName())
- .build();
- intent1.setOperation(operation);
- stopAbility(intent1);
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。