@ohos.app.ability.UIExtensionContentSession (UI Operation Class for ExtensionAbilities with UI) (System API)

UIExtensionContentSession is an instance created when the UIExtensionAbility loads UI content. When the UIExtensionComponent starts a UIExtensionAbility, the UIExtensionAbility creates a UIExtensionContentSession instance and returns it through the onSessionCreate callback. One UIExtensionComponent corresponds to one UIExtensionContentSession instance, which provides methods such as UI loading and result notification. The UIExtensionContentSession instances of multiple UIExtensionAbilities are operated separately.

NOTE

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

The APIs of this module can be used only in the stage model.

This topic describes only system APIs provided by the module. For details about its public APIs, see @ohos.app.ability.UIExtensionContentSession (UI Operation Class for ExtensionAbilities with UI).

Modules to Import

import UIExtensionContentSession from '@ohos.app.ability.UIExtensionContentSession';

UIExtensionContentSession.sendData

sendData(data: Record<string, Object>): void

Sends data to the UIExtensionComponent.

System capability: SystemCapability.Ability.AbilityRuntime.Core

System API: This is a system API.

Parameters

Name Type Mandatory Description
data Record<string, Object> Yes Data to send.

Error codes

ID Error Message
16000050 Internal error.

For details about the error codes, see Ability Error Codes.

UIExtensionContentSession.setReceiveDataCallback

setReceiveDataCallback(callback: (data: Record<string, Object>) => void): void

Sets a callback to receive data from the UIExtensionComponent. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Ability.AbilityRuntime.Core

System API: This is a system API.

Parameters

Name Type Mandatory Description
callback (data: Record<string, Object>) => void Yes Callback used to return the received data.

Error codes

ID Error Message
16000050 Internal error.

For details about the error codes, see Ability Error Codes.

UIExtensionContentSession.setReceiveDataForResultCallback11+

setReceiveDataForResultCallback(callback: (data: Record<string, Object>) => Record<string, Object>): void

Sets a callback with a return value to receive data from the UIExtensionComponent. This API uses an asynchronous callback to return the result.

System API: This is a system API.

System capability: SystemCapability.Ability.AbilityRuntime.Core

Parameters

Name Type Mandatory Description
callback (data: { [key: string]: Object }) => { [key: string]: Object } Yes Callback used to return the received data with a return value.

Error codes

ID Error Message
16000050 Internal error.

For details about the error codes, see Ability Error Codes.

UIExtensionContentSession.startAbility

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

Starts an ability. This API uses an asynchronous callback to return the result.

NOTE

For details about the startup rules for the components in the stage model, see Component Startup Rules (Stage Model). The application where the UIExtensionComponent is located must be running in the foreground and gain focus.

System capability: SystemCapability.Ability.AbilityRuntime.Core

System API: This is a system API.

Parameters

Name Type Mandatory Description
want Want Yes Want information about the target ability.
callback AsyncCallback<void> Yes Callback used to return the result. If the ability is started, err is undefined; otherwise, err is an error object.

Error codes

ID Error Message
16000001 The specified ability does not exist.
16000002 Incorrect ability type.
16000004 Can not start 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 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.
16200001 The caller has been released.

For details about the error codes, see Ability Error Codes.

UIExtensionContentSession.startAbility

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

Starts an ability with options specified. This API uses an asynchronous callback to return the result.

NOTE

For details about the startup rules for the components in the stage model, see Component Startup Rules (Stage Model). The application where the UIExtensionComponent is located must be running in the foreground and gain focus.

System capability: SystemCapability.Ability.AbilityRuntime.Core

System API: This is a system API.

Parameters

Name Type Mandatory Description
want Want Yes Want information about the target ability.
options StartOptions Yes Parameters used for starting the ability.
callback AsyncCallback<void> Yes Callback used to return the result. If the ability is started, err is undefined; otherwise, err is an error object.

Error codes

ID Error Message
16000001 The specified ability does not exist.
16000004 Can not start 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.
16200001 The caller has been released.

For details about the error codes, see Ability Error Codes.

UIExtensionContentSession.startAbility

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

Starts an ability. This API uses a promise to return the result.

NOTE

For details about the startup rules for the components in the stage model, see Component Startup Rules (Stage Model). The application where the UIExtensionComponent is located must be running in the foreground and gain focus.

System capability: SystemCapability.Ability.AbilityRuntime.Core

System API: This is a system API.

Parameters

Name Type Mandatory Description
want Want Yes Want information about the target ability.
options StartOptions No Parameters used for starting the ability.

Return value

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

Error codes

ID Error Message
16000001 The specified ability does not exist.
16000002 Incorrect ability type.
16000004 Can not start 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 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.
16200001 The caller has been released.

For details about the error codes, see Ability Error Codes.

UIExtensionContentSession.startAbilityForResult

startAbilityForResult(want: Want, callback: AsyncCallback<AbilityResult>): void

Starts an ability and returns the result to the caller after the ability is terminated. This API uses an asynchronous callback to return the result.

An ability can be terminated in the following ways:

  • Normally, you can call terminateSelfWithResult to terminate the ability. The result is returned to the caller.
  • If an exception occurs, for example, the ability is killed, an error message, in which resultCode is -1, is returned to the caller.
  • If different applications call this API to start an ability that uses the singleton mode and then call terminateSelfWithResult to terminate the ability, the normal result is returned to the last caller, and an exception message, in which resultCode is -1, is returned to others.

NOTE

For details about the startup rules for the components in the stage model, see Component Startup Rules (Stage Model). The application where the UIExtensionComponent is located must be running in the foreground and gain focus.

System capability: SystemCapability.Ability.AbilityRuntime.Core

System API: This is a system API.

Parameters

Name Type Mandatory Description
want Want Yes Want information about the target ability.
callback AsyncCallback<AbilityResult> Yes Callback used to return the result. If the ability is started and terminated, err is undefined and data is the obtained result code and data; otherwise, err is an error object.

Error codes

ID Error Message
16000001 The specified ability does not exist.
16000002 Incorrect ability type.
16000004 Can not start 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 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.
16200001 The caller has been released.

For details about the error codes, see Ability Error Codes.

UIExtensionContentSession.startAbilityForResult

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

Starts an ability with options specified and returns the result to the caller after the ability is terminated. This API uses an asynchronous callback to return the result.

An ability can be terminated in the following ways:

  • Normally, you can call terminateSelfWithResult to terminate the ability. The result is returned to the caller.
  • If an exception occurs, for example, the ability is killed, an error message, in which resultCode is -1, is returned to the caller.
  • If different applications call this API to start an ability that uses the singleton mode and then call terminateSelfWithResult to terminate the ability, the normal result is returned to the last caller, and an exception message, in which resultCode is -1, is returned to others.

NOTE

For details about the startup rules for the components in the stage model, see Component Startup Rules (Stage Model). The application where the UIExtensionComponent is located must be running in the foreground and gain focus.

System capability: SystemCapability.Ability.AbilityRuntime.Core

System API: This is a system API.

Parameters

Name Type Mandatory Description
want Want Yes Want information about the target ability.
options StartOptions Yes Parameters used for starting the ability.
callback AsyncCallback<AbilityResult> Yes Callback used to return the result. If the ability is started and terminated, err is undefined and data is the obtained result code and data; otherwise, err is an error object.

Error codes

ID Error Message
16000001 The specified ability does not exist.
16000004 Can not start 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.
16200001 The caller has been released.

For details about the error codes, see Ability Error Codes.

UIExtensionContentSession.startAbilityForResult

startAbilityForResult(want: Want, options?: StartOptions): Promise<AbilityResult>

Starts an ability and returns the result to the caller after the ability is terminated. This API uses a promise to return the result.

An ability can be terminated in the following ways:

  • Normally, you can call terminateSelfWithResult to terminate the ability. The result is returned to the caller.
  • If an exception occurs, for example, the ability is killed, an error message, in which resultCode is -1, is returned to the caller.
  • If different applications call this API to start an ability that uses the singleton mode and then call terminateSelfWithResult to terminate the ability, the normal result is returned to the last caller, and an exception message, in which resultCode is -1, is returned to others.

NOTE

For details about the startup rules for the components in the stage model, see Component Startup Rules (Stage Model). The application where the UIExtensionComponent is located must be running in the foreground and gain focus.

System capability: SystemCapability.Ability.AbilityRuntime.Core

System API: This is a system API.

Parameters

Name Type Mandatory Description
want Want Yes Want information about the target ability.
options StartOptions No Parameters used for starting the ability.

Return value

Type Description
Promise<AbilityResult> Promise used to return the result code and data.

Error codes

ID Error Message
16000001 The specified ability does not exist.
16000002 Incorrect ability type.
16000004 Can not start 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 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.
16200001 The caller has been released.

For details about the error codes, see Ability Error Codes.

UIExtensionContentSession.setWindowBackgroundColor

setWindowBackgroundColor(color: string): void

Sets the background color for the loading page of the UIExtensionAbility. This API can be used only after loadContent() is called and takes effect.

System capability: SystemCapability.Ability.AbilityRuntime.Core

System API: This is a system API.

Parameters

Name Type Mandatory Description
color string Yes Background color to set. The value is a hexadecimal RGB or ARGB color code and is case insensitive, for example, #00FF00 or #FF00FF00.

Error codes

ID Error Message
16000050 Internal error.

For details about the error codes, see Ability Error Codes.

UIExtensionContentSession.startAbilityAsCaller11+

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

Starts an ability as the caller. The initial ability places its caller information (such as the bundle name and ability name) in the want parameter and transfers the information to an ExtensionAbility at the middle layer. When the ExtensionAbility starts another ability by calling this API, the started ability can obtain the caller information of the initial ability from the onCreate lifecycle. This API uses an asynchronous callback to return the result.

System API: This is a system API.

System capability: SystemCapability.Ability.AbilityRuntime.Core

Parameters

Name Type Mandatory Description
want Want Yes Want information about the target ability.
callback AsyncCallback<void> Yes Callback used to return the result. If the operation is successful, err is undefined; otherwise, err is an error object.

Error codes

ID Error Message
16000001 The specified ability does not exist.
16000002 Incorrect ability type.
16000004 Can not start 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 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.
16200001 The caller has been released.

For details about the error codes, see Ability Error Codes.

UIExtensionContentSession.startAbilityAsCaller11+

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

Starts an ability as the caller, with options specified. The initial ability places its caller information (such as the bundle name and ability name) in the want parameter and transfers the information to an ExtensionAbility at the middle layer. When the ExtensionAbility starts another ability by calling this API, the started ability can obtain the caller information of the initial ability from the onCreate lifecycle. This API uses an asynchronous callback to return the result.

System API: This is a system API.

System capability: SystemCapability.Ability.AbilityRuntime.Core

Parameters

Name Type Mandatory Description
want Want Yes Want information about the target ability.
options StartOptions Yes Parameters used for starting the ability.
callback AsyncCallback<void> Yes Callback used to return the result. If the operation is successful, err is undefined; otherwise, err is an error object.

Error codes

ID Error Message
16000001 The specified ability does not exist.
16000004 Can not start 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.
16200001 The caller has been released.

For details about the error codes, see Ability Error Codes.

UIExtensionContentSession.startAbilityAsCaller11+

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

Starts an ability as the caller. The initial ability places its caller information (such as the bundle name and ability name) in the want parameter and transfers the information to an ExtensionAbility at the middle layer. When the ExtensionAbility starts another ability by calling this API, the started ability can obtain the caller information of the initial ability from the onCreate lifecycle. This API uses a promise to return the result.

System API: This is a system API.

System capability: SystemCapability.Ability.AbilityRuntime.Core

Parameters

Name Type Mandatory Description
want Want Yes Want information about the target ability.
options StartOptions No Parameters used for starting the ability.

Return value

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

Error codes

ID Error Message
16000001 The specified ability does not exist.
16000002 Incorrect ability type.
16000004 Can not start 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 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.
16200001 The caller has been released.

For details about the error codes, see Ability Error Codes.

UIExtensionContentSession.getUIExtensionHostWindowProxy11+

getUIExtensionHostWindowProxy(): uiExtensionHost.UIExtensionHostWindowProxy

Obtains the window object corresponding to the current UIExtension to notify the width, height, position, and avoided area.

System API: This is a system API.

System capability: SystemCapability.Ability.AbilityRuntime.Core

Return value

Type Description
uiExtensionHost.UIExtensionHostWindowProxy Window object.

Error codes

ID Error Message
16000050 Internal error.

Example

import UIExtensionAbility from '@ohos.app.ability.UIExtensionAbility'
import UIExtensionContentSession from '@ohos.app.ability.UIExtensionContentSession'
import Want from '@ohos.app.ability.Want';
import uiExtensionHost from '@ohos.uiExtensionHost';

const TAG: string = '[UIExtAbility]'
export default class UIExtAbility extends UIExtensionAbility {

  onCreate() {
    console.log(TAG, `UIExtAbility onCreate`)
  }

  onForeground() {
    console.log(TAG, `UIExtAbility onForeground`)
  }

  onBackground() {
    console.log(TAG, `UIExtAbility onBackground`)
  }

  onDestroy() {
    console.log(TAG, `UIExtAbility onDestroy`)
  }

  onSessionCreate(want: Want, session: UIExtensionContentSession) {
    let extensionHostWindow = session.getUIExtensionHostWindowProxy();
    let data: Record<string, UIExtensionContentSession | uiExtensionHost.UIExtensionHostWindowProxy> = {
        'session': session,
        'extensionHostWindow': extensionHostWindow
    }
    let storage: LocalStorage = new LocalStorage(data);
    session.loadContent('pages/extension', storage);
  }

  onSessionDestroy(session: UIExtensionContentSession) {
    console.log(TAG, `UIExtAbility onSessionDestroy`)
  }
}

For details about the error codes, see Ability Error Codes.