harmony 鸿蒙@ohos.distributedMissionManager (Distributed Mission Management)

  • 2022-10-28
  • 浏览 (671)

@ohos.distributedMissionManager (Distributed Mission Management)

The distributedMissionManager module implements mission management across devices. You can use the APIs provided by this module to register or deregister a mission status listener, start or stop synchronizing a remote mission list, and continue a mission on a remote device by mission ID or bundle name.

NOTE

The initial APIs of this module are supported since API version 9. Newly added APIs will be marked with a superscript to indicate their earliest API version.

The APIs provided by this module are system APIs.

Modules to Import

import distributedMissionManager from '@ohos.distributedMissionManager';

distributedMissionManager.registerMissionListener

registerMissionListener(parameter: MissionDeviceInfo, options: MissionCallback, callback: AsyncCallback<void>): void;

Registers a mission status listener. This API uses an asynchronous callback to return the result.

Required permissions: ohos.permission.MANAGE_MISSIONS

System capability: SystemCapability.Ability.AbilityRuntime.Mission

Parameters

Name Type Mandatory Description
parameter MissionDeviceInfo Yes Information about the device to listen for.
options MissionCallback Yes Callback to register.
callback AsyncCallback<void> Yes Callback used to return the result. If the listener is registered, err is undefined. Otherwise, err is an error object.

Example

  import distributedMissionManager from '@ohos.distributedMissionManager'
  import { BusinessError } from '@ohos.base';
  
  // Implement a callback function.
  function NotifyMissionsChanged(deviceId: string): void {
    console.log('NotifyMissionsChanged deviceId ' + JSON.stringify(deviceId));
  }
  function NotifySnapshot(deviceId: string, missionId: number): void {
    console.log('NotifySnapshot deviceId ' + JSON.stringify(deviceId));
    console.log('NotifySnapshot missionId ' + JSON.stringify(missionId));
  }
  function NotifyNetDisconnect(deviceId: string, state: number): void {
    console.log('NotifyNetDisconnect deviceId ' + JSON.stringify(deviceId));
    console.log('NotifyNetDisconnect state ' + JSON.stringify(state));
  }
  try {
    // Call registerMissionListener.
    distributedMissionManager.registerMissionListener(
      { deviceId: "" },
      {
        notifyMissionsChanged: NotifyMissionsChanged,
        notifySnapshot: NotifySnapshot,
        notifyNetDisconnect: NotifyNetDisconnect
      },
      (error: BusinessError) => {
        if (error.code != 0) {
          console.error('registerMissionListener failed, cause: ' + JSON.stringify(error))
        }
        console.info('registerMissionListener finished')
      });
  } catch (error) {
    console.error('registerMissionListener failed, cause: ' + JSON.stringify(error))
  }

distributedMissionManager.registerMissionListener

registerMissionListener(parameter: MissionDeviceInfo, options: MissionCallback): Promise<void>

Registers a mission status listener. This API uses a promise to return the result.

Required permissions: ohos.permission.MANAGE_MISSIONS

System capability: SystemCapability.Ability.AbilityRuntime.Mission

Parameters

Name Type Mandatory Description
parameter MissionDeviceInfo Yes Information about the device to listen for.
options MissionCallback Yes Callback to register.

Return value

Type Description
Promise<void> Promise that returns no value.

Example

  import distributedMissionManager from '@ohos.distributedMissionManager'
  import { BusinessError } from '@ohos.base';

  // Implement a callback function.
  function NotifyMissionsChanged(deviceId: string): void {
    console.log('NotifyMissionsChanged deviceId ' + JSON.stringify(deviceId));
  }
  function NotifySnapshot(deviceId: string, missionId: number): void {
    console.log('NotifySnapshot deviceId ' + JSON.stringify(deviceId));
    console.log('NotifySnapshot missionId ' + JSON.stringify(missionId));
  }
  function NotifyNetDisconnect(deviceId: string, state: number): void {
    console.log('NotifyNetDisconnect deviceId ' + JSON.stringify(deviceId));
    console.log('NotifyNetDisconnect state ' + JSON.stringify(state));
  }
  try {
      // Call registerMissionListener.
      distributedMissionManager.registerMissionListener(
        { deviceId: "" },
        {
          notifyMissionsChanged: NotifyMissionsChanged,
          notifySnapshot: NotifySnapshot,
          notifyNetDisconnect: NotifyNetDisconnect
        }).then((data: void) => {
          console.info('registerMissionListener finished, ' + JSON.stringify(data));
      }).catch((error: BusinessError) => {
          console.error('registerMissionListener failed, cause: ' + JSON.stringify(error));
      })
  } catch (error) {
      console.error('registerMissionListener failed, cause: ' + JSON.stringify(error))
  }

distributedMissionManager.unRegisterMissionListener

unRegisterMissionListener(parameter: MissionDeviceInfo, callback: AsyncCallback<void>): void;

Deregisters a mission status listener. This API uses an asynchronous callback to return the result.

Required permissions: ohos.permission.MANAGE_MISSIONS

System capability: SystemCapability.Ability.AbilityRuntime.Mission

Parameters

Name Type Mandatory Description
parameter MissionDeviceInfo Yes Information about the device to listen for.
callback AsyncCallback<void> Yes Callback used to return the result. If the listener is deregistered, err is undefined. Otherwise, err is an error object.

Example

  import distributedMissionManager from '@ohos.distributedMissionManager'
  import { BusinessError } from '@ohos.base';

  try {
    distributedMissionManager.unRegisterMissionListener(
      { deviceId: "" },
      (error: BusinessError) => {
        if (error.code != 0) {
            console.error('unRegisterMissionListener failed, cause: ' + JSON.stringify(error))
        }
        console.info('unRegisterMissionListener finished')
    })
  } catch (error) {
      console.error('unRegisterMissionListener failed, cause: ' + JSON.stringify(error))
  }

distributedMissionManager.unRegisterMissionListener

unRegisterMissionListener(parameter: MissionDeviceInfo): Promise<void>

Deregisters a mission status listener. This API uses a promise to return the result.

Required permissions: ohos.permission.MANAGE_MISSIONS

System capability: SystemCapability.Ability.AbilityRuntime.Mission

Parameters

Name Type Mandatory Description
parameter MissionDeviceInfo Yes Information about the device to listen for.

Return value

Type Description
Promise<void> Promise that returns no value.

Example

  import distributedMissionManager from '@ohos.distributedMissionManager'
  import { BusinessError } from '@ohos.base';

  try {
    distributedMissionManager.unRegisterMissionListener({deviceId: ""}).then(() => {
      console.info('unRegisterMissionListener finished successfully');
    }).catch((error: BusinessError) => {
        console.error('unRegisterMissionListener failed, cause: ' + JSON.stringify(error));
    })
  } catch (error) {
      console.error('unRegisterMissionListener failed, cause: ' + JSON.stringify(error))
  }

distributedMissionManager.startSyncRemoteMissions

startSyncRemoteMissions(parameter: MissionParameter, callback: AsyncCallback<void>): void;

Starts to synchronize the remote mission list. This API uses an asynchronous callback to return the result.

Required permissions: ohos.permission.MANAGE_MISSIONS

System capability: SystemCapability.Ability.AbilityRuntime.Mission

Parameters

Name Type Mandatory Description
parameter MissionParameter Yes Parameters required for synchronization.
callback AsyncCallback<void> Yes Callback used to return the result. If the synchronization is started, err is undefined. Otherwise, err is an error object.

Example

  import distributedMissionManager from '@ohos.distributedMissionManager'
  import { BusinessError } from '@ohos.base';

  try {
    distributedMissionManager.startSyncRemoteMissions(
      {
        deviceId: "",
        fixConflict: false,
        tag: 0
      },
      (error: BusinessError) => {
        if (error.code != 0) {
          console.error('startSyncRemoteMissions failed, cause: ' + JSON.stringify(error))
        }
        console.info('startSyncRemoteMissions finished')}
    )
  } catch (error) {
    console.error('startSyncRemoteMissions failed, cause: ' + JSON.stringify(error))
  }

distributedMissionManager.startSyncRemoteMissions

startSyncRemoteMissions(parameter: MissionParameter): Promise<void>

Starts to synchronize the remote mission list. This API uses a promise to return the result.

Required permissions: ohos.permission.MANAGE_MISSIONS

System capability: SystemCapability.Ability.AbilityRuntime.Mission

Parameters

Name Type Mandatory Description
parameter MissionParameter Yes Parameters required for synchronization.

Return value

Type Description
Promise<void> Promise that returns no value.

Example

  import distributedMissionManager from '@ohos.distributedMissionManager'
  import { BusinessError } from '@ohos.base';

  try {
    distributedMissionManager.startSyncRemoteMissions(
      {
        deviceId: "",
        fixConflict: false,
        tag: 0
      }
    ).then(() => {
        console.info('startSyncRemoteMissions finished successfully');
      }).catch((error: BusinessError) => {
      console.error('startSyncRemoteMissions failed, cause: ' + JSON.stringify(error));
    })
  } catch (error) {
    console.error('startSyncRemoteMissions failed, cause: ' + JSON.stringify(error))
  }

distributedMissionManager.stopSyncRemoteMissions

stopSyncRemoteMissions(parameter: MissionDeviceInfo, callback: AsyncCallback<void>): void;

Stops synchronizing the remote mission list. This API uses an asynchronous callback to return the result.

Required permissions: ohos.permission.MANAGE_MISSIONS

System capability: SystemCapability.Ability.AbilityRuntime.Mission

Parameters

Name Type Mandatory Description
parameter MissionDeviceInfo Yes Parameters required for synchronization.
callback AsyncCallback<void> Yes Callback used to return the result. If the synchronization is stopped, err is undefined. Otherwise, err is an error object.

Example

  import distributedMissionManager from '@ohos.distributedMissionManager'
  import { BusinessError } from '@ohos.base';

  try {
    distributedMissionManager.stopSyncRemoteMissions(
      {
        deviceId: ""
      },
      (error: BusinessError) => {
        if (error.code != 0) {
          console.error('stopSyncRemoteMissions failed, cause: ' + JSON.stringify(error))
        }
        console.info('stopSyncRemoteMissions finished')}
    )
  } catch (error) {
    console.error('stopSyncRemoteMissions failed, cause: ' + JSON.stringify(error))
  }

distributedMissionManager.stopSyncRemoteMissions

stopSyncRemoteMissions(parameter: MissionDeviceInfo): Promise<void>

Stops synchronizing the remote mission list. This API uses a promise to return the result.

Required permissions: ohos.permission.MANAGE_MISSIONS

System capability: SystemCapability.Ability.AbilityRuntime.Mission

Parameters

Name Type Mandatory Description
parameter MissionDeviceInfo Yes Parameters required for synchronization.

Return value

Type Description
Promise<void> Promise that returns no value.

Example

  import distributedMissionManager from '@ohos.distributedMissionManager'
  import { BusinessError } from '@ohos.base';

  try {
    distributedMissionManager.stopSyncRemoteMissions(
      {
        deviceId: ""
      }).then((data: void) => {
        console.info('stopSyncRemoteMissions finished successfully');
      }).catch((error: BusinessError) => {
      console.error('stopSyncRemoteMissions failed, cause: ' + JSON.stringify(error));
    })
  } catch (error) {
    console.error('stopSyncRemoteMissions failed, cause: ' + JSON.stringify(error))
  }

distributedMissionManager.continueMission

continueMission(parameter: ContinueDeviceInfo, options: ContinueCallback, callback: AsyncCallback<void>): void;

Continues a mission on a remote device, with the mission ID specified. This API uses an asynchronous callback to return the result.

Required permissions: ohos.permission.MANAGE_MISSIONS and ohos.permission.DISTRIBUTED_DATASYNC

System capability: SystemCapability.Ability.AbilityRuntime.Mission

Parameters

Name Type Mandatory Description
parameter ContinueDeviceInfo Yes Parameters required for mission continuation.
options ContinueCallback Yes Callback invoked when the mission continuation is complete.
callback AsyncCallback<void> Yes Callback used to return the result. If the mission is continued, err is undefined. Otherwise, err is an error object.

Error codes

For details about the error codes, see Distributed Scheduler Error Codes.

ID Error Message
16300501 The system ability work abnormally.
16300502 Failed to get the missionInfo of the specified missionId.
16300503 The application is not installed on the remote end and installation-free is not supported.
16300504 The application is not installed on the remote end but installation-free is supported, try again with freeInstall flag.
16300505 The operation device must be the device where the application to be continued is located or the target device to be continued.
16300506 The local continuation task is already in progress.

Example

  import distributedMissionManager from '@ohos.distributedMissionManager'
  import { BusinessError } from '@ohos.base';

  // Implement a callback function.
  function onContinueDone(resultCode: number): void {
    console.log('onContinueDone resultCode: ' + JSON.stringify(resultCode));
  };
  try {
    // Call continueMission.
    distributedMissionManager.continueMission(
      {
        srcDeviceId: "",
        dstDeviceId: "",
        missionId: 1,
        wantParam: {"key": "value"}
      },
      { onContinueDone: onContinueDone },
      (error: BusinessError) => {
        if (error.code != 0) {
          console.error('continueMission failed, cause: ' + JSON.stringify(error))
        }
        console.info('continueMission finished')
    })
  } catch (error) {
    console.error('continueMission failed, cause: ' + JSON.stringify(error))
  }

distributedMissionManager.continueMission

continueMission(parameter: ContinueDeviceInfo, options: ContinueCallback): Promise<void>

Continues a mission on a remote device, with the mission ID specified. This API uses a promise to return the result.

Required permissions: ohos.permission.MANAGE_MISSIONS and ohos.permission.DISTRIBUTED_DATASYNC

System capability: SystemCapability.Ability.AbilityRuntime.Mission

Parameters

Name Type Mandatory Description
parameter ContinueDeviceInfo Yes Parameters required for mission continuation.
options ContinueCallback Yes Callback invoked when the mission continuation is complete.

Return value

Type Description
Promise<void> Promise that returns no value.

Error codes

For details about the error codes, see Distributed Scheduler Error Codes.

ID Error Message
16300501 The system ability work abnormally.
16300502 Failed to get the missionInfo of the specified missionId.
16300503 The application is not installed on the remote end and installation-free is not supported.
16300504 The application is not installed on the remote end but installation-free is supported, try again with freeInstall flag.
16300505 The operation device must be the device where the application to be continued is located or the target device to be continued.
16300506 The local continuation task is already in progress.

Example

  import distributedMissionManager from '@ohos.distributedMissionManager'
  import { BusinessError } from '@ohos.base';

  // Implement a callback function.
  function onContinueDone(resultCode: number): void {
    console.log('onContinueDone resultCode: ' + JSON.stringify(resultCode));
  };
  try {
    // Call continueMission.
    distributedMissionManager.continueMission(
      {
        srcDeviceId: "",
        dstDeviceId: "",
        missionId: 1,
        wantParam: {"key": "value"}
      },
      { onContinueDone: onContinueDone }).then(() => {
        console.info('continueMission finished successfully');
      }).catch((error: BusinessError) => {
      console.error('continueMission failed, cause: ' + JSON.stringify(error));
    })
  } catch (error) {
    console.error('continueMission failed, cause: ' + JSON.stringify(error))
  }

distributedMissionManager.continueMission10+

continueMission(parameter: ContinueMissionInfo, callback: AsyncCallback<void>): void;

Continues a mission on a remote device, with the bundle name specified. This API uses an asynchronous callback to return the result.

Required permissions: ohos.permission.MANAGE_MISSIONS and ohos.permission.DISTRIBUTED_DATASYNC

System capability: SystemCapability.Ability.AbilityRuntime.Mission

Parameters

Name Type Mandatory Description
parameter ContinueMissionInfo Yes Parameters required for mission continuation.
callback AsyncCallback<void> Yes Callback used to return the result. If the mission is continued, err is undefined. Otherwise, err is an error object.

Error codes

For details about the error codes, see Distributed Scheduler Error Codes.

ID Error Message
16300501 The system ability work abnormally.
16300503 The application is not installed on the remote end and installation-free is not supported.
16300504 The application is not installed on the remote end but installation-free is supported, try again with freeInstall flag.
16300505 The operation device must be the device where the application to be continued is located or the target device to be continued.
16300506 The local continuation task is already in progress.
16300507 Failed to get the missionInfo of the specified bundle name.

Example

  import distributedMissionManager from '@ohos.distributedMissionManager'
  import { BusinessError } from '@ohos.base';

  try {
    distributedMissionManager.continueMission(
      {
        srcDeviceId: "",
        dstDeviceId: "",
        bundleName: "ohos.test.continueapp",
        wantParam: {"key": "value"}
      },
      (error: BusinessError) => {
        if (error.code != 0) {
          console.error('continueMission failed, cause: ' + JSON.stringify(error))
        }
        console.info('continueMission finished')
    })
  } catch (error) {
    console.error('continueMission failed, cause: ' + JSON.stringify(error))
  }

distributedMissionManager.continueMission10+

continueMission(parameter: ContinueMissionInfo): Promise<void>

Continues a mission on a remote device, with the bundle name specified. This API uses a promise to return the result.

Required permissions: ohos.permission.MANAGE_MISSIONS and ohos.permission.DISTRIBUTED_DATASYNC

System capability: SystemCapability.Ability.AbilityRuntime.Mission

Parameters

Name Type Mandatory Description
parameter ContinueMissionInfo Yes Parameters required for mission continuation.

Return value

Type Description
Promise<void> Promise that returns no value.

Error codes

For details about the error codes, see Distributed Scheduler Error Codes.

ID Error Message
16300501 The system ability work abnormally.
16300503 The application is not installed on the remote end and installation-free is not supported.
16300504 The application is not installed on the remote end but installation-free is supported, try again with freeInstall flag.
16300505 The operation device must be the device where the application to be continued is located or the target device to be continued.
16300506 The local continuation task is already in progress.
16300507 Failed to get the missionInfo of the specified bundle name.

Example

  import distributedMissionManager from '@ohos.distributedMissionManager'
  import { BusinessError } from '@ohos.base';

  try {
      distributedMissionManager.continueMission(
        {
          srcDeviceId: "",
          dstDeviceId: "",
          bundleName: "ohos.test.continueapp",
          wantParam: {"key": "value"}
        }
      ).then(() => {
          console.info('continueMission finished successfully');
      }).catch((error: BusinessError) => {
          console.error('continueMission failed, cause: ' + JSON.stringify(error));
      })
  } catch (error) {
      console.error('continueMission failed, cause: ' + JSON.stringify(error))
  }

distributedMissionManager.on(‘continueStateChange’)10+

on(type: ‘continueStateChange’, callback: Callback<{ state: ContinueState, info: ContinuableInfo }>): void

Subscribes to continuation state change events of the current mission.

Required permissions: ohos.permission.MANAGE_MISSIONS

System capability: SystemCapability.Ability.AbilityRuntime.Mission

Parameters

Name Type Mandatory Description
type string Yes Event type. The value ‘continueStateChange’ indicates the continuation state change event of the current mission.
callback Callback<{ state: ContinueState, info: ContinuableInfo }> Yes Callback used to return the continuation state and information of the current mission.

Example

  import distributedMissionManager from '@ohos.distributedMissionManager'

  try {
    distributedMissionManager.on('continueStateChange', (data) => {
      console.info("continueStateChange on:" + JSON.stringify(data));
    });
  } catch (error) {
    console.error("continueStateChange err: " + JSON.stringify(error));
  }

distributedMissionManager.off(‘continueStateChange’)10+

off(type: ‘continueStateChange’, callback?: Callback<{ state: ContinueState, info: ContinuableInfo }>): void

Unsubscribes from continuation state change events of the current mission.

Required permissions: ohos.permission.MANAGE_MISSIONS

System capability: SystemCapability.Ability.AbilityRuntime.Mission

Parameters

Name Type Mandatory Description
type string Yes Event type. The value ‘continueStateChange’ indicates the continuation state change event of the current mission.
callback Callback<{ state: ContinueState, info: ContinuableInfo }> No Callback used to return the continuation state and information of the current mission.
If the callback is unspecified, all subscriptions to the specified event are canceled.

Example

  import distributedMissionManager from '@ohos.distributedMissionManager'

  try {
    distributedMissionManager.off('continueStateChange', (data) => {
      console.info("continueStateChange off:" + JSON.stringify(data));
    });
  } catch (err) {
    console.error("continueStateChange err: " + JSON.stringify(err));
  }

MissionCallback

Defines the callbacks that can be registered as a mission status listener.

Required permissions: ohos.permission.MANAGE_MISSIONS

System capability: SystemCapability.Ability.AbilityRuntime.Mission

Name Type Readable Writable Description
notifyMissionsChanged function Yes No Callback used to notify the mission change event and return the device ID.
notifySnapshot function Yes No Callback used to notify the snapshot change event and return the device ID and mission ID.
notifyNetDisconnect function Yes No Callback used to notify the disconnection event and return the device ID and network status.

MissionParameter

Defines the parameters required for mission synchronization.

Required permissions: ohos.permission.MANAGE_MISSIONS

System capability: SystemCapability.Ability.AbilityRuntime.Mission

Name Type Readable Writable Description
deviceId string Yes Yes Device ID.
fixConflict boolean Yes Yes Whether a version conflict occurs.
tag number Yes Yes Tag of the mission.

MissionDeviceInfo

Defines the parameters required for registering a listener.

Required permissions: ohos.permission.MANAGE_MISSIONS

System capability: SystemCapability.Ability.AbilityRuntime.Mission

Name Type Readable Writable Description
deviceId string Yes Yes Device ID.

ContinueState10+

Enumerates the mission continuation states.

System capability: SystemCapability.Ability.AbilityRuntime.Mission

Name Value Description
ACTIVE 0 Continuation is activated for the current mission.
INACTIVE 1 Continuation is not activated for the current mission.

你可能感兴趣的鸿蒙文章

harmony 鸿蒙APIs

harmony 鸿蒙System Common Events (To Be Deprecated Soon)

harmony 鸿蒙System Common Events

harmony 鸿蒙API Reference Document Description

harmony 鸿蒙Enterprise Device Management Overview (for System Applications Only)

harmony 鸿蒙BundleStatusCallback

harmony 鸿蒙@ohos.bundle.innerBundleManager (innerBundleManager)

harmony 鸿蒙@ohos.distributedBundle (Distributed Bundle Management)

harmony 鸿蒙@ohos.bundle (Bundle)

harmony 鸿蒙@ohos.enterprise.EnterpriseAdminExtensionAbility (EnterpriseAdminExtensionAbility)

0  赞