前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >自学HarmonyOS应用开发(56)- 用Service保证应用在后台持续运行

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

作者头像
面向对象思考
发布2021-07-15 16:49:33
5260
发布2021-07-15 16:49:33
举报
秒表程序的功能当然是计时,但是Harmony应用的默认动作是切到后台之后程序会退出,无法实现连续计时。首先来看效果视频:

创建Service类

首先创建一个Ability的子类StopWatchService:

public class StopWatchService extends Ability {
    private static final int NOTIFICATION_ID = 0XD0000002;
    private static final String TAG = StopWatchService.class.getSimpleName();
    private static final HiLogLabel LABEL_LOG = new HiLogLabel(3, 0x00209, TAG);
    private static final String DESCRIPTOR = "xwg.harmony.stopwatch.StopWatchService";

    @Override
    public void onStart(Intent intent) {
        HiLog.info(LABEL_LOG, "StopWatchService.onStart!");
        startForeground();
        super.onStart(intent);
    }

    @Override
    public void onCommand(Intent intent, boolean restart, int startId) {
        HiLog.info(LABEL_LOG, "StopWatchService.onCommand!");
        super.onCommand(intent, restart, startId);
    }

    @Override
    public IRemoteObject onConnect(Intent intent) {
        HiLog.info(LABEL_LOG, "StopWatchService.onConnect!");
        return remoteAgentStub;
    }

    @Override
    public void onDisconnect(Intent intent) {
        HiLog.info(LABEL_LOG, "StopWatchService.onDisconnect!");
        super.onDisconnect(intent);
    }

    @Override
    public void onStop() {
        HiLog.info(LABEL_LOG, "StopWatchService.onStop()<<<<<<<<<<<<<<<<<!");
        super.onStop();
        cancelBackgroundRunning();
    }

    private void startForeground() {
        NotificationRequest request = new NotificationRequest(NOTIFICATION_ID).setTapDismissed(true);
        NotificationRequest.NotificationNormalContent content = new NotificationRequest.NotificationNormalContent();
        content.setTitle("秒表服务").setText("前台服务运行中...");
        NotificationRequest.NotificationContent notificationContent = new NotificationRequest.NotificationContent(
                content);
        request.setContent(notificationContent);
        keepBackgroundRunning(NOTIFICATION_ID, request);
    }
}

关于重写的几个方法的详细信息,请参照文后链接【创建Service】;startForground和cancelBackgroundRunning方法用于开启和关闭前台Service模式,具体说明请参照文后链接【前台Service】,这里不再一一赘述。

注册Service类

修改config.json文件,增加StopWatchService相关内容。

"module": {
  "abilities": [
    {
      "backgroundModes": [
        "dataTransfer",
        "location"
      ],
      "name": "xwg.harmony.stopwatch.StopWatchService",
      "icon": "$media:icon",
      "description": "$string:stopwatchservice_description",
      "type": "service",
      "visible": true
    }
  ],
}

最主要的是type信息。

启动Service

在需要启动StopWatchService的画面中调用下面的startLocalService方法即可启动Service。两个常量就是我们启动StopWatchService时需要的参数。

private static final String LOCAL_BUNDLE = "xwg.harmony.stopwatch";
private static final String FOREGROUND_SERVICE = "StopWatchService";

private void startLocalService(String bundleName, String serviceName) {
    Intent intent = getLocalServiceIntent(LOCAL_BUNDLE, FOREGROUND_SERVICE);
    startAbility(intent);
}

private Intent getLocalServiceIntent(String bundleName, String serviceName) {
        Operation operation = new Intent.OperationBuilder().withDeviceId("")
                .withBundleName(bundleName)
                .withAbilityName(serviceName)
                .build();
        Intent intent = new Intent();
        intent.setOperation(operation);
        return intent;
    }

具体说明请参照文后链接【启动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

https://developer.harmonyos.com/cn/docs/documentation/doc-guides/ability-service-creating-0000000000044464

前台Service

https://developer.harmonyos.com/cn/docs/documentation/doc-guides/ability-service-foreground-0000000000044473

启动Service

https://developer.harmonyos.com/cn/docs/documentation/doc-guides/ability-service-starting-0000000000044465

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2021-07-11,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 面向对象思考 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档