harmony 鸿蒙ServiceExtensionContext (系统接口)

  • 2025-06-12
  • 浏览 (5)

ServiceExtensionContext (系统接口)

ServiceExtensionContext模块是ServiceExtensionAbility的上下文环境,继承自ExtensionContext。

ServiceExtensionContext模块提供ServiceExtensionAbility具有的能力,包括启动、停止、绑定、解绑Ability。

说明:

  • 本模块首批接口从API version 9开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
  • 本模块接口仅可在Stage模型下使用。
  • 本模块接口需要在主线程中使用,不要在Worker、TaskPool等子线程中使用。
  • 本模块接口为系统接口。

导入模块

import { common } from '@kit.AbilityKit';

使用说明

在使用ServiceExtensionContext的功能前,需要通过ServiceExtensionAbility子类实例获取。

示例:

import { ServiceExtensionAbility } from '@kit.AbilityKit';
import { rpc } from '@kit.IPCKit';

let commRemote: rpc.IRemoteObject|null; // 断开连接时需要释放

class EntryAbility extends ServiceExtensionAbility {
  onCreate() {
    let context = this.context; // 获取ServiceExtensionContext
  }
}

ServiceExtensionContext.startAbility

startAbility(want: Want, callback: AsyncCallback<void>): void

启动Ability。仅支持在主线程调用。使用callback异步回调。

系统能力:SystemCapability.Ability.AbilityRuntime.Core

系统接口:此接口为系统接口。

参数:

参数名 类型 必填 说明
want Want Want类型参数,传入需要启动的Ability的信息,如Ability名称,Bundle名称等。
callback AsyncCallback<void> 回调函数。当启动Ability成功,err为undefined,否则为错误对象。

错误码:

以下错误码详细介绍请参考通用错误码元能力子系统错误码

错误码ID 错误信息
201 The application does not have permission to call the interface.
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed.
16000001 The specified ability does not exist.
16000002 Incorrect ability type.
16000004 Cannot start an invisible component.
16000005 The specified process does not have the permission.
16000006 Cross-user operations are not allowed.
16000008 The crowdtesting application expires.
16000009 An ability cannot be started or stopped in Wukong mode.
16000010 The call with the continuation and prepare continuation flag is forbidden.
16000011 The context does not exist.
16000012 The application is controlled.
16000013 The application is controlled by EDM.
16000019 No matching ability is found.
16000050 Internal error.
16000053 The ability is not on the top of the UI.
16000055 Installation-free timed out.
16000071 App clone is not supported.
16000072 App clone or multi-instance is not supported.
16000073 The app clone index is invalid.
16000076 The app instance key is invalid.
16000077 The number of app instances reaches the limit.
16000078 The multi-instance is not supported.
16000079 The APP_INSTANCE_KEY cannot be specified.
16000080 Creating a new instance is not supported.
16200001 The caller has been released.

示例:

import { ServiceExtensionAbility, Want } from '@kit.AbilityKit';
import { BusinessError } from '@kit.BasicServicesKit';

class EntryAbility extends ServiceExtensionAbility {
  onCreate() {
    let want: Want = {
      bundleName: 'com.example.myapp',
      abilityName: 'MyAbility'
    };

    try {
      this.context.startAbility(want, (error: BusinessError) => {
        if (error.code) {
          // 处理业务逻辑错误
          console.error(`startAbility failed, error.code: ${error.code}, error.message: ${error.message}`);
          return;
        }
        // 执行正常业务
        console.info('startAbility succeed');
      });
    } catch (paramError) {
      // 处理入参错误异常
      console.error(`error.code: ${paramError.code}, error.message: ${paramError.message}`);
    }
  }
}

ServiceExtensionContext.startAbility

startAbility(want: Want, options?: StartOptions): Promise<void>

启动Ability。仅支持在主线程调用。使用Promise异步回调。

系统能力:SystemCapability.Ability.AbilityRuntime.Core

系统接口:此接口为系统接口。

参数:

参数名 类型 必填 说明
want Want Want类型参数,传入需要启动的Ability的信息,如Ability名称,Bundle名称等。
options StartOptions 启动Ability所携带的参数。

返回值:

类型 说明
Promise&lt;void&gt; Promise对象。无返回结果的Promise对象。

错误码:

以下错误码详细介绍请参考通用错误码元能力子系统错误码

错误码ID 错误信息
201 The application does not have permission to call the interface.
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed.
16000001 The specified ability does not exist.
16000002 Incorrect ability type.
16000004 Cannot start an invisible component.
16000005 The specified process does not have the permission.
16000006 Cross-user operations are not allowed.
16000008 The crowdtesting application expires.
16000009 An ability cannot be started or stopped in Wukong mode.
16000010 The call with the continuation and prepare continuation flag is forbidden.
16000011 The context does not exist.
16000012 The application is controlled.
16000013 The application is controlled by EDM.
16000019 No matching ability is found.
16000050 Internal error.
16000053 The ability is not on the top of the UI.
16000055 Installation-free timed out.
16000071 App clone is not supported.
16000072 App clone or multi-instance is not supported.
16000073 The app clone index is invalid.
16000076 The app instance key is invalid.
16000077 The number of app instances reaches the limit.
16000078 The multi-instance is not supported.
16000079 The APP_INSTANCE_KEY cannot be specified.
16000080 Creating a new instance is not supported.
16200001 The caller has been released.

示例:

import { ServiceExtensionAbility, Want, StartOptions } from '@kit.AbilityKit';
import { BusinessError } from '@kit.BasicServicesKit';

class EntryAbility extends ServiceExtensionAbility {
  onCreate() {
    let want: Want = {
      bundleName: 'com.example.myapp',
      abilityName: 'MyAbility'
    };
    let options: StartOptions = {
      windowMode: 0,
    };

    try {
      this.context.startAbility(want, options)
        .then((data: void) => {
          // 执行正常业务
          console.info('startAbility succeed');
        })
        .catch((error: BusinessError) => {
          // 处理业务逻辑错误
          console.error(`startAbility failed, error.code: ${error.code}, error.message: ${error.message}`);
        });
    } catch (paramError) {
      // 处理入参错误异常
      console.error(`error.code: ${paramError.code}, error.message: ${paramError.message}`);
    }
  }
}

ServiceExtensionContext.startAbility

startAbility(want: Want, options: StartOptions, callback: AsyncCallback&lt;void&gt;): void

启动Ability。仅支持在主线程调用。使用callback异步回调。

系统能力:SystemCapability.Ability.AbilityRuntime.Core

系统接口:此接口为系统接口。

参数:

参数名 类型 必填 说明
want Want 启动Ability的Want信息。
options StartOptions 启动Ability所携带的参数。
callback AsyncCallback&lt;void&gt; 回调函数。当启动Ability成功,err为undefined,否则为错误对象。

错误码:

以下错误码详细介绍请参考通用错误码元能力子系统错误码

错误码ID 错误信息
201 The application does not have permission to call the interface.
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed.
16000001 The specified ability does not exist.
16000002 Incorrect ability type.
16000004 Cannot start an invisible component.
16000005 The specified process does not have the permission.
16000006 Cross-user operations are not allowed.
16000008 The crowdtesting application expires.
16000009 An ability cannot be started or stopped in Wukong mode.
16000010 The call with the continuation and prepare continuation flag is forbidden.
16000011 The context does not exist.
16000012 The application is controlled.
16000013 The application is controlled by EDM.
16000019 No matching ability is found.
16000050 Internal error.
16000053 The ability is not on the top of the UI.
16000055 Installation-free timed out.
16000071 App clone is not supported.
16000072 App clone or multi-instance is not supported.
16000073 The app clone index is invalid.
16000076 The app instance key is invalid.
16000077 The number of app instances reaches the limit.
16000078 The multi-instance is not supported.
16000079 The APP_INSTANCE_KEY cannot be specified.
16000080 Creating a new instance is not supported.
16200001 The caller has been released.

示例:

import { ServiceExtensionAbility, Want, StartOptions } from '@kit.AbilityKit';
import { BusinessError } from '@kit.BasicServicesKit';

class EntryAbility extends ServiceExtensionAbility {
  onCreate() {
    let want: Want = {
      deviceId: '',
      bundleName: 'com.example.myapplication',
      abilityName: 'EntryAbility'
    };
    let options: StartOptions = {
      windowMode: 0
    };

    try {
      this.context.startAbility(want, options, (error: BusinessError) => {
        if (error.code) {
          // 处理业务逻辑错误
          console.error(`startAbility failed, error.code: ${error.code}, error.message: ${error.message}`);
          return;
        }
        // 执行正常业务
        console.info('startAbility succeed');
      });
    } catch (paramError) {
      // 处理入参错误异常
      console.error(`error.code: ${paramError.code}, error.message: ${paramError.message}`);
    }
  }
}

ServiceExtensionContext.startAbilityWithAccount

startAbilityWithAccount(want: Want, accountId: number, callback: AsyncCallback<void>): void

根据accountId启动Ability。仅支持在主线程调用。使用callback异步回调。

说明:

组件启动规则详见:组件启动规则(Stage模型)

需要权限:ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS

系统能力:SystemCapability.Ability.AbilityRuntime.Core

系统接口:此接口为系统接口。

参数:

参数名 类型 必填 说明
want Want 启动Ability的Want信息。
accountId number 系统账号的账号ID,详情参考getCreatedOsAccountsCount
callback AsyncCallback<void> 回调函数。当根据accountId启动Ability成功,err为undefined,否则为错误对象。

错误码:

以下错误码详细介绍请参考通用错误码元能力子系统错误码

错误码ID 错误信息
201 The application does not have permission to call the interface.
202 The application is not system-app, can not use system-api.
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed.
16000001 The specified ability does not exist.
16000002 Incorrect ability type.
16000004 Cannot start an invisible component.
16000005 The specified process does not have the permission.
16000006 Cross-user operations are not allowed.
16000008 The crowdtesting application expires.
16000009 An ability cannot be started or stopped in Wukong mode.
16000010 The call with the continuation and prepare continuation flag is forbidden.
16000011 The context does not exist.
16000012 The application is controlled.
16000013 The application is controlled by EDM.
16000019 No matching ability is found.
16000050 Internal error.
16000053 The ability is not on the top of the UI.
16000055 Installation-free timed out.
16000071 App clone is not supported.
16000072 App clone or multi-instance is not supported.
16000073 The app clone index is invalid.
16000076 The app instance key is invalid.
16000077 The number of app instances reaches the limit.
16000078 The multi-instance is not supported.
16000079 The APP_INSTANCE_KEY cannot be specified.
16000080 Creating a new instance is not supported.
16200001 The caller has been released.

示例:

import { ServiceExtensionAbility, Want, StartOptions } from '@kit.AbilityKit';
import { BusinessError } from '@kit.BasicServicesKit';

class EntryAbility extends ServiceExtensionAbility {
  onCreate() {
    let want: Want = {
      deviceId: '',
      bundleName: 'com.example.myapplication',
      abilityName: 'EntryAbility'
    };
    let accountId = 100;

    try {
      this.context.startAbilityWithAccount(want, accountId, (error: BusinessError) => {
        if (error.code) {
          // 处理业务逻辑错误
          console.error(`startAbilityWithAccount failed, error.code: ${error.code}, error.message: ${error.message}`);
          return;
        }
        // 执行正常业务
        console.info('startAbilityWithAccount succeed');
      });
    } catch (paramError) {
      // 处理入参错误异常
      console.error(`error.code: ${paramError.code}, error.message: ${paramError.message}`);
    }
  }
}

ServiceExtensionContext.startAbilityWithAccount

startAbilityWithAccount(want: Want, accountId: number, options: StartOptions, callback: AsyncCallback<void>): void

根据accountId启动Ability。仅支持在主线程调用。使用callback异步回调。

说明:

组件启动规则详见:组件启动规则(Stage模型)

需要权限:ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS

系统能力:SystemCapability.Ability.AbilityRuntime.Core

系统接口:此接口为系统接口。

参数:

参数名 类型 必填 说明
want Want 启动Ability的Want信息。
accountId number 系统账号的账号ID,详情参考getCreatedOsAccountsCount
options StartOptions 启动Ability所携带的参数。
callback AsyncCallback<void> 回调函数。当根据accountId启动Ability成功,err为undefined,否则为错误对象。

错误码:

以下错误码详细介绍请参考通用错误码元能力子系统错误码

错误码ID 错误信息
201 The application does not have permission to call the interface.
202 The application is not system-app, can not use system-api.
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed.
16000001 The specified ability does not exist.
16000002 Incorrect ability type.
16000004 Cannot start an invisible component.
16000005 The specified process does not have the permission.
16000006 Cross-user operations are not allowed.
16000008 The crowdtesting application expires.
16000009 An ability cannot be started or stopped in Wukong mode.
16000010 The call with the continuation and prepare continuation flag is forbidden.
16000011 The context does not exist.
16000012 The application is controlled.
16000013 The application is controlled by EDM.
16000019 No matching ability is found.
16000050 Internal error.
16000053 The ability is not on the top of the UI.
16000055 Installation-free timed out.
16000071 App clone is not supported.
16000072 App clone or multi-instance is not supported.
16000073 The app clone index is invalid.
16000076 The app instance key is invalid.
16000077 The number of app instances reaches the limit.
16000078 The multi-instance is not supported.
16000079 The APP_INSTANCE_KEY cannot be specified.
16000080 Creating a new instance is not supported.
16200001 The caller has been released.

示例:

import { ServiceExtensionAbility, Want, StartOptions } from '@kit.AbilityKit';
import { BusinessError } from '@kit.BasicServicesKit';

class EntryAbility extends ServiceExtensionAbility {
  onCreate() {
    let want: Want = {
      deviceId: '',
      bundleName: 'com.example.myapplication',
      abilityName: 'EntryAbility'
    };
    let accountId = 100;
    let options: StartOptions = {
      windowMode: 0
    };

    try {
      this.context.startAbilityWithAccount(want, accountId, options, (error: BusinessError) => {
        if (error.code) {
          // 处理业务逻辑错误
          console.error(`startAbilityWithAccount failed, error.code: ${error.code}, error.message: ${error.message}`);
          return;
        }
        // 执行正常业务
        console.info('startAbilityWithAccount succeed');
      });
    } catch (paramError) {
      // 处理入参错误异常
      console.error(`error.code: ${(paramError as BusinessError).code}, error.message: ${(paramError as BusinessError).message}`);
    }
  }
}

ServiceExtensionContext.startAbilityWithAccount

startAbilityWithAccount(want: Want, accountId: number, options?: StartOptions): Promise<void>

根据accountId启动Ability。仅支持在主线程调用。使用Promise异步回调。

说明:

组件启动规则详见:组件启动规则(Stage模型)

需要权限:ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS

系统能力:SystemCapability.Ability.AbilityRuntime.Core

系统接口:此接口为系统接口。

参数:

参数名 类型 必填 说明
want Want 启动Ability的Want信息。
accountId number 系统账号的账号ID,详情参考getCreatedOsAccountsCount
options StartOptions 启动Ability所携带的参数。

返回值:

类型 说明
Promise&lt;void&gt; Promise对象。无返回结果的Promise对象。

错误码:

以下错误码详细介绍请参考通用错误码元能力子系统错误码

错误码ID 错误信息
201 The application does not have permission to call the interface.
202 The application is not system-app, can not use system-api.
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed.
16000001 The specified ability does not exist.
16000002 Incorrect ability type.
16000004 Cannot start an invisible component.
16000005 The specified process does not have the permission.
16000006 Cross-user operations are not allowed.
16000008 The crowdtesting application expires.
16000009 An ability cannot be started or stopped in Wukong mode.
16000010 The call with the continuation and prepare continuation flag is forbidden.
16000011 The context does not exist.
16000012 The application is controlled.
16000013 The application is controlled by EDM.
16000019 No matching ability is found.
16000050 Internal error.
16000053 The ability is not on the top of the UI.
16000055 Installation-free timed out.
16000071 App clone is not supported.
16000072 App clone or multi-instance is not supported.
16000073 The app clone index is invalid.
16000076 The app instance key is invalid.
16000077 The number of app instances reaches the limit.
16000078 The multi-instance is not supported.
16000079 The APP_INSTANCE_KEY cannot be specified.
16000080 Creating a new instance is not supported.
16200001 The caller has been released.

示例:

import { ServiceExtensionAbility, Want, StartOptions } from '@kit.AbilityKit';
import { BusinessError } from '@kit.BasicServicesKit';

class EntryAbility extends ServiceExtensionAbility {
  onCreate() {
    let want: Want = {
      deviceId: '',
      bundleName: 'com.example.myapplication',
      abilityName: 'EntryAbility'
    };
    let accountId = 100;
    let options: StartOptions = {
      windowMode: 0
    };

    try {
      this.context.startAbilityWithAccount(want, accountId, options)
        .then((data: void) => {
          // 执行正常业务
          console.info('startAbilityWithAccount succeed');
        })
        .catch((error: BusinessError) => {
          // 处理业务逻辑错误
          console.error(`startAbilityWithAccount failed, error.code: ${error.code}, error.message: ${error.message}`);
        });
    } catch (paramError) {
      // 处理入参错误异常
      console.error(`error.code: ${(paramError as BusinessError).code}, error.message: ${(paramError as BusinessError).message}`);
    }
  }
}

ServiceExtensionContext.startServiceExtensionAbility

startServiceExtensionAbility(want: Want, callback: AsyncCallback<void>): void

启动一个新的ServiceExtensionAbility。使用callback异步回调。

系统能力:SystemCapability.Ability.AbilityRuntime.Core

系统接口:此接口为系统接口。

参数:

参数名 类型 必填 说明
want Want 启动Ability的Want信息。
callback AsyncCallback<void> 回调函数。当启动一个新的ServiceExtensionAbility成功,err为undefined,否则为错误对象。

错误码:

以下错误码详细介绍请参考通用错误码元能力子系统错误码

错误码ID 错误信息
201 The application does not have permission to call the interface.
202 The application is not system-app, can not use system-api.
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed.
16000001 The specified ability does not exist.
16000002 Incorrect ability type.
16000004 Cannot start an invisible component.
16000005 The specified process does not have the permission.
16000006 Cross-user operations are not allowed.
16000008 The crowdtesting application expires.
16000011 The context does not exist.
16000012 The application is controlled.
16000013 The application is controlled by EDM.
16000019 No matching ability is found.
16000050 Internal error.
16200001 The caller has been released.

示例:

import { ServiceExtensionAbility, Want } from '@kit.AbilityKit';
import { BusinessError } from '@kit.BasicServicesKit';

class EntryAbility extends ServiceExtensionAbility {
  onCreate() {
    let want: Want = {
      deviceId: '',
      bundleName: 'com.example.myapplication',
      abilityName: 'EntryAbility'
    };

    try {
      this.context.startServiceExtensionAbility(want, (error: BusinessError) => {
        if (error.code) {
          // 处理业务逻辑错误
          console.error(`startServiceExtensionAbility failed, error.code: ${error.code}, error.message: ${error.message}`);
          return;
        }
        // 执行正常业务
        console.info('startServiceExtensionAbility succeed');
      });
    } catch (paramError) {
      // 处理入参错误异常
      console.error(`error.code: ${(paramError as BusinessError).code}, error.message: ${(paramError as BusinessError).message}`);
    }
  }
}

ServiceExtensionContext.startServiceExtensionAbility

startServiceExtensionAbility(want: Want): Promise<void>

启动一个新的ServiceExtensionAbility。使用Promise异步回调。

系统能力:SystemCapability.Ability.AbilityRuntime.Core

系统接口:此接口为系统接口。

参数:

参数名 类型 必填 说明
want Want 启动Ability的Want信息。

返回值:

类型 说明
Promise&lt;void&gt; Promise对象。无返回结果的Promise对象。

错误码:

以下错误码详细介绍请参考通用错误码元能力子系统错误码

错误码ID 错误信息
201 The application does not have permission to call the interface.
202 The application is not system-app, can not use system-api.
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed.
16000001 The specified ability does not exist.
16000002 Incorrect ability type.
16000004 Cannot start an invisible component.
16000005 The specified process does not have the permission.
16000006 Cross-user operations are not allowed.
16000008 The crowdtesting application expires.
16000011 The context does not exist.
16000012 The application is controlled.
16000013 The application is controlled by EDM.
16000019 No matching ability is found.
16000050 Internal error.
16200001 The caller has been released.

示例:

import { ServiceExtensionAbility, Want } from '@kit.AbilityKit';
import { BusinessError } from '@kit.BasicServicesKit';

class EntryAbility extends ServiceExtensionAbility {
  onCreate() {
    let want: Want = {
      deviceId: '',
      bundleName: 'com.example.myapplication',
      abilityName: 'EntryAbility'
    };

    try {
      this.context.startServiceExtensionAbility(want)
        .then((data) => {
          // 执行正常业务
          console.info('startServiceExtensionAbility succeed');
        })
        .catch((error: BusinessError) => {
          // 处理业务逻辑错误
          console.error(`startServiceExtensionAbility failed, error.code: ${error.code}, error.message: ${error.message}`);
        });
    } catch (paramError) {
      // 处理入参错误异常
      console.error(`error.code: ${(paramError as BusinessError).code}, error.message: ${(paramError as BusinessError).message}`);
    }
  }
}

ServiceExtensionContext.startServiceExtensionAbilityWithAccount

startServiceExtensionAbilityWithAccount(want: Want, accountId: number, callback: AsyncCallback<void>): void

启动一个新的ServiceExtensionAbility。使用callback异步回调。

说明:

组件启动规则详见:组件启动规则(Stage模型)
当accountId为当前用户时,无需进行权限校验。

需要权限:ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS

系统能力:SystemCapability.Ability.AbilityRuntime.Core

系统接口:此接口为系统接口。

参数:

参数名 类型 必填 说明
want Want 启动Ability的Want信息。
accountId number 系统账号的账号ID,详情参考getCreatedOsAccountsCount
callback AsyncCallback<void> 回调函数。当启动一个新的ServiceExtensionAbility成功,err为undefined,否则为错误对象。

错误码:

以下错误码详细介绍请参考通用错误码元能力子系统错误码

错误码ID 错误信息
201 The application does not have permission to call the interface.
202 The application is not system-app, can not use system-api.
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed.
16000001 The specified ability does not exist.
16000002 Incorrect ability type.
16000004 Cannot start an invisible component.
16000005 The specified process does not have the permission.
16000006 Cross-user operations are not allowed.
16000008 The crowdtesting application expires.
16000011 The context does not exist.
16000012 The application is controlled.
16000013 The application is controlled by EDM.
16000019 No matching ability is found.
16000050 Internal error.
16200001 The caller has been released.

示例:

import { ServiceExtensionAbility, Want } from '@kit.AbilityKit';
import { BusinessError } from '@kit.BasicServicesKit';

class EntryAbility extends ServiceExtensionAbility {
  onCreate() {
    let want: Want = {
      deviceId: '',
      bundleName: 'com.example.myapplication',
      abilityName: 'EntryAbility'
    };
    let accountId = 100;

    try {
      this.context.startServiceExtensionAbilityWithAccount(want, accountId, (error: BusinessError) => {
        if (error.code) {
          // 处理业务逻辑错误
          console.error(`startServiceExtensionAbilityWithAccount failed, error.code: ${error.code}, error.message: ${error.message}`);
          return;
        }
        // 执行正常业务
        console.info('startServiceExtensionAbilityWithAccount succeed');
      });
    } catch (paramError) {
      // 处理入参错误异常
      console.error(`error.code: ${(paramError as BusinessError).code}, error.message: ${(paramError as BusinessError).message}`);
    }
  }
}

ServiceExtensionContext.startServiceExtensionAbilityWithAccount

startServiceExtensionAbilityWithAccount(want: Want, accountId: number): Promise<void>

启动一个新的ServiceExtensionAbility。使用Promise异步回调。

说明:

组件启动规则详见:组件启动规则(Stage模型)
当accountId为当前用户时,无需进行权限校验。

需要权限:ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS

系统能力:SystemCapability.Ability.AbilityRuntime.Core

系统接口:此接口为系统接口。

参数:

参数名 类型 必填 说明
want Want 启动Ability的Want信息。
accountId number 系统账号的账号ID,详情参考getCreatedOsAccountsCount

返回值:

类型 说明
Promise&lt;void&gt; Promise对象。无返回结果的Promise对象。

错误码:

以下错误码详细介绍请参考通用错误码元能力子系统错误码

错误码ID 错误信息
201 The application does not have permission to call the interface.
202 The application is not system-app, can not use system-api.
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed.
16000001 The specified ability does not exist.
16000002 Incorrect ability type.
16000004 Cannot start an invisible component.
16000005 The specified process does not have the permission.
16000006 Cross-user operations are not allowed.
16000008 The crowdtesting application expires.
16000011 The context does not exist.
16000012 The application is controlled.
16000013 The application is controlled by EDM.
16000019 No matching ability is found.
16000050 Internal error.
16200001 The caller has been released.

示例:

import { ServiceExtensionAbility, Want } from '@kit.AbilityKit';
import { BusinessError } from '@kit.BasicServicesKit';

class EntryAbility extends ServiceExtensionAbility {
  onCreate() {
    let want: Want = {
      deviceId: '',
      bundleName: 'com.example.myapplication',
      abilityName: 'EntryAbility'
    };
    let accountId = 100;

    try {
      this.context.startServiceExtensionAbilityWithAccount(want, accountId)
        .then((data: void) => {
          // 执行正常业务
          console.info('startServiceExtensionAbilityWithAccount succeed');
        })
        .catch((error: BusinessError) => {
          // 处理业务逻辑错误
          console.error(`startServiceExtensionAbilityWithAccount failed, error.code: ${error.code}, error.message: ${error.message}`);
        });
    } catch (paramError) {
      // 处理入参错误异常
      console.error(`error.code: ${(paramError as BusinessError).code}, error.message: ${(paramError as BusinessError).message}`);
    }
  }
}

ServiceExtensionContext.startAbilityAsCaller10+

startAbilityAsCaller(want: Want, callback: AsyncCallback<void>): void

使用设置的caller信息启动一个Ability,caller信息由Want携带,在系统服务层识别,Ability可以在onCreate生命周期的Want参数中获取到caller信息。使用该接口启动一个Ability时,Want的caller信息不会被当前自身的应用信息覆盖,系统服务层可获取到初始caller的信息。仅支持在主线程调用。使用callback异步回调。

说明:

组件启动规则详见:组件启动规则(Stage模型)

系统能力:SystemCapability.Ability.AbilityRuntime.Core

系统接口:此接口为系统接口。

参数:

参数名 类型 必填 说明
want Want 启动Ability的Want信息。
callback AsyncCallback&lt;void&gt; 回调函数。当启动Ability成功,err为undefined,否则为错误对象。

错误码:

以下错误码详细介绍请参考通用错误码元能力子系统错误码

错误码ID 错误信息
201 The application does not have permission to call the interface.
202 The application is not system-app, can not use system-api.
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed.
16000001 The specified ability does not exist.
16000002 Incorrect ability type.
16000004 Cannot start an invisible component.
16000005 The specified process does not have the permission.
16000006 Cross-user operations are not allowed.
16000008 The crowdtesting application expires.
16000009 An ability cannot be started or stopped in Wukong mode.
16000010 The call with the continuation and prepare continuation flag is forbidden.
16000011 The context does not exist.
16000012 The application is controlled.
16000013 The application is controlled by EDM.
16000050 Internal error.
16000053 The ability is not on the top of the UI.
16000055 Installation-free timed out.
16000071 App clone is not supported.
16000072 App clone or multi-instance is not supported.
16000073 The app clone index is invalid.
16000076 The app instance key is invalid.
16000077 The number of app instances reaches the limit.
16000078 The multi-instance is not supported.
16000079 The APP_INSTANCE_KEY cannot be specified.
16000080 Creating a new instance is not supported.
16200001 The caller has been released.

示例:

import { ServiceExtensionAbility, Want } from '@kit.AbilityKit';

class EntryAbility extends ServiceExtensionAbility {
  onCreate(want: Want) {
    // want包含启动该应用的Caller信息
    let localWant: Want = want;
    localWant.bundleName = 'com.example.demo';
    localWant.moduleName = 'entry';
    localWant.abilityName = 'TestAbility';

    // 使用启动方的Caller身份信息启动新Ability
    this.context.startAbilityAsCaller(localWant, (err) => {
      if (err && err.code != 0) {
        console.error('startAbilityAsCaller failed, err:' + JSON.stringify(err));
      } else {
        console.info('startAbilityAsCaller success.');
      }
    })
  }
}

ServiceExtensionContext.startAbilityAsCaller10+

startAbilityAsCaller(want: Want, options: StartOptions, callback: AsyncCallback<void>): void

使用设置的caller信息启动一个Ability,caller信息由Want携带,在系统服务层识别,Ability可以在onCreate生命周期的Want参数中获取到caller信息。使用该接口启动一个Ability时,Want的caller信息不会被当前自身的应用信息覆盖,系统服务层可获取到初始caller的信息。仅支持在主线程调用。使用callback异步回调。

说明:

组件启动规则详见:组件启动规则(Stage模型)

系统能力:SystemCapability.Ability.AbilityRuntime.Core

系统接口:此接口为系统接口。

参数:

参数名 类型 必填 说明
want Want 启动Ability的Want信息。
options StartOptions 启动Ability所携带的参数。
callback AsyncCallback&lt;void&gt; 回调函数。当启动Ability成功,err为undefined,否则为错误对象。

错误码:

以下错误码详细介绍请参考通用错误码元能力子系统错误码

错误码ID 错误信息
201 The application does not have permission to call the interface.
202 The application is not system-app, can not use system-api.
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed.
16000001 The specified ability does not exist.
16000004 Cannot start an invisible component.
16000005 The specified process does not have the permission.
16000006 Cross-user operations are not allowed.
16000008 The crowdtesting application expires.
16000009 An ability cannot be started or stopped in Wukong mode.
16000011 The context does not exist.
16000012 The application is controlled.
16000013 The application is controlled by EDM.
16000050 Internal error.
16000053 The ability is not on the top of the UI.
16000055 Installation-free timed out.
16000071 App clone is not supported.
16000072 App clone or multi-instance is not supported.
16000073 The app clone index is invalid.
16000076 The app instance key is invalid.
16000077 The number of app instances reaches the limit.
16000078 The multi-instance is not supported.
16000079 The APP_INSTANCE_KEY cannot be specified.
16000080 Creating a new instance is not supported.
16200001 The caller has been released.

示例:

import { ServiceExtensionAbility, Want, StartOptions } from '@kit.AbilityKit';

class EntryAbility extends ServiceExtensionAbility {
  onCreate(want: Want) {
    // want包含启动该应用的Caller信息
    let localWant: Want = want;
    localWant.bundleName = 'com.example.demo';
    localWant.moduleName = 'entry';
    localWant.abilityName = 'TestAbility';

    let option: StartOptions = {
      displayId: 0
    }

    // 使用启动方的Caller身份信息启动新Ability
    this.context.startAbilityAsCaller(localWant, option, (err) => {
      if (err && err.code != 0) {
        console.error('startAbilityAsCaller failed, err:' + JSON.stringify(err));
      } else {
        console.info('startAbilityAsCaller success.');
      }
    })
  }
}

ServiceExtensionContext.startAbilityAsCaller10+

startAbilityAsCaller(want: Want, options?: StartOptions): Promise<void>

使用设置的caller信息启动一个Ability,caller信息由Want携带,在系统服务层识别,Ability可以在onCreate生命周期的Want参数中获取到caller信息。使用该接口启动一个Ability时,Want的caller信息不会被当前自身的应用信息覆盖,系统服务层可获取到初始caller的信息。仅支持在主线程调用。使用Promise异步回调。

说明:

组件启动规则详见:组件启动规则(Stage模型)

系统能力:SystemCapability.Ability.AbilityRuntime.Core

系统接口:此接口为系统接口。

参数:

参数名 类型 必填 说明
want Want 启动Ability的Want信息。
options StartOptions 启动Ability所携带的参数。

返回值:

类型 说明
Promise&lt;void&gt; Promise对象。无返回结果的Promise对象。

错误码:

以下错误码详细介绍请参考通用错误码元能力子系统错误码

错误码ID 错误信息
201 The application does not have permission to call the interface.
202 The application is not system-app, can not use system-api.
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed.
16000001 The specified ability does not exist.
16000002 Incorrect ability type.
16000004 Cannot start an invisible component.
16000005 The specified process does not have the permission.
16000006 Cross-user operations are not allowed.
16000008 The crowdtesting application expires.
16000009 An ability cannot be started or stopped in Wukong mode.
16000010 The call with the continuation and prepare continuation flag is forbidden.
16000011 The context does not exist.
16000012 The application is controlled.
16000013 The application is controlled by EDM.
16000050 Internal error.
16000053 The ability is not on the top of the UI.
16000055 Installation-free timed out.
16000071 App clone is not supported.
16000072 App clone or multi-instance is not supported.
16000073 The app clone index is invalid.
16000076 The app instance key is invalid.
16000077 The number of app instances reaches the limit.
16000078 The multi-instance is not supported.
16000079 The APP_INSTANCE_KEY cannot be specified.
16000080 Creating a new instance is not supported.
16200001 The caller has been released.

示例:

import { ServiceExtensionAbility, Want, StartOptions } from '@kit.AbilityKit';
import { BusinessError } from '@kit.BasicServicesKit';

class EntryAbility extends ServiceExtensionAbility {
  onCreate(want: Want) {
    // want包含启动该应用的Caller信息
    let localWant: Want = want;
    localWant.bundleName = 'com.example.demo';
    localWant.moduleName = 'entry';
    localWant.abilityName = 'TestAbility';

    let option: StartOptions = {
      displayId: 0
    };

    // 使用启动方的Caller身份信息启动新Ability
    this.context.startAbilityAsCaller(localWant, option)
      .then(() => {
        console.info('startAbilityAsCaller success.');
      })
      .catch((err: BusinessError) => {
        console.error('startAbilityAsCaller failed, err:' + JSON.stringify(err));
      })
  }
}

ServiceExtensionContext.stopServiceExtensionAbility

stopServiceExtensionAbility(want: Want, callback: AsyncCallback<void>): void

停止同一应用程序内的服务。使用callback异步回调。

系统能力:SystemCapability.Ability.AbilityRuntime.Core

系统接口:此接口为系统接口。

参数:

参数名 类型 必填 说明
want Want 停止Ability的Want信息。
callback AsyncCallback<void> 回调函数。当停止同一应用程序内的服务成功,err为undefined,否则为错误对象。

错误码:

以下错误码详细介绍请参考通用错误码元能力子系统错误码

错误码ID 错误信息
201 The application does not have permission to call the interface.
202 The application is not system-app, can not use system-api.
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed.
16000001 The specified ability does not exist.
16000002 Incorrect ability type.
16000004 Cannot start an invisible component.
16000005 The specified process does not have the permission.
16000006 Cross-user operations are not allowed.
16000011 The context does not exist.
16000050 Internal error.
16200001 The caller has been released.

示例:

import { ServiceExtensionAbility, Want } from '@kit.AbilityKit';
import { BusinessError } from '@kit.BasicServicesKit';

class EntryAbility extends ServiceExtensionAbility {
  onCreate() {
    let want: Want = {
      deviceId: '',
      bundleName: 'com.example.myapplication',
      abilityName: 'EntryAbility'
    };

    try {
      this.context.stopServiceExtensionAbility(want, (error: BusinessError) => {
        if (error.code) {
          // 处理业务逻辑错误
          console.error(`stopServiceExtensionAbility failed, error.code: ${error.code}, error.message: ${error.message}`);
          return;
        }
        // 执行正常业务
        console.info('stopServiceExtensionAbility succeed');
      });
    } catch (paramError) {
      // 处理入参错误异常
      console.error(`error.code: ${(paramError as BusinessError).code}, error.message: ${(paramError as BusinessError).message}`);
    }
  }
}

ServiceExtensionContext.stopServiceExtensionAbility

stopServiceExtensionAbility(want: Want): Promise<void>

停止同一应用程序内的服务。使用Promise异步回调。

系统能力:SystemCapability.Ability.AbilityRuntime.Core

系统接口:此接口为系统接口。

参数:

参数名 类型 必填 说明
want Want 停止Ability的Want信息。

返回值:

类型 说明
Promise&lt;void&gt; Promise对象。无返回结果的Promise对象。

错误码:

以下错误码详细介绍请参考通用错误码元能力子系统错误码

错误码ID 错误信息
201 The application does not have permission to call the interface.
202 The application is not system-app, can not use system-api.
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed.
16000001 The specified ability does not exist.
16000002 Incorrect ability type.
16000004 Cannot start an invisible component.
16000005 The specified process does not have the permission.
16000006 Cross-user operations are not allowed.
16000011 The context does not exist.
16000050 Internal error.
16200001 The caller has been released.

示例:

import { ServiceExtensionAbility, Want } from '@kit.AbilityKit';
import { BusinessError } from '@kit.BasicServicesKit';

class EntryAbility extends ServiceExtensionAbility {
  onCreate() {
    let want: Want = {
      deviceId: '',
      bundleName: 'com.example.myapplication',
      abilityName: 'EntryAbility'
    };

    try {
      this.context.stopServiceExtensionAbility(want)
        .then(() => {
          // 执行正常业务
          console.info('stopServiceExtensionAbility succeed');
        })
        .catch((error: BusinessError) => {
          // 处理业务逻辑错误
          console.error(`stopServiceExtensionAbility failed, error.code: ${error.code}, error.message: ${error.message}`);
        });
    } catch (paramError) {
      // 处理入参错误异常
      console.error(`error.code: ${(paramError as BusinessError).code}, error.message: ${(paramError as BusinessError).message}`);
    }
  }
}

ServiceExtensionContext.stopServiceExtensionAbilityWithAccount

stopServiceExtensionAbilityWithAccount(want: Want, accountId: number, callback: AsyncCallback<void>): void

使用账户停止同一应用程序内的服务。使用callback异步回调。

说明:

当accountId为当前用户时,无需进行权限校验。

需要权限:ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS

系统能力:SystemCapability.Ability.AbilityRuntime.Core

系统接口:此接口为系统接口。

参数:

参数名 类型 必填 说明
want Want 停止Ability的Want信息。
accountId number 需要停止的系统账号的账号ID,详情参考getCreatedOsAccountsCount
callback AsyncCallback<void> 回调函数。当使用账户停止同一应用程序内的服务成功,err为undefined,否则为错误对象。

错误码:

以下错误码详细介绍请参考通用错误码元能力子系统错误码

错误码ID 错误信息
201 The application does not have permission to call the interface.
202 The application is not system-app, can not use system-api.
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed.
16000001 The specified ability does not exist.
16000002 Incorrect ability type.
16000004 Cannot start an invisible component.
16000005 The specified process does not have the permission.
16000006 Cross-user operations are not allowed.
16000011 The context does not exist.
16000050 Internal error.
16200001 The caller has been released.

示例:

import { ServiceExtensionAbility, Want } from '@kit.AbilityKit';
import { BusinessError } from '@kit.BasicServicesKit';

class EntryAbility extends ServiceExtensionAbility {
  onCreate() {
    let want: Want = {
      deviceId: '',
      bundleName: 'com.example.myapplication',
      abilityName: 'EntryAbility'
    };
    let accountId = 100;

    try {
      this.context.stopServiceExtensionAbilityWithAccount(want, accountId, (error: BusinessError) => {
        if (error.code) {
          // 处理业务逻辑错误
          console.error(`stopServiceExtensionAbilityWithAccount failed, error.code: ${error.code}, error.message: ${error.message}`);
          return;
        }
        // 执行正常业务
        console.info('stopServiceExtensionAbilityWithAccount succeed');
      });
    } catch (paramError) {
      // 处理入参错误异常
      console.error(`error.code: ${(paramError as BusinessError).code}, error.message: ${(paramError as BusinessError).message}`);
    }
  }
}

ServiceExtensionContext.stopServiceExtensionAbilityWithAccount

stopServiceExtensionAbilityWithAccount(want: Want, accountId: number): Promise<void>

使用账户停止同一应用程序内的服务。使用Promise异步回调。

说明:

当accountId为当前用户时,无需进行权限校验。

需要权限:ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS

系统能力:SystemCapability.Ability.AbilityRuntime.Core

系统接口:此接口为系统接口。

参数:

参数名 类型 必填 说明
want Want 停止Ability的Want信息。
accountId number 需要停止的系统账号的账号ID,详情参考getCreatedOsAccountsCount

返回值:

类型 说明
Promise&lt;void&gt; Promise对象。无返回结果的Promise对象。

错误码:

以下错误码详细介绍请参考通用错误码元能力子系统错误码

错误码ID 错误信息
201 The application does not have permission to call the interface.
202 The application is not system-app, can not use system-api.
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed.
16000001 The specified ability does not exist.
16000002 Incorrect ability type.
16000004 Cannot start an invisible component.
16000005 The specified process does not have the permission.
16000006 Cross-user operations are not allowed.
16000011 The context does not exist.
16000050 Internal error.
16200001 The caller has been released.

示例:

import { ServiceExtensionAbility, Want } from '@kit.AbilityKit';
import { BusinessError } from '@kit.BasicServicesKit';

class EntryAbility extends ServiceExtensionAbility {
  onCreate() {
    let want: Want = {
      deviceId: '',
      bundleName: 'com.example.myapplication',
      abilityName: 'EntryAbility'
    };
    let accountId = 100;

    try {
      this.context.stopServiceExtensionAbilityWithAccount(want, accountId)
        .then(() => {
          // 执行正常业务
          console.info('stopServiceExtensionAbilityWithAccount succeed');
        })
        .catch((error: BusinessError) => {
          // 处理业务逻辑错误
          console.error(`stopServiceExtensionAbilityWithAccount failed, error.code: ${error.code}, error.message: ${error.message}`);
        });
    } catch (paramError) {
      // 处理入参错误异常
      console.error(`error.code: ${(paramError as BusinessError).code}, error.message: ${(paramError as BusinessError).message}`);
    }
  }
}

ServiceExtensionContext.terminateSelf

terminateSelf(callback: AsyncCallback&lt;void&gt;): void

停止Ability自身。仅支持在主线程调用。使用callback异步回调。

系统能力:SystemCapability.Ability.AbilityRuntime.Core

系统接口:此接口为系统接口。

参数:

参数名 类型 必填 说明
callback AsyncCallback&lt;void&gt; 回调函数。当停止Ability自身成功,err为undefined,否则为错误对象。

错误码:

以下错误码详细介绍请参考通用错误码元能力子系统错误码

错误码ID 错误信息
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed.
16000009 An ability cannot be started or stopped in Wukong mode.
16000011 The context does not exist.
16000050 Internal error.

示例:

import { ServiceExtensionAbility } from '@kit.AbilityKit';
import { BusinessError } from '@kit.BasicServicesKit';

class EntryAbility extends ServiceExtensionAbility {
  onCreate() {
    this.context.terminateSelf((error: BusinessError) => {
      if (error.code) {
        // 处理业务逻辑错误
        console.error(`terminateSelf failed, error.code: ${error.code}, error.message: ${error.message}`);
        return;
      }
      // 执行正常业务
      console.info('terminateSelf succeed');
    });
  }
}

ServiceExtensionContext.terminateSelf

terminateSelf(): Promise&lt;void&gt;

停止Ability自身。仅支持在主线程调用。使用Promise异步回调。

系统能力:SystemCapability.Ability.AbilityRuntime.Core

系统接口:此接口为系统接口。

返回值:

类型 说明
Promise&lt;void&gt; Promise对象。无返回结果的Promise对象。

错误码:

以下错误码详细介绍请参考元能力子系统错误码

错误码ID 错误信息
16000009 An ability cannot be started or stopped in Wukong mode.
16000011 The context does not exist.
16000050 Internal error.

示例:

import { ServiceExtensionAbility } from '@kit.AbilityKit';
import { BusinessError } from '@kit.BasicServicesKit';

class EntryAbility extends ServiceExtensionAbility {
  onCreate() {
    this.context.terminateSelf().then(() => {
      // 执行正常业务
      console.info('terminateSelf succeed');
    }).catch((error: BusinessError) => {
      // 处理业务逻辑错误
      console.error(`terminateSelf failed, error.code: ${error.code}, error.message: ${error.message}`);
    });
  }
}

ServiceExtensionContext.connectServiceExtensionAbility

connectServiceExtensionAbility(want: Want, options: ConnectOptions): number

将当前Ability连接到一个ServiceExtensionAbility。仅支持在主线程调用。

说明:

组件启动规则详见:组件启动规则(Stage模型)

系统能力:SystemCapability.Ability.AbilityRuntime.Core

系统接口:此接口为系统接口。

参数:

参数名 类型 必填 说明
want Want Want类型参数,传入需要启动的Ability的信息,如Ability名称,Bundle名称等。
options ConnectOptions ConnectOptions类型的回调函数,返回服务连接成功、断开或连接失败后的信息。

返回值:

类型 说明
number 返回一个number,后续根据这个number去断开连接。

错误码:

以下错误码详细介绍请参考通用错误码元能力子系统错误码

错误码ID 错误信息
201 The application does not have permission to call the interface.
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed.
16000001 The specified ability does not exist.
16000002 Incorrect ability type.
16000004 Cannot start an invisible component.
16000005 The specified process does not have the permission.
16000006 Cross-user operations are not allowed.
16000008 The crowdtesting application expires.
16000053 The ability is not on the top of the UI.
16000055 Installation-free timed out.
16000011 The context does not exist.
16000050 Internal error.

示例:

import { ServiceExtensionAbility, Want, common } from '@kit.AbilityKit';
import { rpc } from '@kit.IPCKit';
import { BusinessError } from '@kit.BasicServicesKit';

let commRemote: rpc.IRemoteObject; // 断开连接时需要释放

class EntryAbility extends ServiceExtensionAbility {
  onCreate() {
    let want: Want = {
      bundleName: 'com.example.myapp',
      abilityName: 'MyAbility'
    };
    let options: common.ConnectOptions = {
      onConnect(elementName, remote) {
        commRemote = remote;
        console.info('----------- onConnect -----------');
      },
      onDisconnect(elementName) {
        console.info('----------- onDisconnect -----------');
      },
      onFailed(code) {
        console.error('----------- onFailed -----------');
      }
    };
    let connection: number;

    try {
      connection = this.context.connectServiceExtensionAbility(want, options);
    } catch (paramError) {
      // 处理入参错误异常
      console.error(`error.code: ${(paramError as BusinessError).code}, error.message: ${(paramError as BusinessError).message}`);
    }
  }
}

ServiceExtensionContext.connectServiceExtensionAbilityWithAccount

connectServiceExtensionAbilityWithAccount(want: Want, accountId: number, options: ConnectOptions): number

将当前Ability连接到一个指定account的ServiceExtensionAbility。仅支持在主线程调用。

当前仅在phone、tablet设备上生效。

说明:

组件启动规则详见:组件启动规则(Stage模型)
当accountId为当前用户时,无需进行权限校验。

需要权限:ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS

系统能力:SystemCapability.Ability.AbilityRuntime.Core

系统接口:此接口为系统接口。

参数:

参数名 类型 必填 说明
want Want 启动Ability的Want信息。
accountId number 系统账号的账号ID,详情参考getCreatedOsAccountsCount
options ConnectOptions 远端对象实例。

返回值:

类型 说明
number 返回Ability连接的结果code。

错误码:

以下错误码详细介绍请参考通用错误码元能力子系统错误码

错误码ID 错误信息
201 The application does not have permission to call the interface.
202 The application is not system-app, can not use system-api.
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed.
16000001 The specified ability does not exist.
16000002 Incorrect ability type.
16000004 Cannot start an invisible component.
16000005 The specified process does not have the permission.
16000006 Cross-user operations are not allowed.
16000008 The crowdtesting application expires.
16000053 The ability is not on the top of the UI.
16000055 Installation-free timed out.
16000011 The context does not exist.
16000050 Internal error.

示例:

import { ServiceExtensionAbility, Want, common } from '@kit.AbilityKit';
import { rpc } from '@kit.IPCKit';
import { BusinessError } from '@kit.BasicServicesKit';

let commRemote: rpc.IRemoteObject; // 断开连接时需要释放

class EntryAbility extends ServiceExtensionAbility {
  onCreate() {
    let want: Want = {
      deviceId: '',
      bundleName: 'com.example.myapplication',
      abilityName: 'EntryAbility'
    };
    let accountId = 100;
    let options: common.ConnectOptions = {
      onConnect(elementName, remote) {
        commRemote = remote;
        console.info('----------- onConnect -----------');
      },
      onDisconnect(elementName) {
        console.info('----------- onDisconnect -----------');
      },
      onFailed(code) {
        console.info('----------- onFailed -----------');
      }
    };
    let connection: number;

    try {
      connection = this.context.connectServiceExtensionAbilityWithAccount(want, accountId, options);
    } catch (paramError) {
      // 处理入参错误异常
      console.error(`error.code: ${(paramError as BusinessError).code}, error.message: ${(paramError as BusinessError).message}`);
    }
  }
}

ServiceExtensionContext.disconnectServiceExtensionAbility

disconnectServiceExtensionAbility(connection: number, callback:AsyncCallback&lt;void&gt;): void

将一个Ability与绑定的服务类型的Ability解绑,断开连接之后需要将连接成功时返回的remote对象置空。仅支持在主线程调用。使用callback异步回调。

系统能力:SystemCapability.Ability.AbilityRuntime.Core

系统接口:此接口为系统接口。

参数:

参数名 类型 必填 说明
connection number 在connectServiceExtensionAbility中返回的number。
callback AsyncCallback&lt;void&gt; 回调函数。当Ability与绑定服务类型的Ability解绑成功,err为undefined,否则为错误对象。

错误码:

以下错误码详细介绍请参考通用错误码元能力子系统错误码

错误码ID 错误信息
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed.
16000011 The context does not exist.
16000050 Internal error.

示例:

import { ServiceExtensionAbility } from '@kit.AbilityKit';
import { rpc } from '@kit.IPCKit';
import { BusinessError } from '@kit.BasicServicesKit';

let commRemote: rpc.IRemoteObject|null; // 断开连接时需要释放

class EntryAbility extends ServiceExtensionAbility {
  onCreate() {
    // connection为connectServiceExtensionAbility中的返回值
    let connection = 1;
    try {
      this.context.disconnectServiceExtensionAbility(connection, (error: BusinessError) => {
        commRemote = null;
        if (error.code) {
          // 处理业务逻辑错误
          console.error(`disconnectServiceExtensionAbility failed, error.code: ${error.code}, error.message: ${error.message}`);
          return;
        }
        // 执行正常业务
        console.info('disconnectServiceExtensionAbility succeed');
      });
    } catch (paramError) {
      commRemote = null;
      // 处理入参错误异常
      console.error(`error.code: ${(paramError as BusinessError).code}, error.message: ${(paramError as BusinessError).message}`);
    }
  }
}

ServiceExtensionContext.disconnectServiceExtensionAbility

disconnectServiceExtensionAbility(connection: number): Promise&lt;void&gt;

将一个Ability与绑定的服务类型的Ability解绑,断开连接之后需要将连接成功时返回的remote对象置空。仅支持在主线程调用。使用Promise异步回调。

系统能力:SystemCapability.Ability.AbilityRuntime.Core

系统接口:此接口为系统接口。

参数:

参数名 类型 必填 说明
connection number 在connectServiceExtensionAbility中返回的number。

返回值:

类型 说明
Promise&lt;void&gt; Promise对象。无返回结果的Promise对象。

错误码:

以下错误码详细介绍请参考通用错误码元能力子系统错误码

错误码ID 错误信息
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed.
16000011 The context does not exist.
16000050 Internal error.

示例:

import { ServiceExtensionAbility } from '@kit.AbilityKit';
import { rpc } from '@kit.IPCKit';
import { BusinessError } from '@kit.BasicServicesKit';

let commRemote: rpc.IRemoteObject|null; // 断开连接时需要释放

class EntryAbility extends ServiceExtensionAbility {
  onCreate() {
    // connection为connectServiceExtensionAbility中的返回值
    let connection = 1;
    try {
      this.context.disconnectServiceExtensionAbility(connection)
        .then(() => {
          commRemote = null;
          // 执行正常业务
          console.info('disconnectServiceExtensionAbility succeed');
        })
        .catch((error: BusinessError) => {
          commRemote = null;
          // 处理业务逻辑错误
          console.error(`disconnectServiceExtensionAbility failed, error.code: ${error.code}, error.message: ${error.message}`);
        });
    } catch (paramError) {
      commRemote = null;
      // 处理入参错误异常
      console.error(`error.code: ${(paramError as BusinessError).code}, error.message: ${(paramError as BusinessError).message}`);
    }
  }
}

ServiceExtensionContext.startAbilityByCall

startAbilityByCall(want: Want): Promise&lt;Caller&gt;

启动指定Ability至前台或后台,同时获取其Caller通信接口,调用方可使用Caller与被启动的Ability进行通信。仅支持在主线程调用。使用Promise异步回调。

该接口不支持拉起启动模式为specified模式的UIAbility。

使用规则: - 调用方应用位于后台时,使用该接口启动Ability需申请ohos.permission.START_ABILITIES_FROM_BACKGROUND权限。 - 跨应用场景下,目标Ability的exported属性若配置为false,调用方应用需申请ohos.permission.START_INVISIBLE_ABILITY权限。 - 同设备与跨设备场景下,该接口的使用规则存在差异,详见:组件启动规则(Stage模型)

需要权限:ohos.permission.ABILITY_BACKGROUND_COMMUNICATION

系统能力:SystemCapability.Ability.AbilityRuntime.Core

系统接口:此接口为系统接口。

参数:

参数名 类型 必填 说明
want Want 传入需要启动的Ability的信息,包含abilityName、moduleName、bundleName、deviceId、parameters(可选),parameters缺省或为空表示后台启动Ability。

返回值:

类型 说明
Promise&lt;Caller&gt; Promise对象,返回要通讯的caller对象。

错误码:

以下错误码详细介绍请参考通用错误码元能力子系统错误码

错误码ID 错误信息
201 The application does not have permission to call the interface.
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed.
16000001 The specified ability does not exist.
16000002 Incorrect ability type.
16000004 Cannot start an invisible component.
16000006 Cross-user operations are not allowed.
16000008 The crowdtesting application expires.
16000011 The context does not exist.
16000050 Internal error.
16200001 The caller has been released.

示例:

后台启动:

import { ServiceExtensionAbility, Caller, Want } from '@kit.AbilityKit';
import { BusinessError } from '@kit.BasicServicesKit';

class EntryAbility extends ServiceExtensionAbility {
  onCreate() {
    let caller: Caller;
    // 后台启动Ability,不配置parameters
    let wantBackground: Want = {
      bundleName: 'com.example.myservice',
      moduleName: 'entry',
      abilityName: 'EntryAbility',
      deviceId: ''
    };

    try {
      this.context.startAbilityByCall(wantBackground)
        .then((obj: Caller) => {
          // 执行正常业务
          caller = obj;
          console.info('startAbilityByCall succeed');
        }).catch((error: BusinessError) => {
        // 处理业务逻辑错误
        console.error(`startAbilityByCall failed, error.code: ${error.code}, error.message: ${error.message}`);
      });
    } catch (paramError) {
      // 处理入参错误异常
      console.error(`error.code: ${(paramError as BusinessError).code}, error.message: ${(paramError as BusinessError).message}`);
    }
  }
}

前台启动:

import { ServiceExtensionAbility, Caller, Want } from '@kit.AbilityKit';
import { BusinessError } from '@kit.BasicServicesKit';

class EntryAbility extends ServiceExtensionAbility {
  onCreate() {
    let caller: Caller;
    // 前台启动Ability,将parameters中的'ohos.aafwk.param.callAbilityToForeground'配置为true
    let wantForeground: Want = {
      bundleName: 'com.example.myservice',
      moduleName: 'entry',
      abilityName: 'EntryAbility',
      deviceId: '',
      parameters: {
        'ohos.aafwk.param.callAbilityToForeground': true
      }
    };

    try {
      this.context.startAbilityByCall(wantForeground)
        .then((obj: Caller) => {
          // 执行正常业务
          caller = obj;
          console.info('startAbilityByCall succeed');
        }).catch((error: BusinessError) => {
        // 处理业务逻辑错误
        console.error(`startAbilityByCall failed, error.code: ${error.code}, error.message: ${error.message}`);
      });
    } catch (paramError) {
      // 处理入参错误异常
      console.error(`error.code: ${(paramError as BusinessError).code}, error.message: ${(paramError as BusinessError).message}`);
    }
  }
}

ServiceExtensionContext.startRecentAbility

startRecentAbility(want: Want, callback: AsyncCallback<void>): void

启动一个指定的Ability,如果该Ability有多个实例,将拉起最近启动的那个实例。仅支持在主线程调用。使用callback异步回调。

说明:

组件启动规则详见:组件启动规则(Stage模型)

系统能力:SystemCapability.Ability.AbilityRuntime.Core

系统接口:此接口为系统接口。

参数:

参数名 类型 必填 说明
want Want 需要启动Ability的Want信息。
callback AsyncCallback<void> 回调函数。当启动一个指定的Ability成功,err为undefined,否则为错误对象。

错误码:

以下错误码详细介绍请参考通用错误码元能力子系统错误码

错误码ID 错误信息
201 The application does not have permission to call the interface.
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed.
16000001 The specified ability does not exist.
16000002 Incorrect ability type.
16000004 Cannot start an invisible component.
16000005 The specified process does not have the permission.
16000006 Cross-user operations are not allowed.
16000008 The crowdtesting application expires.
16000009 An ability cannot be started or stopped in Wukong mode.
16000010 The call with the continuation and prepare continuation flag is forbidden.
16000011 The context does not exist.
16000050 Internal error.
16000053 The ability is not on the top of the UI.
16000055 Installation-free timed out.
16000071 App clone is not supported.
16000072 App clone or multi-instance is not supported.
16000073 The app clone index is invalid.
16000076 The app instance key is invalid.
16000077 The number of app instances reaches the limit.
16000078 The multi-instance is not supported.
16000079 The APP_INSTANCE_KEY cannot be specified.
16000080 Creating a new instance is not supported.
16200001 The caller has been released.

示例:

import { ServiceExtensionAbility, Want } from '@kit.AbilityKit';
import { BusinessError } from '@kit.BasicServicesKit';

class EntryAbility extends ServiceExtensionAbility {
  onCreate() {
    let want: Want = {
      bundleName: 'com.example.myapplication',
      abilityName: 'EntryAbility'
    };

    try {
      this.context.startRecentAbility(want, (err: BusinessError) => {
        if (err.code) {
          // 处理业务逻辑错误
          console.error(`startRecentAbility failed, code is ${err.code}, message is ${err.message}`);
          return;
        }
        // 执行正常业务
        console.info('startRecentAbility succeed');
      });
    } catch (err) {
      // 处理入参错误异常
      let code = (err as BusinessError).code;
      let message = (err as BusinessError).message;
      console.error(`startRecentAbility failed, code is ${code}, message is ${message}`);
    }
  }
}

ServiceExtensionContext.startRecentAbility

startRecentAbility(want: Want, options: StartOptions, callback: AsyncCallback<void>): void

启动一个指定的Ability,如果该Ability有多个实例,将拉起最近启动的那个实例。仅支持在主线程调用。使用callback异步回调。

当开发者需要携带启动参数时可以选择此API。

说明:

组件启动规则详见:组件启动规则(Stage模型)

系统能力:SystemCapability.Ability.AbilityRuntime.Core

系统接口:此接口为系统接口。

参数:

参数名 类型 必填 说明
want Want 需要启动Ability的Want信息。
options StartOptions 启动Ability所携带的参数。
callback AsyncCallback<void> 回调函数。当启动一个指定的Ability成功,err为undefined,否则为错误对象。

错误码:

以下错误码详细介绍请参考通用错误码元能力子系统错误码

错误码ID 错误信息
201 The application does not have permission to call the interface.
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed.
16000001 The specified ability does not exist.
16000004 Cannot start an invisible component.
16000005 The specified process does not have the permission.
16000006 Cross-user operations are not allowed.
16000008 The crowdtesting application expires.
16000009 An ability cannot be started or stopped in Wukong mode.
16000011 The context does not exist.
16000050 Internal error.
16000053 The ability is not on the top of the UI.
16000055 Installation-free timed out.
16000071 App clone is not supported.
16000072 App clone or multi-instance is not supported.
16000073 The app clone index is invalid.
16000076 The app instance key is invalid.
16000077 The number of app instances reaches the limit.
16000078 The multi-instance is not supported.
16000079 The APP_INSTANCE_KEY cannot be specified.
16000080 Creating a new instance is not supported.
16200001 The caller has been released.

示例:

import { ServiceExtensionAbility, Want, StartOptions } from '@kit.AbilityKit';
import { BusinessError } from '@kit.BasicServicesKit';

class EntryAbility extends ServiceExtensionAbility {
  onCreate() {
    let want: Want = {
      deviceId: '',
      bundleName: 'com.example.myapplication',
      abilityName: 'EntryAbility'
    };
    let options: StartOptions = {
      windowMode: 0
    };

    try {
      this.context.startRecentAbility(want, options, (err: BusinessError) => {
        if (err.code) {
          // 处理业务逻辑错误
          console.error(`startRecentAbility failed, code is ${err.code}, message is ${err.message}`);
          return;
        }
        // 执行正常业务
        console.info('startRecentAbility succeed');
      });
    } catch (err) {
      // 处理入参错误异常
      let code = (err as BusinessError).code;
      let message = (err as BusinessError).message;
      console.error(`startRecentAbility failed, code is ${code}, message is ${message}`);
    }
  }
}

ServiceExtensionContext.startRecentAbility

startRecentAbility(want: Want, options?: StartOptions): Promise<void>

启动一个指定的Ability,如果该Ability有多个实例,将拉起最近启动的那个实例。使用Promise异步回调。仅支持在主线程调用。

说明:

组件启动规则详见:组件启动规则(Stage模型)

系统能力:SystemCapability.Ability.AbilityRuntime.Core

系统接口:此接口为系统接口。

参数:

参数名 类型 必填 说明
want Want 需要启动Ability的Want信息。
options StartOptions 启动Ability所携带的参数。

错误码:

以下错误码详细介绍请参考通用错误码元能力子系统错误码

错误码ID 错误信息
201 The application does not have permission to call the interface.
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed.
16000001 The specified ability does not exist.
16000002 Incorrect ability type.
16000004 Cannot start an invisible component.
16000005 The specified process does not have the permission.
16000006 Cross-user operations are not allowed.
16000008 The crowdtesting application expires.
16000009 An ability cannot be started or stopped in Wukong mode.
16000010 The call with the continuation and prepare continuation flag is forbidden.
16000011 The context does not exist.
16000050 Internal error.
16000053 The ability is not on the top of the UI.
16000055 Installation-free timed out.
16000071 App clone is not supported.
16000072 App clone or multi-instance is not supported.
16000073 The app clone index is invalid.
16000076 The app instance key is invalid.
16000077 The number of app instances reaches the limit.
16000078 The multi-instance is not supported.
16000079 The APP_INSTANCE_KEY cannot be specified.
16000080 Creating a new instance is not supported.
16200001 The caller has been released.

示例:

import { ServiceExtensionAbility, Want, StartOptions } from '@kit.AbilityKit';
import { BusinessError } from '@kit.BasicServicesKit';

class EntryAbility extends ServiceExtensionAbility {
  onCreate() {
    let want: Want = {
      bundleName: 'com.example.myapplication',
      abilityName: 'EntryAbility'
    };
    let options: StartOptions = {
      windowMode: 0,
    };

    try {
      this.context.startRecentAbility(want, options)
        .then(() => {
          // 执行正常业务
          console.info('startRecentAbility succeed');
        })
        .catch((err: BusinessError) => {
          // 处理业务逻辑错误
          console.error(`startRecentAbility failed, code is ${err.code}, message is ${err.message}`);
        });
    } catch (err) {
      // 处理入参错误异常
      let code = (err as BusinessError).code;
      let message = (err as BusinessError).message;
      console.error(`startRecentAbility failed, code is ${code}, message is ${message}`);
    }
  }
}

ServiceExtensionContext.startAbilityByCallWithAccount10+

startAbilityByCallWithAccount(want: Want, accountId: number): Promise&lt;Caller&gt;

根据accountId对指定的Ability进行call调用,并且可以使用返回的Caller通信接口与被调用方进行通信。仅支持在主线程调用。使用Promise异步回调。

该接口不支持拉起启动模式为specified模式的UIAbility。

使用规则: - 跨用户场景下,Call调用目标Ability时,调用方应用需同时申请ohos.permission.ABILITY_BACKGROUND_COMMUNICATIONohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS权限。 - 调用方应用位于后台时,使用该接口启动Ability需申请ohos.permission.START_ABILITIES_FROM_BACKGROUND权限。 - 跨应用场景下,目标Ability的exported属性若配置为false,调用方应用需申请ohos.permission.START_INVISIBLE_ABILITY权限。 - 同设备与跨设备场景下,该接口的使用规则存在差异,详见:组件启动规则(Stage模型)

需要权限:ohos.permission.ABILITY_BACKGROUND_COMMUNICATION, ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS

系统能力:SystemCapability.Ability.AbilityRuntime.Core

系统接口:此接口为系统接口。

参数:

参数名 类型 必填 说明
want Want 传入需要启动的Ability的信息,包含abilityName、moduleName、bundleName、deviceId(可选)、parameters(可选),其中deviceId缺省或为空表示启动本地Ability,parameters缺省或为空表示后台启动Ability。
accountId number 系统账号的账号ID,-1表示当前活动用户,详情参考getCreatedOsAccountsCount

返回值:

类型 说明
Promise&lt;Caller&gt; Promise对象,返回要通讯的caller对象。

错误码:

以下错误码详细介绍请参考通用错误码元能力子系统错误码

错误码ID 错误信息
201 The application does not have permission to call the interface.
202 The application is not system-app, can not use system-api.
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed.
16000001 The specified ability does not exist.
16000002 Incorrect ability type.
16000004 Cannot start an invisible component.
16000005 Static permission denied. The specified process does not have the permission.
16000006 Cross-user operations are not allowed.
16000008 The crowdtesting application expires.
16000011 The context does not exist.
16000012 The application is controlled.
16000013 The application is controlled by EDM.
16000050 Internal error.
16200001 The caller has been released.

示例:

import { ServiceExtensionAbility, Want, Caller } from '@kit.AbilityKit';
import { BusinessError } from '@kit.BasicServicesKit';

class EntryAbility extends ServiceExtensionAbility {
  onCreate() {
    let caller: Caller;
    // 系统账号的账号ID, -1表示当前激活用户
    let accountId = -1;
    // 指定启动的Ability
    let want: Want = {
      bundleName: 'com.acts.actscalleeabilityrely',
      moduleName: 'entry',
      abilityName: 'EntryAbility',
      deviceId: '',
      parameters: {
        // 'ohos.aafwk.param.callAbilityToForeground' 值设置为true时为前台启动, 设置false或不设置为后台启动
        'ohos.aafwk.param.callAbilityToForeground': true
      }
    };

    try {
      this.context.startAbilityByCallWithAccount(want, accountId)
        .then((obj: Caller) => {
          // 执行正常业务
          caller = obj;
          console.info('startAbilityByCallWithAccount succeed');
        }).catch((error: BusinessError) => {
        // 处理业务逻辑错误
        console.error(`startAbilityByCallWithAccount failed, error.code: ${error.code}, error.message: ${error.message}`);
      });
    } catch (paramError) {
      // 处理入参错误异常
      console.error(`error.code: ${(paramError as BusinessError).code}, error.message: ${(paramError as BusinessError).message}`);
    }
  }
}

ServiceExtensionContext.requestModalUIExtension11+

requestModalUIExtension(pickerWant: Want): Promise<void>

请求在指定的前台应用上拉起对应类型的UIExtensionAbility。其中,前台应用通过want.parameters中bundleName来指定,如果未指定前台应用、bundleName指定的应用未在前台或指定的前台应用的bundleName不正确,则在系统界面上直接拉起UIExtensionAbility;被拉起的UIExtensionAbility通过Want中bundleName、abilityName、moduleName字段共同确定,同时需要通过want.parameters中的ability.want.params.uiExtensionType字段配置UIExtensionAbility的类型。仅支持在主线程调用。使用promise形式异步回调。

在前台应用上拉起UIExtensionAility之前,必须确保该应用已完成页面初始化,否则将导致拉起失败、并出现”uiContent is nullptr”的报错信息。应用可通过监听页面加载状态来判断拉起UIExtensionAbility的时机,页面初始化成功后会出现关键日志信息”UIContentImpl: focus again”。

说明:

组件启动规则详见:组件启动规则(Stage模型)

系统能力:SystemCapability.Ability.AbilityRuntime.Core

系统接口:此接口为系统接口。

参数:

参数名 类型 必填 说明
pickerWant Want 拉起UIExtension的Want信息。

返回值:

类型 说明
Promise&lt;void&gt; Promise对象。无返回结果的Promise对象。

错误码:

以下错误码详细介绍请参考通用错误码元能力子系统错误码

错误码ID 错误信息
202 The application is not system-app, can not use system-api.
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed.
16000050 Internal error.

示例:

import { ServiceExtensionAbility, Want } from '@kit.AbilityKit';
import { BusinessError } from '@kit.BasicServicesKit';

class ServiceExtension extends ServiceExtensionAbility {
  onCreate() {
    let pickerWant: Want = {
      bundleName: 'com.example.myapplication',
      abilityName: 'UIExtAbility',
      moduleName: 'entry_test',
      parameters: {
        'bundleName': 'com.example.myapplication',
        //与com.example.myapplication.UIExtAbility配置的type相同
        'ability.want.params.uiExtensionType': 'sys/commonUI'
      }
    };

    try {
      this.context.requestModalUIExtension(pickerWant)
        .then(() => {
          // 执行正常业务
          console.info('requestModalUIExtension succeed');
        })
        .catch((err: BusinessError) => {
          // 处理业务逻辑错误
          console.error(`requestModalUIExtension failed, code is ${err.code}, message is ${err.message}`);
        });
    } catch (err) {
      // 处理入参错误异常
      let code = (err as BusinessError).code;
      let message = (err as BusinessError).message;
      console.error(`requestModalUIExtension failed, code is ${code}, message is ${message}`);
    }
  }
}

ServiceExtensionContext.requestModalUIExtension11+

requestModalUIExtension(pickerWant: Want, callback: AsyncCallback<void>): void

请求在指定的前台应用上拉起对应类型的UIExtensionAbility。其中,前台应用通过want.parameters中bundleName来指定,如果未指定前台应用、bundleName指定的应用未在前台或指定的前台应用的bundleName不正确,则在系统界面上直接拉起UIExtensionAbility;被拉起的UIExtensionAbility通过Want中bundleName、abilityName、moduleName字段共同确定,同时需要通过want.parameters中的ability.want.params.uiExtensionType字段配置UIExtensionAbility的类型。仅支持在主线程调用。使用callback形式异步回调。

在前台应用上拉起UIExtensionAility之前,必须确保该应用已完成页面初始化,否则将导致拉起失败、并出现”uiContent is nullptr”的报错信息。应用可通过监听页面加载状态来判断拉起UIExtensionAbility的时机,页面初始化成功后会出现关键日志信息”UIContentImpl: focus again”。

说明:

组件启动规则详见:组件启动规则(Stage模型)

系统能力:SystemCapability.Ability.AbilityRuntime.Core

系统接口:此接口为系统接口。

参数:

参数名 类型 必填 说明
pickerWant Want 拉起UIExtension的Want信息。
callback AsyncCallback&lt;void&gt; 回调函数。当拉起UIExtension成功,err为undefined,否则为错误对象。

错误码:

以下错误码详细介绍请参考通用错误码元能力子系统错误码

错误码ID 错误信息
202 The application is not system-app, can not use system-api.
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed.
16000050 Internal error.

示例:

import { ServiceExtensionAbility, Want } from '@kit.AbilityKit';
import { BusinessError } from '@kit.BasicServicesKit';

class ServiceExtension extends ServiceExtensionAbility {
  onCreate() {
    let pickerWant: Want = {
      bundleName: 'com.example.myapplication',
      abilityName: 'com.example.myapplication.UIExtAbility',
      moduleName: 'entry_test',
      parameters: {
        'bundleName': 'com.example.myapplication',
        //与com.example.myapplication.UIExtAbility配置的type相同
        'ability.want.params.uiExtensionType': 'sys/commonUI'
      }
    };

    try {
      this.context.requestModalUIExtension(pickerWant, (err: BusinessError) => {
        if (err.code) {
          // 处理业务逻辑错误
          console.error(`requestModalUIExtension failed, code is ${err.code}, message is ${err.message}`);
          return;
        }
        // 执行正常业务
        console.info('requestModalUIExtension succeed');
      });
    } catch (err) {
      // 处理入参错误异常
      let code = (err as BusinessError).code;
      let message = (err as BusinessError).message;
      console.error(`requestModalUIExtension failed, code is ${code}, message is ${message}`);
    }
  }
}

ServiceExtensionContext.openLink12+

openLink(link:string, options?: OpenLinkOptions): Promise&lt;void&gt;

通过AppLinking启动UIAbility。仅支持在主线程调用。使用Promise异步回调。

通过在link字段中传入标准格式的URL,基于隐式Want匹配规则拉起目标UIAbility。目标方必须具备以下过滤器特征,才能处理AppLinking链接: - “actions”列表中包含”ohos.want.action.viewData”。 - “entities”列表中包含”entity.system.browsable”。 - “uris”列表中包含”scheme”为”https”且”domainVerify”为true的元素。

传入的参数不合法时,如未设置必选参数或link字符串不是标准格式的URL,接口会直接抛出异常。参数校验通过,拉起目标方时出现的错误通过promise返回错误信息。

说明:

组件启动规则详见:组件启动规则(Stage模型)

系统能力:SystemCapability.Ability.AbilityRuntime.Core

系统接口:此接口为系统接口。

参数:

参数名 类型 必填 说明
link string 指示要打开的标准格式URL。
options OpenLinkOptions 打开URL的选项参数。

返回值:

类型 说明
Promise&lt;void&gt; Promise对象。无返回结果的Promise对象。

错误码:

以下错误码详细介绍请参考通用错误码元能力子系统错误码

错误码ID 错误信息
201 The application does not have permission to call the interface.
202 The application is not system-app, can not use system-api.
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed.
16000001 The specified ability does not exist.
16000002 Incorrect ability type.
16000004 Cannot start an invisible component.
16000005 The specified process does not have the permission.
16000006 Cross-user operations are not allowed.
16000008 The crowdtesting application expires.
16000009 An ability cannot be started or stopped in Wukong mode.
16000010 The call with the continuation and prepare continuation flag is forbidden.
16000011 The context does not exist.
16000012 The application is controlled.
16000013 The application is controlled by EDM.
16000019 No matching ability is found.
16200001 The caller has been released.

示例:

import { ServiceExtensionAbility, Want, OpenLinkOptions } from '@kit.AbilityKit';
import { BusinessError } from '@kit.BasicServicesKit';

function log(info: string) {
  console.error(`[ServiceExtApp]:: ${JSON.stringify(info)}`);
}

export default class ServiceExtAbility extends ServiceExtensionAbility {
  onCreate(want: Want) {
    log(`ServiceExtAbility OnCreate`);
  }

  onRequest(want: Want, startId: number) {
    log(`ServiceExtAbility onRequest`);
    let link: string = 'https://www.example.com';
    let openLinkOptions: OpenLinkOptions = {
      appLinkingOnly: false
    };
    try {
      this.context.openLink(
        link,
        openLinkOptions
      ).then(() => {
        log(`open link success.`);
      }).catch((err: BusinessError) => {
        log(`open link failed, errCode ${JSON.stringify(err.code)}`);
      });
    }
    catch (e) {
      log(`exception occured, errCode ${JSON.stringify(e.code)}`);
    }
  }

  onDestroy() {
    log(`ServiceExtAbility onDestroy`);
  }
}

ServiceExtensionContext.preStartMission12+

preStartMission(bundleName:string, moduleName: string, abilitName: string, startTime: string): Promise&lt;void&gt;

打开原子化服务跳过loading框并预打开窗口,使用Promise异步回调。

参数校验通过,拉起目标方时出现的错误需要通过异常机制捕获。

需要权限:ohos.permission.PRE_START_ATOMIC_SERVICE

系统能力:SystemCapability.Ability.AbilityRuntime.Core

系统接口:此接口为系统接口。

参数:

参数名 类型 必填 说明
bundleName string 打开原子化服务对应的包名。
moduleName string 打开原子化服务对应的模块名。
abilityName string 打开原子化服务对应的能力名。
startTime string 打开原子化服务对应的开始时间,单位为毫秒级的时间戳。

返回值:

类型 说明
Promise&lt;void&gt; Promise对象。无返回结果的Promise对象。

错误码:

以下错误码详细介绍请参考通用错误码元能力子系统错误码

错误码ID 错误信息
201 The application does not have permission to call the interface.
202 The application is not system-app, can not use system-api.
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed.
16300007 The target free-installation task does not exist.
16000011 The context does not exist.

示例:

import { ServiceExtensionAbility, Want } from '@kit.AbilityKit';
import { BusinessError } from '@kit.BasicServicesKit';

function log(info: string) {
  console.error(`[ServiceExtApp]:: ${JSON.stringify(info)}`);
}

export default class ServiceExtAbility extends ServiceExtensionAbility {
  onCreate(want: Want) {
    log(`ServiceExtAbility OnCreate`);
  }

  onRequest(want: Want, startId: number) {
    log(`ServiceExtAbility onRequest`);
    try {
      this.context.preStartMission(
        want.bundleName,
        want.moduleName,
        want.abilityName,
        want.parameters["ohos.aafwk.param.startTime"]
      ).then(() => {
        log(`pre-start mission success.`);
      }).catch((err: BusinessError) => {
        log(`pre-start mission failed, errCode ${JSON.stringify(err.code)}`);
      });
    }
    catch (e) {
      log(`exception occured, errCode ${JSON.stringify(e.code)}`);
    }
  }

  onDestroy() {
    log(`ServiceExtAbility onDestroy`);
  }
}

ServiceExtensionContext.startUIServiceExtensionAbility14+

startUIServiceExtensionAbility(want: Want): Promise&lt;void&gt;

启动一个新的UIServiceExtensionAbility。使用Promise异步回调。

说明:

组件启动规则详见:组件启动规则(Stage模型)

系统能力:SystemCapability.Ability.AbilityRuntime.Core

系统接口:此接口为系统接口。

参数: |参数名|类型|只读|可选|说明 | |——|—-|—-|——————–|——————–| |want |Want|是|否|Want类型参数,传入需要启动的Ability的信息,如Ability名称,Bundle名称等。|

返回值:

类型 说明
Promise&lt;void&gt; Promise对象。无返回结果的Promise对象。

错误码:

以下错误码详细介绍请参考通用错误码元能力子系统错误码。 |错误码ID|错误信息 | |——–|———————————————————————| |201 |The application does not have permission to call the interface. | |202 |The application is not system-app, can not use system-api. | |401 |Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed.| |801 |The Ability is not supported. | |16000001|The specified ability does not exist. | |16000002|Incorrect ability type. | |16000004|Cannot start an invisible component. | |16000005|The specified process does not have the permission.| |16000006|Cross-user operations are not allowed. | |16000008|The crowdtesting application expires. | |16000011|The context does not exist. | |16000012|The application is controlled. | |16000013|The application is controlled by EDM. | |16000019|No matching ability is found. | |16000050|Internal error. | |16200001|The caller has been released. |

示例:

import { BusinessError } from '@ohos.base';
import { ServiceExtensionAbility, Want } from '@kit.AbilityKit';

export default class MyServiceExtensionAbility extends ServiceExtensionAbility {
  onRequest(want: Want, startId: number) {
    const startWant: Want = {
      bundleName: 'com.example.myapplication',
      abilityName: 'UIServiceExtensionAbility'
    }
    // 启动一个UIServiceExtensionAbility
    this.context.startUIServiceExtensionAbility(startWant).then(() => {
      console.info('succeeded');
    }).catch((error: BusinessError) => {
      console.error(`error code: ${error.code}, error essage : ${error.message}`);
    })
  }
}

ServiceExtensionContext.openAtomicService18+

openAtomicService(appId: string, options?: AtomicServiceOptions): Promise&lt;void&gt;

通过应用ID,拉起原子化服务。使用Promise异步回调。

说明:

组件启动规则详见:组件启动规则(Stage模型)

系统能力:SystemCapability.Ability.AbilityRuntime.Core

系统接口:此接口为系统接口。

参数:

参数名 类型 必填 说明
appId string 应用的唯一标识,由云端统一分配。
options AtomicServiceOptions 跳出式启动原子化服务所携带的参数。

返回值:

类型 说明
Promise&lt;void&gt; Promise对象。无返回结果的Promise对象。

错误码:

以下错误码详细介绍请参考通用错误码元能力子系统错误码

错误码ID 错误信息
201 The application does not have permission to call the interface.
401 Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types.
16000002 Incorrect ability type.
16000004 Cannot start an invisible component.
16000011 The context does not exist.
16000012 The application is controlled.
16000050 Internal error.
16200001 The caller has been released.

示例:

import { ServiceExtensionAbility, AtomicServiceOptions } from '@kit.AbilityKit';
import { BusinessError } from '@kit.BasicServicesKit';

export default class ServiceExtension extends ServiceExtensionAbility {
  onRequest(want: Want, startId: number) {
    let appId: string = '6918661953712445909';
    let options: AtomicServiceOptions = {
      displayId: 0,
    };
    try {
      this.context.openAtomicService(appId, options)
        .then(() => {
          console.info('openAtomicService succeed');
        })
        .catch((err: BusinessError) => {
          console.error(`openAtomicService failed, code is ${err.code}, message is ${err.message}`);
        });
    } catch (err) {
      let code = (err as BusinessError).code;
      let message = (err as BusinessError).message;
      console.error(`openAtomicService failed, code is ${code}, message is ${message}`);
    }
  }
}

你可能感兴趣的鸿蒙文章

harmony 鸿蒙Ability Kit(程序框架服务)

harmony 鸿蒙AbilityBase

harmony 鸿蒙AbilityBase_Element

harmony 鸿蒙AbilityRuntime

harmony 鸿蒙bundle

harmony 鸿蒙OH_NativeBundle_ApplicationInfo

harmony 鸿蒙OH_NativeBundle_ElementName

harmony 鸿蒙ability_base_common.h

harmony 鸿蒙ability_runtime_common.h

harmony 鸿蒙application_context.h

0  赞