当前位置:   article > 正文

华为Harmony鸿蒙开发笔记四:ServiceAbility使用_鸿蒙serviceability

鸿蒙serviceability

按照上图创建ServiceAbility,会自动生成带有生命周期的类和配置文件:

  1. package com.example.serviceabilitydemo;
  2. import ohos.aafwk.ability.Ability;
  3. import ohos.aafwk.content.Intent;
  4. import ohos.rpc.IRemoteObject;
  5. import ohos.hiviewdfx.HiLog;
  6. import ohos.hiviewdfx.HiLogLabel;
  7. public class ServiceAbility extends Ability {
  8. private static final HiLogLabel LABEL_LOG = new HiLogLabel(3, 0xD001100, "Demo");
  9. @Override
  10. public void onStart(Intent intent) {
  11. HiLog.error(LABEL_LOG, "ServiceAbility::onStart");
  12. super.onStart(intent);
  13. }
  14. @Override
  15. public void onBackground() {
  16. super.onBackground();
  17. HiLog.info(LABEL_LOG, "ServiceAbility::onBackground");
  18. }
  19. @Override
  20. public void onStop() {
  21. super.onStop();
  22. HiLog.info(LABEL_LOG, "ServiceAbility::onStop");
  23. }
  24. @Override
  25. public void onCommand(Intent intent, boolean restart, int startId) {
  26. }
  27. @Override
  28. public IRemoteObject onConnect(Intent intent) {
  29. return null;
  30. }
  31. @Override
  32. public void onDisconnect(Intent intent) {
  33. }
  34. }
  1. {
  2. "name": "com.example.serviceabilitydemo.ServiceAbility",
  3. "icon": "$media:icon",
  4. "description": "$string:serviceability_description",
  5. "type": "service",
  6. "visible": true
  7. }

启动ServiceAbility跟跳转PageAbility的代码一模一样:

  1. btnStartAbility.setClickedListener(new Component.ClickedListener() {
  2. @Override
  3. public void onClick(Component component) {
  4. Intent intent = new Intent();
  5. Operation operation = new Intent.OperationBuilder()
  6. .withDeviceId("")
  7. .withBundleName("com.example.serviceabilitydemo")
  8. .withAbilityName("com.example.serviceabilitydemo.ServiceAbility")
  9. .build();
  10. intent.setOperation(operation);
  11. startAbility(intent);
  12. }
  13. });

从启动到结束的生命周期:

  1. 12-28 21:49:00.645 3798-3798/com.example.serviceabilitydemo E 01100/MyLog: ServiceAbility::onStart
  2. 12-28 21:49:00.646 3798-3798/com.example.serviceabilitydemo E 01100/MyLog: ServiceAbility::onCommand
  3. 12-28 21:49:13.521 3800-3800/? E 01100/MyLog: ServiceAbility::onStart
  4. 12-28 21:49:13.524 3800-3800/? E 01100/MyLog: ServiceAbility::onBackground
  5. 12-28 21:49:13.524 3800-3800/com.example.serviceabilitydemo E 01100/MyLog: ServiceAbility::onStop

onStart方法居然执行了两次,官方文档是这样的

由于我是用杀进程的方式停止Service的,于是我又试了试用stopAbility来停止Service:

  1. Button btnStartAbility= (Button) findComponentById(ResourceTable.Id_btn_start_service);
  2. if (btnStartAbility != null) {
  3. // 为按钮设置点击回调
  4. btnStartAbility.setClickedListener(new Component.ClickedListener() {
  5. @Override
  6. public void onClick(Component component) {
  7. Intent intent = new Intent();
  8. Operation operation = new Intent.OperationBuilder()
  9. .withDeviceId("")
  10. .withBundleName("com.example.serviceabilitydemo")
  11. .withAbilityName("com.example.serviceabilitydemo.ServiceAbility")
  12. .build();
  13. intent.setOperation(operation);
  14. startAbility(intent);
  15. }
  16. });
  17. }
  18. Button btnStopAbility= (Button) findComponentById(ResourceTable.Id_btn_stop_service);
  19. if (btnStopAbility != null) {
  20. // 为按钮设置点击回调
  21. btnStopAbility.setClickedListener(new Component.ClickedListener() {
  22. @Override
  23. public void onClick(Component component) {
  24. Intent intent = new Intent();
  25. Operation operation = new Intent.OperationBuilder()
  26. .withDeviceId("")
  27. .withBundleName("com.example.serviceabilitydemo")
  28. .withAbilityName("com.example.serviceabilitydemo.ServiceAbility")
  29. .build();
  30. intent.setOperation(operation);
  31. stopAbility(intent);
  32. }
  33. });
  34. }

这次是正常的:

  1. 12-28 21:55:12.115 28321-28321/com.example.serviceabilitydemo E 01100/MyLog: ServiceAbility::onStart
  2. 12-28 21:55:12.116 28321-28321/com.example.serviceabilitydemo E 01100/MyLog: ServiceAbility::onCommand
  3. 12-28 21:55:18.776 28321-28321/com.example.serviceabilitydemo E 01100/MyLog: ServiceAbility::onBackground
  4. 12-28 21:55:18.776 28321-28321/com.example.serviceabilitydemo E 01100/MyLog: ServiceAbility::onStop

不知道为什么杀进程会多执行一次onStart方法。

最后,连接ServiceAbility,先定义一个用来传递的类,这个类必须继承LocalRemoteObject:

  1. package com.example.serviceabilitydemo;
  2. import ohos.aafwk.ability.LocalRemoteObject;
  3. public class MyRemoteObject extends LocalRemoteObject {
  4. private String msg;
  5. public MyRemoteObject() {
  6. super();
  7. }
  8. public MyRemoteObject(String msg) {
  9. super();
  10. this.msg = msg;
  11. }
  12. public String getMsg() {
  13. return msg;
  14. }
  15. public void setMsg(String msg) {
  16. this.msg = msg;
  17. }
  18. }

连接:

  1. Button connStopAbility= (Button) findComponentById(ResourceTable.Id_btn_conn_service);
  2. if (connStopAbility != null) {
  3. // 为按钮设置点击回调
  4. connStopAbility.setClickedListener(new Component.ClickedListener() {
  5. @Override
  6. public void onClick(Component component) {
  7. Intent intent = new Intent();
  8. Operation operation = new Intent.OperationBuilder()
  9. .withDeviceId("")
  10. .withBundleName("com.example.serviceabilitydemo")
  11. .withAbilityName("com.example.serviceabilitydemo.ServiceAbility")
  12. .build();
  13. intent.setOperation(operation);
  14. // 连接Service
  15. connectAbility(intent, connection);
  16. }
  17. });
  18. }

其中connection是实现了IAbilityConnection接口的内部类

  1. // 创建连接回调实例
  2. private IAbilityConnection connection = new IAbilityConnection() {
  3. // 连接到Service的回调
  4. @Override
  5. public void onAbilityConnectDone(ElementName elementName, IRemoteObject iRemoteObject, int resultCode) {
  6. // Client侧需要定义与Service侧相同的IRemoteObject实现类。
  7. // 开发者获取服务端传过来IRemoteObject对象,并从中解析出服务端传过来的信息。
  8. MyRemoteObject remoteObject=(MyRemoteObject)iRemoteObject;
  9. remoteObject.getMsg();
  10. HiLog.error(LABEL_LOG, "onAbilityConnectDone() msg="+remoteObject.getMsg());
  11. }
  12. // 断开与连接的回调
  13. @Override
  14. public void onAbilityDisconnectDone(ElementName elementName, int resultCode) {
  15. }
  16. };

Service中要在onConnect方法返回数据:

  1. @Override
  2. public IRemoteObject onConnect(Intent intent) {
  3. HiLog.error(LABEL_LOG, "ServiceAbility::onConnect");
  4. return new MyRemoteObject("我是从服务器端返回的消息");
  5. }

 

官方文档上还有这样一个流程:

 

但是我一旦执行连接ServiceAbility,就无法在执行停止ServiceAbility了,杀进程也不行。

Demo:https://download.csdn.net/download/y280903468/13973575

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/313513
推荐阅读
相关标签
  

闽ICP备14008679号