自學(xué)HarmonyOS應(yīng)用開發(fā)(56)- 用Service保證應(yīng)用在后臺(tái)持續(xù)運(yùn)行

秒表程序的功能當(dāng)然是計(jì)時(shí),但是Harmony應(yīng)用的默認(rèn)動(dòng)作是切到后臺(tái)之后程序會(huì)退出,無法實(shí)現(xiàn)連續(xù)計(jì)時(shí)。首先來看效果視頻:
創(chuàng)建Service類
首先創(chuàng)建一個(gè)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";public void onStart(Intent intent) {HiLog.info(LABEL_LOG, "StopWatchService.onStart!");startForeground();super.onStart(intent);}public void onCommand(Intent intent, boolean restart, int startId) {HiLog.info(LABEL_LOG, "StopWatchService.onCommand!");super.onCommand(intent, restart, startId);}public IRemoteObject onConnect(Intent intent) {HiLog.info(LABEL_LOG, "StopWatchService.onConnect!");return remoteAgentStub;}public void onDisconnect(Intent intent) {HiLog.info(LABEL_LOG, "StopWatchService.onDisconnect!");super.onDisconnect(intent);}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("秒表服務(wù)").setText("前臺(tái)服務(wù)運(yùn)行中...");NotificationRequest.NotificationContent notificationContent = new NotificationRequest.NotificationContent(content);request.setContent(notificationContent);keepBackgroundRunning(NOTIFICATION_ID, request);}}
關(guān)于重寫的幾個(gè)方法的詳細(xì)信息,請(qǐng)參照文后鏈接【創(chuàng)建Service】;startForground和cancelBackgroundRunning方法用于開啟和關(guān)閉前臺(tái)Service模式,具體說明請(qǐng)參照文后鏈接【前臺(tái)Service】,這里不再一一贅述。
注冊(cè)Service類
修改config.json文件,增加StopWatchService相關(guān)內(nèi)容。
"module": {"abilities": [{"backgroundModes": ["dataTransfer","location"],"name": "xwg.harmony.stopwatch.StopWatchService","icon": "$media:icon","description": "$string:stopwatchservice_description","type": "service","visible": true}],}
最主要的是type信息。
啟動(dòng)Service
在需要啟動(dòng)StopWatchService的畫面中調(diào)用下面的startLocalService方法即可啟動(dòng)Service。兩個(gè)常量就是我們啟動(dòng)StopWatchService時(shí)需要的參數(shù)。
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;}
具體說明請(qǐng)參照文后鏈接【啟動(dòng)Service】。
參考代碼
完整代碼可以從以下鏈接下載:
https://github.com/xueweiguo/Harmony/tree/master/StopWatch
參考資料:
Service Ability基本概念
https://developer.harmonyos.com/cn/docs/documentation/doc-
guides/ability-service-concept-0000000000044457
創(chuàng)建Service
https://developer.harmonyos.com/cn/docs/documentation/doc-guides/ability-service-creating-0000000000044464
前臺(tái)Service
https://developer.harmonyos.com/cn/docs/documentation/doc-guides/ability-service-foreground-0000000000044473
啟動(dòng)Service
https://developer.harmonyos.com/cn/docs/documentation/doc-guides/ability-service-starting-0000000000044465
作者著作介紹
《實(shí)戰(zhàn)Python設(shè)計(jì)模式》是作者去年3月份出版的技術(shù)書籍,該書利用Python 的標(biāo)準(zhǔn)GUI 工具包tkinter,通過可執(zhí)行的示例對(duì)23 個(gè)設(shè)計(jì)模式逐個(gè)進(jìn)行說明。這樣一方面可以使讀者了解真實(shí)的軟件開發(fā)工作中每個(gè)設(shè)計(jì)模式的運(yùn)用場景和想要解決的問題;另一方面通過對(duì)這些問題的解決過程進(jìn)行說明,讓讀者明白在編寫代碼時(shí)如何判斷使用設(shè)計(jì)模式的利弊,并合理運(yùn)用設(shè)計(jì)模式。

對(duì)設(shè)計(jì)模式感興趣而且希望隨學(xué)隨用的讀者通過本書可以快速跨越從理解到運(yùn)用的門檻;希望學(xué)習(xí)Python GUI 編程的讀者可以將本書中的示例作為設(shè)計(jì)和開發(fā)的參考;使用Python 語言進(jìn)行圖像分析、數(shù)據(jù)處理工作的讀者可以直接以本書中的示例為基礎(chǔ),迅速構(gòu)建自己的系統(tǒng)架構(gòu)。
覺得本文有幫助?請(qǐng)分享給更多人。
關(guān)注微信公眾號(hào)【面向?qū)ο笏伎肌枯p松學(xué)習(xí)每一天!
面向?qū)ο箝_發(fā),面向?qū)ο笏伎迹?/span>
