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

面向对象思考

共 4391字,需浏览 9分钟

 ·

2021-07-13 16:49

秒表程序的功能当然是计时,但是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


作者著作介绍

《实战Python设计模式》是作者去年3月份出版的技术书籍,该书利用Python 的标准GUI 工具包tkinter,通过可执行的示例对23 个设计模式逐个进行说明。这样一方面可以使读者了解真实的软件开发工作中每个设计模式的运用场景和想要解决的问题;另一方面通过对这些问题的解决过程进行说明,让读者明白在编写代码时如何判断使用设计模式的利弊,并合理运用设计模式。

对设计模式感兴趣而且希望随学随用的读者通过本书可以快速跨越从理解到运用的门槛;希望学习Python GUI 编程的读者可以将本书中的示例作为设计和开发的参考;使用Python 语言进行图像分析、数据处理工作的读者可以直接以本书中的示例为基础,迅速构建自己的系统架构。




觉得本文有帮助?请分享给更多人。

关注微信公众号【面向对象思考】轻松学习每一天!

面向对象开发,面向对象思考!





浏览 39
点赞
评论
收藏
分享

手机扫一扫分享

举报
评论
图片
表情
推荐
点赞
评论
收藏
分享

手机扫一扫分享

举报