十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
介绍通过startAbility()启动Service以及对应的停止方法。

创新互联专注于田阳网站建设服务及定制,我们拥有丰富的企业做网站经验。 热诚为您提供田阳营销型网站建设,田阳网站制作、田阳网页设计、田阳网站官网定制、成都微信小程序服务,打造田阳网络公司原创品牌,更为您提供田阳网站排名全网营销落地服务。
Ability为开发者提供了 startAbility() 方法来启动另外一个 Ability。因为Service也是 Ability 的一种,开发者同样可以通过将 Intent 传递给该方法来启动 Service。不仅支持启动本地 Service,还支持启动远程 Service。
开发者可以通过构造包含 DeviceId、BundleName 与 AbilityName 的 Operation 对象来设置目标 Service 信息。这三个参数的含义如下:
启动本地设备 Service 的代码示例如下:
  Intent intent = new Intent();
  Operation operation = new Intent.OperationBuilder()
          .withDeviceId("")
          .withBundleName("com.huawei.hiworld.himusic")
          .withAbilityName("com.huawei.hiworld.himusic.entry.ServiceAbility")
          .build();
  intent.setOperation(operation);
  startAbility(intent);启动远程设备 Service 的代码示例如下:
  Operation operation = new Intent.OperationBuilder()
          .withDeviceId("deviceId")
          .withBundleName("com.huawei.hiworld.himusic")
          .withAbilityName("com.huawei.hiworld.himusic.entry.ServiceAbility")
          .withFlags(Intent.FLAG_ABILITYSLICE_MULTI_DEVICE) // 设置支持分布式调度系统多设备启动的标识
          .build();
  Intent intent = new Intent();
  intent.setOperation(operation);
  startAbility(intent);执行上述代码后,Ability 将通过 startAbility() 方法来启动 Service。
Service 一旦创建就会一直保持在后台运行,除非必须回收内存资源,否则系统不会停止或销毁 Service。开发者可以在 Service 中通过 terminateAbility() 停止本 Service 或在其他 Ability 调用 stopAbility() 来停止 Service。
停止 Service 同样支持停止本地设备 Service 和停止远程设备 Service,使用方法与启动 Service 一样。一旦调用停止 Service 的方法,系统便会尽快销毁 Service。