创建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