harmony 鸿蒙UIExtensionContext
UIExtensionContext
UIExtensionContext, inherited from ExtensionContext, provides the context environment for the UIExtensionAbility. It provides UIExtensionAbility-related configurations and APIs for operating the UIExtensionAbility. For example, you can use the APIs to start a UIExtensionAbility.
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.
- The APIs of this module must be used in the main thread, but not in sub-threads such as Worker and TaskPool.
Modules to Import
import { common } from '@kit.AbilityKit';
UIExtensionContext.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).
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 ability is started, err is undefined; otherwise, err is an error object. |
Error codes
For details about the error codes, see Universal Error Codes and Ability Error Codes.
ID | Error Message |
---|---|
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. |
16000001 | The specified ability does not exist. |
16000002 | Incorrect ability type. |
16000004 | Failed to start the invisible ability. |
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. |
16000018 | Redirection to a third-party application is not allowed in API version 11 or later. |
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. |
16000069 | The extension cannot start the third party application. |
16000070 | The extension cannot start the service. |
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. |
Example
import { UIExtensionAbility, Want } from '@kit.AbilityKit';
import { BusinessError } from '@kit.BasicServicesKit';
export default class EntryAbility extends UIExtensionAbility {
onForeground() {
let want: Want = {
bundleName: 'com.example.myapplication',
abilityName: 'EntryAbility'
};
try {
this.context.startAbility(want, (err: BusinessError) => {
if (err.code) {
// Process service logic errors.
console.error(`startAbility failed, code is ${err.code}, message is ${err.message}`);
return;
}
// Carry out normal service processing.
console.info('startAbility succeed');
});
} catch (err) {
// Process input parameter errors.
let code = (err as BusinessError).code;
let message = (err as BusinessError).message;
console.error(`startAbility failed, code is ${code}, message is ${message}`);
}
}
}
UIExtensionContext.startAbility
startAbility(want: Want, options: StartOptions, callback: AsyncCallback<void>): void
Starts an ability with the start 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).
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 ability is started, err is undefined; otherwise, err is an error object. |
Error codes
For details about the error codes, see Universal Error Codes and Ability Error Codes.
ID | Error Message |
---|---|
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. |
16000001 | The specified ability does not exist. |
16000004 | Failed to start the invisible ability. |
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. |
16000018 | Redirection to a third-party application is not allowed in API version 11 or later. |
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. |
16000069 | The extension cannot start the third party application. |
16000070 | The extension cannot start the service. |
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. |
Example
import { UIExtensionAbility, Want, StartOptions } from '@kit.AbilityKit';
import { BusinessError } from '@kit.BasicServicesKit';
export default class EntryAbility extends UIExtensionAbility {
onForeground() {
let want: Want = {
deviceId: '',
bundleName: 'com.example.myapplication',
abilityName: 'EntryAbility'
};
let options: StartOptions = {
displayId: 0
};
try {
this.context.startAbility(want, options, (err: BusinessError) => {
if (err.code) {
// Process service logic errors.
console.error(`startAbility failed, code is ${err.code}, message is ${err.message}`);
return;
}
// Carry out normal service processing.
console.info('startAbility succeed');
});
} catch (err) {
// Process input parameter errors.
let code = (err as BusinessError).code;
let message = (err as BusinessError).message;
console.error(`startAbility failed, code is ${code}, message is ${message}`);
}
}
}
UIExtensionContext.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).
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
For details about the error codes, see Universal Error Codes and Ability Error Codes.
ID | Error Message |
---|---|
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. |
16000001 | The specified ability does not exist. |
16000002 | Incorrect ability type. |
16000004 | Failed to start the invisible ability. |
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. |
16000018 | Redirection to a third-party application is not allowed in API version 11 or later. |
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. |
16000069 | The extension cannot start the third party application. |
16000070 | The extension cannot start the service. |
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. |
Example
import { UIExtensionAbility, Want, StartOptions } from '@kit.AbilityKit';
import { BusinessError } from '@kit.BasicServicesKit';
export default class EntryAbility extends UIExtensionAbility {
onForeground() {
let want: Want = {
bundleName: 'com.example.myapplication',
abilityName: 'EntryAbility'
};
let options: StartOptions = {
displayId: 0,
};
try {
this.context.startAbility(want, options)
.then(() => {
// Carry out normal service processing.
console.info('startAbility succeed');
})
.catch((err: BusinessError) => {
// Process service logic errors.
console.error(`startAbility failed, code is ${err.code}, message is ${err.message}`);
});
} catch (err) {
// Process input parameter errors.
let code = (err as BusinessError).code;
let message = (err as BusinessError).message;
console.error(`startAbility failed, code is ${code}, message is ${message}`);
}
}
}
UIExtensionContext.startAbilityForResult
startAbilityForResult(want: Want, callback: AsyncCallback<AbilityResult>): void
Starts an ability and obtains the result when the ability is terminated. This API uses an asynchronous callback to return the result. The following situations may be possible for a started ability: - 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).
System capability: SystemCapability.Ability.AbilityRuntime.Core
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
want | Want | Yes | Want information about the target ability. |
callback | AsyncCallback<AbilityResult> | Yes | Callback used to return the result. |
Error codes
For details about the error codes, see Universal Error Codes and Ability Error Codes.
ID | Error Message |
---|---|
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. |
16000001 | The specified ability does not exist. |
16000002 | Incorrect ability type. |
16000004 | Failed to start the invisible ability. |
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. |
16000018 | Redirection to a third-party application is not allowed in API version 11 or later. |
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. |
16000069 | The extension cannot start the third party application. |
16000070 | The extension cannot start the service. |
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. |
Example
import { UIExtensionAbility, Want, common } from '@kit.AbilityKit';
import { BusinessError } from '@kit.BasicServicesKit';
export default class EntryAbility extends UIExtensionAbility {
onForeground() {
let want: Want = {
deviceId: '',
bundleName: 'com.example.myapplication',
};
try {
this.context.startAbilityForResult(want, (err: BusinessError, result: common.AbilityResult) => {
if (err.code) {
// Process service logic errors.
console.error(`startAbilityForResult failed, code is ${err.code}, message is ${err.message}`);
return;
}
// Carry out normal service processing.
console.info('startAbilityForResult succeed');
});
} catch (err) {
// Process input parameter errors.
let code = (err as BusinessError).code;
let message = (err as BusinessError).message;
console.error(`startAbilityForResult failed, code is ${code}, message is ${message}`);
}
}
}
UIExtensionContext.startAbilityForResult
startAbilityForResult(want: Want, options: StartOptions, callback: AsyncCallback<AbilityResult>): void
Starts an ability with the start options specified and obtains the result when the ability is terminated. This API uses an asynchronous callback to return the result. The following situations may be possible for a started ability: - 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).
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<AbilityResult> | Yes | Callback used to return the result. |
Error codes
For details about the error codes, see Universal Error Codes and Ability Error Codes.
ID | Error Message |
---|---|
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. |
16000001 | The specified ability does not exist. |
16000004 | Failed to start the invisible ability. |
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. |
16000018 | Redirection to a third-party application is not allowed in API version 11 or later. |
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. |
16000069 | The extension cannot start the third party application. |
16000070 | The extension cannot start the service. |
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. |
Example
import { UIExtensionAbility, Want, common, StartOptions } from '@kit.AbilityKit';
import { BusinessError } from '@kit.BasicServicesKit';
export default class EntryAbility extends UIExtensionAbility {
onForeground() {
let want: Want = {
deviceId: '',
bundleName: 'com.example.myapplication',
abilityName: 'EntryAbility'
};
let options: StartOptions = {
displayId: 0,
};
try {
this.context.startAbilityForResult(want, options, (err: BusinessError, result: common.AbilityResult) => {
if (err.code) {
// Process service logic errors.
console.error(`startAbilityForResult failed, code is ${err.code}, message is ${err.message}`);
return;
}
// Carry out normal service processing.
console.info('startAbilityForResult succeed');
});
} catch (err) {
// Process input parameter errors.
let code = (err as BusinessError).code;
let message = (err as BusinessError).message;
console.error(`startAbilityForResult failed, code is ${code}, message is ${message}`);
}
}
}
UIExtensionContext.startAbilityForResult
startAbilityForResult(want: Want, options?: StartOptions): Promise<AbilityResult>
Starts an ability and obtains the result when the ability is terminated. This API uses a promise to return the result. The following situations may be possible for a started ability: - 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).
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<AbilityResult> | Promise used to return the result. |
Error codes
For details about the error codes, see Universal Error Codes and Ability Error Codes.
ID | Error Message |
---|---|
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. |
16000001 | The specified ability does not exist. |
16000002 | Incorrect ability type. |
16000004 | Failed to start the invisible ability. |
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. |
16000018 | Redirection to a third-party application is not allowed in API version 11 or later. |
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. |
16000069 | The extension cannot start the third party application. |
16000070 | The extension cannot start the service. |
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. |
Example
import { UIExtensionAbility, Want, common, StartOptions } from '@kit.AbilityKit';
import { BusinessError } from '@kit.BasicServicesKit';
export default class EntryAbility extends UIExtensionAbility {
onForeground() {
let want: Want = {
bundleName: 'com.example.myapplication',
abilityName: 'EntryAbility'
};
let options: StartOptions = {
displayId: 0,
};
try {
this.context.startAbilityForResult(want, options)
.then((result: common.AbilityResult) => {
// Carry out normal service processing.
console.info('startAbilityForResult succeed');
})
.catch((err: BusinessError) => {
// Process service logic errors.
console.error(`startAbilityForResult failed, code is ${err.code}, message is ${err.message}`);
});
} catch (err) {
// Process input parameter errors.
let code = (err as BusinessError).code;
let message = (err as BusinessError).message;
console.error(`startAbilityForResult failed, code is ${code}, message is ${message}`);
}
}
}
UIExtensionContext.connectServiceExtensionAbility
connectServiceExtensionAbility(want: Want, options: ConnectOptions): number
Connects this ability to a ServiceExtensionAbility.
NOTE
For details about the startup rules for the components in the stage model, see Component Startup Rules (Stage Model).
System capability: SystemCapability.Ability.AbilityRuntime.Core
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
want | Want | Yes | Want information for connecting to the ServiceExtensionAbility. |
options | ConnectOptions | Yes | Instance of the callback function after the connection to the ServiceExtensionAbility is set up. |
Return value
Type | Description |
---|---|
number | Result code of the connection. |
Error codes
For details about the error codes, see Universal Error Codes and Ability Error Codes.
ID | Error Message |
---|---|
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. |
16000001 | The specified ability does not exist. |
16000002 | Incorrect ability type. |
16000004 | Failed to start the invisible ability. |
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. |
16000050 | Internal error. |
16000053 | The ability is not on the top of the UI. |
16000055 | Installation-free timed out. |
16000070 | The extension cannot start the service. |
Example
import { UIExtensionAbility, Want, common } from '@kit.AbilityKit';
import { rpc } from '@kit.IPCKit';
import { BusinessError } from '@kit.BasicServicesKit';
export default class EntryAbility extends UIExtensionAbility {
onForeground() {
let want: Want = {
deviceId: '',
bundleName: 'com.example.myapplication',
abilityName: 'ServiceExtensionAbility'
};
let commRemote: rpc.IRemoteObject;
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.connectServiceExtensionAbility(want, options);
} catch (err) {
// Process input parameter errors.
let code = (err as BusinessError).code;
let message = (err as BusinessError).message;
console.error(`connectServiceExtensionAbility failed, code is ${code}, message is ${message}`);
}
}
}
UIExtensionContext.disconnectServiceExtensionAbility
disconnectServiceExtensionAbility(connection: number): Promise<void>
Disconnects this ability from a ServiceExtensionAbility and after the successful disconnection, sets the remote object returned upon the connection to void. This API uses a promise to return the result.
System capability: SystemCapability.Ability.AbilityRuntime.Core
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
connection | number | Yes | Digital code of the connected ServiceExtensionAbility, that is, connectionId returned by connectServiceExtensionAbility. |
Return value
Type | Description |
---|---|
Promise<void> | Promise that returns no value. |
Error codes
For details about the error codes, see Universal Error Codes and Ability Error Codes.
ID | Error Message |
---|---|
401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. |
16000011 | The context does not exist. |
16000050 | Internal error. |
Example
import { UIExtensionAbility } from '@kit.AbilityKit';
import { rpc } from '@kit.IPCKit';
import { BusinessError } from '@kit.BasicServicesKit';
export default class EntryAbility extends UIExtensionAbility {
onForeground() {
// connection is the return value of connectServiceExtensionAbility.
let connection = 1;
let commRemote: rpc.IRemoteObject|null;
try {
this.context.disconnectServiceExtensionAbility(connection).then(() => {
commRemote = null;
// Carry out normal service processing.
console.info('disconnectServiceExtensionAbility succeed');
}).catch((err: BusinessError) => {
// Process service logic errors.
console.error(`disconnectServiceExtensionAbility failed, code is ${err.code}, message is ${err.message}`);
})
} catch (err) {
commRemote = null;
// Process input parameter errors.
let code = (err as BusinessError).code;
let message = (err as BusinessError).message;
console.error(`disconnectServiceExtensionAbility failed, code is ${code}, message is ${message}`);
}
}
}
UIExtensionContext.disconnectServiceExtensionAbility
disconnectServiceExtensionAbility(connection: number, callback: AsyncCallback<void>): void
Disconnects this ability from a ServiceExtensionAbility and after the successful disconnection, sets the remote object returned upon the connection to void. This API uses an asynchronous callback to return the result.
System capability: SystemCapability.Ability.AbilityRuntime.Core
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
connection | number | Yes | Digital code of the connected ServiceExtensionAbility, that is, connectionId returned by connectServiceExtensionAbility. |
callback | AsyncCallback<void> | Yes | Callback used to return the result. If the disconnection is successful, err is undefined. Otherwise, err is an error object. |
Error codes
For details about the error codes, see Universal Error Codes and Ability Error Codes.
ID | Error Message |
---|---|
401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. |
16000011 | The context does not exist. |
16000050 | Internal error. |
Example
import { UIExtensionAbility } from '@kit.AbilityKit';
import { rpc } from '@kit.IPCKit';
import { BusinessError } from '@kit.BasicServicesKit';
export default class EntryAbility extends UIExtensionAbility {
onForeground() {
// connection is the return value of connectServiceExtensionAbility.
let connection = 1;
let commRemote: rpc.IRemoteObject|null;
try {
this.context.disconnectServiceExtensionAbility(connection, (err: BusinessError) => {
commRemote = null;
if (err.code) {
// Process service logic errors.
console.error(`disconnectServiceExtensionAbility failed, code is ${err.code}, message is ${err.message}`);
return;
}
// Carry out normal service processing.
console.info('disconnectServiceExtensionAbility succeed');
});
} catch (err) {
commRemote = null;
// Process input parameter errors.
let code = (err as BusinessError).code;
let message = (err as BusinessError).message;
console.error(`disconnectServiceExtensionAbility failed, code is ${code}, message is ${message}`);
}
}
}
UIExtensionContext.terminateSelf12+
terminateSelf(callback: AsyncCallback<void>): void
Stops the window object corresponding to this UIExtensionContext. This API uses an asynchronous callback to return the result.
System capability: SystemCapability.Ability.AbilityRuntime.Core
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
callback | AsyncCallback<void> | Yes | Callback used to return the result. If the window object is stopped, err is undefined; otherwise, err is an error object. |
Error codes
For details about the error codes, see Universal Error Codes.
ID | Error Message |
---|---|
401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. |
Example
import { UIExtensionAbility } from '@kit.AbilityKit';
import { BusinessError } from '@kit.BasicServicesKit';
export default class EntryAbility extends UIExtensionAbility {
onForeground() {
try {
this.context.terminateSelf((err: BusinessError) => {
if (err.code) {
// Process service logic errors.
console.error(`terminateSelf failed, code is ${err.code}, message is ${err.message}`);
return;
}
// Carry out normal service processing.
console.info('terminateSelf succeed');
});
} catch (err) {
// Capture the synchronization parameter error.
let code = (err as BusinessError).code;
let message = (err as BusinessError).message;
console.error(`terminateSelf failed, code is ${code}, message is ${message}`);
}
}
}
UIExtensionContext.terminateSelf12+
terminateSelf(): Promise<void>
Stops the window object corresponding to this UIExtensionContext. This API uses a promise to return the result.
System capability: SystemCapability.Ability.AbilityRuntime.Core
Return value
Type | Description |
---|---|
Promise<void> | Promise that returns no value. |
Example
import { UIExtensionAbility } from '@kit.AbilityKit';
import { BusinessError } from '@kit.BasicServicesKit';
export default class EntryAbility extends UIExtensionAbility {
onForeground() {
try {
this.context.terminateSelf()
.then(() => {
// Carry out normal service processing.
console.info('terminateSelf succeed');
})
.catch((err: BusinessError) => {
// Process service logic errors.
console.error(`terminateSelf failed, code is ${err.code}, message is ${err.message}`);
});
} catch (err) {
// Capture the synchronization parameter error.
let code = (err as BusinessError).code;
let message = (err as BusinessError).message;
console.error(`terminateSelf failed, code is ${code}, message is ${message}`);
}
}
}
UIExtensionContext.terminateSelfWithResult12+
terminateSelfWithResult(parameter: AbilityResult, callback: AsyncCallback<void>): void
Stops the window object corresponding to this UIExtensionContext and returns the result to the UIExtensionComponent. This API uses an asynchronous callback to return the result.
System capability: SystemCapability.Ability.AbilityRuntime.Core
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
parameter | AbilityResult | Yes | Result returned to the UIExtensionComponent. |
callback | AsyncCallback<void> | Yes | Callback used to return the result. If the window object is stopped, err is undefined; otherwise, err is an error object. |
Error codes
For details about the error codes, see Universal Error Codes.
ID | Error Message |
---|---|
401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. |
Example
import { UIExtensionAbility, Want, common } from '@kit.AbilityKit';
import { BusinessError } from '@kit.BasicServicesKit';
export default class EntryAbility extends UIExtensionAbility {
onForeground() {
let want: Want = {
bundleName: 'com.example.myapplication',
abilityName: 'EntryAbility'
};
let resultCode = 100;
// AbilityResult information returned to the caller.
let abilityResult: common.AbilityResult = {
want,
resultCode
};
try {
this.context.terminateSelfWithResult(abilityResult, (err: BusinessError) => {
if (err.code) {
// Process service logic errors.
console.error(`terminateSelfWithResult failed, code is ${err.code}, message is ${err.message}`);
return;
}
// Carry out normal service processing.
console.info('terminateSelfWithResult succeed');
});
} catch (err) {
// Process input parameter errors.
let code = (err as BusinessError).code;
let message = (err as BusinessError).message;
console.error(`terminateSelfWithResult failed, code is ${code}, message is ${message}`);
}
}
}
UIExtensionContext.terminateSelfWithResult12+
terminateSelfWithResult(parameter: AbilityResult): Promise<void>
Stops the window object corresponding to this UIExtensionContext and returns the result to the UIExtensionComponent. This API uses a promise to return the result.
System capability: SystemCapability.Ability.AbilityRuntime.Core
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
parameter | AbilityResult | Yes | Result returned to the UIExtensionComponent. |
Return value
Type | Description |
---|---|
Promise<void> | Promise that returns no value. |
Error codes
For details about the error codes, see Universal Error Codes.
ID | Error Message |
---|---|
401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. |
import { UIExtensionAbility, Want, common } from '@kit.AbilityKit';
import { BusinessError } from '@kit.BasicServicesKit';
export default class EntryAbility extends UIExtensionAbility {
onForeground() {
let want: Want = {
bundleName: 'com.example.myapplication',
abilityName: 'EntryAbility'
};
let resultCode = 100;
// AbilityResult information returned to the caller.
let abilityResult: common.AbilityResult = {
want,
resultCode
};
try {
this.context.terminateSelfWithResult(abilityResult)
.then(() => {
// Carry out normal service processing.
console.info('terminateSelfWithResult succeed');
})
.catch((err: BusinessError) => {
// Process service logic errors.
console.error(`terminateSelfWithResult failed, code is ${err.code}, message is ${err.message}`);
});
} catch (err) {
// Process input parameter errors.
let code = (err as BusinessError).code;
let message = (err as BusinessError).message;
console.error(`terminateSelfWithResult failed, code is ${code}, message is ${message}`);
}
}
}
UIExtensionContext.reportDrawnCompleted12+
reportDrawnCompleted(callback: AsyncCallback<void>): void
Reports an event indicating that page loading is complete (onSessionCreate() is successfully called). This API uses an asynchronous callback to return the result.
System capability: SystemCapability.Ability.AbilityRuntime.Core
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
callback | AsyncCallback<void> | Yes | Callback used to return the result. If the event is reported, err is undefined; otherwise, err is an error object. |
Error codes
For details about the error codes, see Ability Error Codes.
ID | Error Message |
---|---|
16000011 | The context does not exist. |
16000050 | Internal error. |
Example
import { UIExtensionAbility, Want, UIExtensionContentSession } from '@kit.AbilityKit';
import { BusinessError } from '@kit.BasicServicesKit';
const TAG: string = '[testTag] UIExtAbility';
export default class UIExtAbility extends UIExtensionAbility {
onSessionCreate(want: Want, session: UIExtensionContentSession) {
console.info(TAG, `onSessionCreate, want: ${JSON.stringify(want)}`);
let data: Record<string, UIExtensionContentSession> = {
'session': session
};
let storage: LocalStorage = new LocalStorage(data);
session.loadContent('pages/extension', storage);
try {
this.context.reportDrawnCompleted((err) => {
if (err.code) {
// Process service logic errors.
console.error(`reportDrawnCompleted failed, code is ${err.code}, message is ${err.message}`);
return;
}
// Carry out normal service processing.
console.info('reportDrawnCompleted succeed');
});
} catch (err) {
// Capture the synchronization parameter error.
let code = (err as BusinessError).code;
let message = (err as BusinessError).message;
console.error(`reportDrawnCompleted failed, code is ${code}, message is ${message}`);
}
}
}
UIExtensionContext.openAtomicService12+
openAtomicService(appId: string, options?: AtomicServiceOptions): Promise<AbilityResult>
Starts an EmbeddableUIAbility in jump-out mode and returns the result. This API uses a promise to return the result. The following situations may be possible for a started EmbeddableUIAbility: - Normally, you can call terminateSelfWithResult to terminate the EmbeddableUIAbility. The result is returned to the caller. - If an exception occurs, for example, the EmbeddableUIAbility is killed, an error message, in which resultCode is -1, is returned to the caller. - If different applications call this API to start an EmbeddableUIAbility and then call terminateSelfWithResult to terminate the EmbeddableUIAbility, 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).
System capability: SystemCapability.Ability.AbilityRuntime.Core
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
appId | string | Yes | Unique ID of the application, which is allocated by the cloud. |
options | AtomicServiceOptions | No | Parameter carried in the request for starting the atomic service in jump-out mode. |
Return value
Type | Description |
---|---|
Promise<AbilityResult> | Promise used to return the result, which is an AbilityResult object. |
Error codes
For details about the error codes, see Universal Error Codes and Ability Error Codes.
ID | Error Message |
---|---|
401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. |
16000002 | Incorrect ability type. |
16000003 | The specified ID does not exist. |
16000004 | Failed to start the invisible ability. |
16000011 | The context does not exist. |
16000012 | The application is controlled. |
16000050 | Internal error. |
16000069 | The extension cannot start the third party application. |
16200001 | The caller has been released. |
Example
import { UIExtensionAbility, common, AtomicServiceOptions } from '@kit.AbilityKit';
import { BusinessError } from '@kit.BasicServicesKit';
export default class EntryAbility extends UIExtensionAbility {
onForeground() {
let appId: string = '6918661953712445909';
let options: AtomicServiceOptions = {
displayId: 0,
};
try {
this.context.openAtomicService(appId, options)
.then((result: common.AbilityResult) => {
// Carry out normal service processing.
console.info('openAtomicService succeed');
})
.catch((err: BusinessError) => {
// Process service logic errors.
console.error(`openAtomicService failed, code is ${err.code}, message is ${err.message}`);
});
} catch (err) {
// Process input parameter errors.
let code = (err as BusinessError).code;
let message = (err as BusinessError).message;
console.error(`openAtomicService failed, code is ${code}, message is ${message}`);
}
}
}
UIExtensionContext.openLink12+
openLink(link:string, options?: OpenLinkOptions, callback?: AsyncCallback<AbilityResult>): Promise<void>
Starts a UIAbility through App Linking. This API uses a promise to return the result.
A URL in the standard format is passed in to the link field to start the target UIAbility based on the implicit Want matching rules. The target UIAbility must have the following filter characteristics to process links of App Linking: - The actions field contains ohos.want.action.viewData. - The entities field contains entity.system.browsable. - The uris field contains elements whose scheme is https and domainVerify is true.
If you want to obtain the result after the started UIAbility is terminated, set the callback parameter. For details about how to use this parameter, see startAbilityForResult. If an input parameter is invalid, for example, a mandatory parameter is not set or the URL set in link is not in the standard format, an exception is thrown. If the parameter verification is successful but an error occurs when starting the target UIAbility, the error information is returned through promise.
NOTE
For details about the startup rules for the components in the stage model, see Component Startup Rules (Stage Model).
System capability: SystemCapability.Ability.AbilityRuntime.Core
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
link | string | Yes | URL to open, which must be in the standard format. |
options | OpenLinkOptions | No | Options of the URL. |
callback | AsyncCallback<AbilityResult> | No | Callback used to return the result. |
Return value
Type | Description |
---|---|
Promise<void> | Promise that returns no value. |
Error codes
For details about the error codes, see Universal Error Codes and Ability Error Codes.
ID | Error Message |
---|---|
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. |
16000001 | The specified ability does not exist. |
16000002 | Incorrect ability type. |
16000004 | Failed to start the invisible ability. |
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. |
16000019 | No matching ability is found. |
16000069 | The extension cannot start the third party application. |
16200001 | The caller has been released. |
16000053 | The ability is not on the top of the UI. |
Example
import { UIExtensionAbility, Want, UIExtensionContentSession, OpenLinkOptions } from '@kit.AbilityKit';
import { BusinessError } from '@kit.BasicServicesKit';
function log(info: string) {
console.error(`MyUIExtension:: ${JSON.stringify(info)}`);
}
export default class UIExtAbility extends UIExtensionAbility {
onCreate() {
log(`UIExtAbility onCreate`);
}
onForeground() {
log(`UIExtAbility onForeground`);
}
onBackground() {
log(`UIExtAbility onBackground`);
}
onDestroy() {
log(`UIExtAbility onDestroy`);
}
onSessionCreate(want: Want, session: UIExtensionContentSession) {
log(`UIExtAbility onSessionCreate`);
log(`UIExtAbility onSessionCreate, want: ${JSON.stringify(want)}`);
let record: Record<string, UIExtensionContentSession> = {
'session': session
};
let storage: LocalStorage = new LocalStorage(record);
session.loadContent('pages/UIExtensionIndex', storage);
let link: string = 'https://www.example.com';
let openLinkOptions: OpenLinkOptions = {
appLinkingOnly: true
};
try {
this.context.openLink(
link,
openLinkOptions,
(err, result) => {
log(`openLink callback error.code: ${JSON.stringify(err)}`);
log(`openLink callback result: ${JSON.stringify(result.resultCode)}`);
log(`openLink callback result data: ${JSON.stringify(result.want)}`);
}
).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)}`);
}
}
onSessionDestroy(session: UIExtensionContentSession) {
log(`UIExtAbility onSessionDestroy`);
}
}
UIExtensionContext.startUIServiceExtensionAbility14+
startUIServiceExtensionAbility(want: Want): Promise<void>
Starts a UIServiceExtensionAbility.
NOTE
For details about the startup rules for the components in the stage model, see Component Startup Rules (Stage Model).
System capability: SystemCapability.Ability.AbilityRuntime.Core
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
want | Want | Yes | Want information for starting the UIServiceExtensionAbility. |
Return value
Type | Description |
---|---|
Promise<void> | Promise that returns no value. |
Error codes
For details about the error codes, see Universal Error Codes and Ability Error Codes.
ID | Error Message |
---|---|
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. |
801 | Capability not supported. |
16000001 | The specified ability does not exist. |
16000002 | Incorrect ability type. |
16000004 | Failed to start the invisible ability. |
16000005 | The specified process does not have the permission. |
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. |
Example
import { common, Want } from '@kit.AbilityKit';
import { BusinessError } from '@kit.BasicServicesKit';
@Entry
@Component
struct Index {
build() {
Column() {
Row() {
// Create a Start button.
Button('start ability')
.enabled(true)
.onClick(() => {
let context = this.getUIContext().getHostContext() as common.UIExtensionContext;
let startWant: Want = {
bundleName: 'com.acts.uiserviceextensionability',
abilityName: 'UiServiceExtAbility',
};
try {
// Start the UIServiceExtensionAbility.
context.startUIServiceExtensionAbility(startWant).then(() => {
console.log('startUIServiceExtensionAbility success');
}).catch((error: BusinessError) => {
console.log('startUIServiceExtensionAbility error', JSON.stringify(error));
})
} catch (err) {
console.log('startUIServiceExtensionAbility failed', JSON.stringify(err));
}
})
}
}
}
}
UIExtensionContext.connectUIServiceExtensionAbility14+
connectUIServiceExtensionAbility(want: Want, callback: UIServiceExtensionConnectCallback) : Promise<UIServiceProxy>
Connects to a UIServiceExtensionAbility.
NOTE
For details about the startup rules for the components in the stage model, see Component Startup Rules (Stage Model).
System capability: SystemCapability.Ability.AbilityRuntime.Core
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
want | Want | Yes | Want information used for connection. |
callback | UIServiceExtensionConnectCallback | Yes | Callback for connecting to the UIServiceExtensionAbility. |
Return value
Type | Description |
---|---|
Promise<UIServiceProxy> | When the UIServiceExtensionAbility is connected, a UIServiceProxy object is returned, which can be used to send data to the UIServiceExtensionAbility. |
Error codes
For details about the error codes, see Universal Error Codes and Ability Error Codes.
ID | Error Message |
---|---|
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. |
801 | Capability not supported. |
16000001 | The specified ability does not exist. |
16000002 | Incorrect ability type. |
16000004 | Failed to start the invisible ability. |
16000005 | The specified process does not have the permission. |
16000008 | The crowdtesting application expires. |
16000011 | The context does not exist. |
16000013 | The application is controlled by EDM. |
16000050 | Internal error. |
16000055 | Installation-free timed out. |
Example
import { common, Want } from '@kit.AbilityKit';
import { BusinessError } from '@kit.BasicServicesKit';
@Entry
@Component
struct Page_UIServiceExtensionAbility {
@State uiServiceProxy: common.UIServiceProxy|null = null;
build() {
Column() {
//...
Row() {
//...
}.onClick(() => {
const context = this.getUIContext().getHostContext() as common.UIExtensionContext;
const want: Want = {
deviceId: '',
bundleName: 'com.example.myapplication',
abilityName: ''
};
// Define a callback.
const callback: common.UIServiceExtensionConnectCallback = {
onData: (data: Record<string, Object>): void => {
console.log('onData:', JSON.stringify(data));
},
onDisconnect: (): void => {
console.log('onDisconnect');
}
};
// Connect to the UIServiceExtensionAbility.
context.connectUIServiceExtensionAbility(want, callback).then((uiServiceProxy: common.UIServiceProxy) => {
this.uiServiceProxy = uiServiceProxy;
console.log('connectUIServiceExtensionAbility success');
}).catch((error: BusinessError) => {
console.log('connectUIServiceExtensionAbility failed', JSON.stringify(error));
})
})
}
}
}
UIExtensionContext.disconnectUIServiceExtensionAbility14+
disconnectUIServiceExtensionAbility(proxy: UIServiceProxy): Promise<void>
Disconnects a UIServiceExtensionAbility.
System capability: SystemCapability.Ability.AbilityRuntime.Core
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
proxy | UIServiceProxy | Yes | Proxy used returned by calling connectUIServiceExtensionAbility. |
Return value
Type | Description |
---|---|
Promise<void> | Promise that returns no value. |
Error codes
For details about the error codes, see Universal Error Codes and Ability Error Codes.
ID | Error Message |
---|---|
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. |
Example
import { common } from '@kit.AbilityKit';
import { BusinessError } from '@kit.BasicServicesKit';
@Entry
@Component
struct Page_UIServiceExtensionAbility {
@State uiServiceProxy: common.UIServiceProxy|null = null;
build() {
Column() {
//...
Row() {
//...
}.onClick(() => {
const context = this.getUIContext().getHostContext() as common.UIExtensionContext;
// this.uiServiceProxy is the proxy object saved during connection.
context.disconnectUIServiceExtensionAbility(this.uiServiceProxy).then(() => {
console.log('disconnectUIServiceExtensionAbility success');
}).catch((error: BusinessError) => {
console.log('disconnectUIServiceExtensionAbility failed', JSON.stringify(error));
})
})
}
}
}
UIExtensionContext.setColorMode18+
setColorMode(colorMode: ConfigurationConstant.ColorMode): void
Sets the color mode for this UIExtensionAbility. Before calling this API, ensure that the page corresponding to the UIExtensionContext has been loaded. This API can be called only by the main thread.
NOTE - After this API is called, a new resource manager object is created. If a resource manager was previously cached, it should be updated accordingly. - The priority of the color mode is as follows: UIExtensionAbility color mode > Application color mode (set via ApplicationContext.setColorMode) > System color mode.
System capability: SystemCapability.Ability.AbilityRuntime.Core
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
colorMode | ConfigurationConstant.ColorMode | Yes | Color mode. The options are as follows: - COLOR_MODE_DARK: dark mode. - COLOR_MODE_LIGHT: light mode. - COLOR_MODE_NOT_SET: not set (following the system or application). |
Error codes
For details about the error codes, see Universal Error Codes and Ability Error Codes.
ID | Error Message |
---|---|
401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. |
16000011 | The context does not exist. |
Example
import { UIExtensionAbility, ConfigurationConstant } from '@kit.AbilityKit';
export default class MyAbility extends UIExtensionAbility {
onForeground() {
let uiExtensionContext = this.context;
uiExtensionContext.setColorMode(ConfigurationConstant.ColorMode.COLOR_MODE_DARK);
}
}
你可能感兴趣的鸿蒙文章
harmony 鸿蒙AbilityAccessControl
harmony 鸿蒙OH_NativeBundle_ApplicationInfo
harmony 鸿蒙OH_NativeBundle_ElementName
- 所属分类: 后端技术
- 本文标签:
热门推荐
-
2、 - 优质文章
-
3、 gate.io
-
8、 golang
-
9、 openharmony
-
10、 Vue中input框自动聚焦