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

2022-10-28 浏览 (844)

@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

NameTypeMandatoryDescription
parameterMissionDeviceInfoYesInformation about the device to listen for.
optionsMissionCallbackYesCallback to register.
callbackAsyncCallback<void>YesCallback 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

NameTypeMandatoryDescription
parameterMissionDeviceInfoYesInformation about the device to listen for.
optionsMissionCallbackYesCallback to register.

Return value

TypeDescription
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

NameTypeMandatoryDescription
parameterMissionDeviceInfoYesInformation about the device to listen for.
callbackAsyncCallback<void>YesCallback 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

NameTypeMandatoryDescription
parameterMissionDeviceInfoYesInformation about the device to listen for.

Return value

TypeDescription
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

NameTypeMandatoryDescription
parameterMissionParameterYesParameters required for synchronization.
callbackAsyncCallback<void>YesCallback 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

NameTypeMandatoryDescription
parameterMissionParameterYesParameters required for synchronization.

Return value

TypeDescription
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

NameTypeMandatoryDescription
parameterMissionDeviceInfoYesParameters required for synchronization.
callbackAsyncCallback<void>YesCallback 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

NameTypeMandatoryDescription
parameterMissionDeviceInfoYesParameters required for synchronization.

Return value

TypeDescription
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

NameTypeMandatoryDescription
parameterContinueDeviceInfoYesParameters required for mission continuation.
optionsContinueCallbackYesCallback invoked when the mission continuation is complete.
callbackAsyncCallback<void>YesCallback 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.

IDError Message
16300501The system ability work abnormally.
16300502Failed to get the missionInfo of the specified missionId.
16300503The application is not installed on the remote end and installation-free is not supported.
16300504The application is not installed on the remote end but installation-free is supported, try again with freeInstall flag.
16300505The operation device must be the device where the application to be continued is located or the target device to be continued.
16300506The 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

NameTypeMandatoryDescription
parameterContinueDeviceInfoYesParameters required for mission continuation.
optionsContinueCallbackYesCallback invoked when the mission continuation is complete.

Return value

TypeDescription
Promise<void>Promise that returns no value.

Error codes

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

IDError Message
16300501The system ability work abnormally.
16300502Failed to get the missionInfo of the specified missionId.
16300503The application is not installed on the remote end and installation-free is not supported.
16300504The application is not installed on the remote end but installation-free is supported, try again with freeInstall flag.
16300505The operation device must be the device where the application to be continued is located or the target device to be continued.
16300506The 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

NameTypeMandatoryDescription
parameterContinueMissionInfoYesParameters required for mission continuation.
callbackAsyncCallback<void>YesCallback 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.

IDError Message
16300501The system ability work abnormally.
16300503The application is not installed on the remote end and installation-free is not supported.
16300504The application is not installed on the remote end but installation-free is supported, try again with freeInstall flag.
16300505The operation device must be the device where the application to be continued is located or the target device to be continued.
16300506The local continuation task is already in progress.
16300507Failed 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

NameTypeMandatoryDescription
parameterContinueMissionInfoYesParameters required for mission continuation.

Return value

TypeDescription
Promise<void>Promise that returns no value.

Error codes

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

IDError Message
16300501The system ability work abnormally.
16300503The application is not installed on the remote end and installation-free is not supported.
16300504The application is not installed on the remote end but installation-free is supported, try again with freeInstall flag.
16300505The operation device must be the device where the application to be continued is located or the target device to be continued.
16300506The local continuation task is already in progress.
16300507Failed 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

NameTypeMandatoryDescription
typestringYesEvent type. The value 'continueStateChange' indicates the continuation state change event of the current mission.
callbackCallback<{ state: ContinueState, info: ContinuableInfo }>YesCallback 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

NameTypeMandatoryDescription
typestringYesEvent type. The value 'continueStateChange' indicates the continuation state change event of the current mission.
callbackCallback<{ state: ContinueState, info: ContinuableInfo }>NoCallback 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

NameTypeReadableWritableDescription
notifyMissionsChangedfunctionYesNoCallback used to notify the mission change event and return the device ID.
notifySnapshotfunctionYesNoCallback used to notify the snapshot change event and return the device ID and mission ID.
notifyNetDisconnectfunctionYesNoCallback 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

NameTypeReadableWritableDescription
deviceIdstringYesYesDevice ID.
fixConflictbooleanYesYesWhether a version conflict occurs.
tagnumberYesYesTag of the mission.

MissionDeviceInfo

Defines the parameters required for registering a listener.

Required permissions: ohos.permission.MANAGE_MISSIONS

System capability: SystemCapability.Ability.AbilityRuntime.Mission

NameTypeReadableWritableDescription
deviceIdstringYesYesDevice ID.

ContinueState10+

Enumerates the mission continuation states.

System capability: SystemCapability.Ability.AbilityRuntime.Mission

NameValueDescription
ACTIVE0Continuation is activated for the current mission.
INACTIVE1Continuation 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)

  • 所属分类: 后端技术
  • 本文标签: 鸿蒙 软件
  • 版权声明: 本文链接 https://seaxiang.com/blog/3434d38674614b21a3c0348ed1918f4d