Page Ablity响应交互,从而调用Service Ability是鸿蒙开发中最常用的交互方式,在Page Ablity使用startAbility(intent)启动Service Ability,Service Ability端在onCommend做出响应。
Page Ablity:
- Intent intent = new Intent();
- Operation operation = new Intent.OperationBuilder()
- .withDeviceId("")
- .withBundleName("mytest.app")
- .withAbilityName("com.example.album.Ability.service.PlayServiceAbility")
- .build();
- intent.setOperation(operation);
- intent.setParam("item",item);
- // 启动Service
- context.startAbility(intent,0);
复制代码
Service Ability:
- @Override
- protected void onCommand(Intent intent, boolean restart, int startId) {
- super.onCommand(intent, restart, startId);
- HiLog.info(TAG, "发布公共事件");
- //这里写响应内容
-
- }
复制代码
本文主要根据官方文档学习体验整理