harmony 鸿蒙ApplicationContext
ApplicationContext
The ApplicationContext module, inherited from Context, provides application-level context capabilities, including APIs for registering and unregistering the lifecycle of application components.
NOTE
The initial APIs of this module are supported since API version 9. Newly added APIs will be marked with a superscript to indicate their earliest API version. The APIs of this module can be used only in the stage model.
Modules to Import
import { common } from '@kit.AbilityKit';
Usage
Before calling any APIs in ApplicationContext, obtain an ApplicationContext instance through the Context instance.
ApplicationContext.on(‘abilityLifecycle’)
on(type: ‘abilityLifecycle’, callback: AbilityLifecycleCallback): number
Registers a listener to monitor the ability lifecycle of the application. This API uses an asynchronous callback to return the result. It can be called only by the main thread.
Atomic service API: This API can be used in atomic services since API version 11.
System capability: SystemCapability.Ability.AbilityRuntime.Core
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
type | ‘abilityLifecycle’ | Yes | Event type. |
callback | AbilityLifecycleCallback | Yes | Callback used to return the ID of the registered listener. |
Return value
Type | Description |
---|---|
number | ID of the registered listener. The ID is incremented by 1 each time the listener is registered. When the ID exceeds 2^63-1, -1 is returned. |
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 { UIAbility, AbilityLifecycleCallback } from '@kit.AbilityKit';
import { BusinessError } from '@kit.BasicServicesKit';
let lifecycleId: number;
export default class EntryAbility extends UIAbility {
onCreate() {
console.log('MyAbility onCreate');
let AbilityLifecycleCallback: AbilityLifecycleCallback = {
onAbilityCreate(ability) {
console.log(`AbilityLifecycleCallback onAbilityCreate ability: ${ability}`);
},
onWindowStageCreate(ability, windowStage) {
console.log(`AbilityLifecycleCallback onWindowStageCreate ability: ${ability}`);
console.log(`AbilityLifecycleCallback onWindowStageCreate windowStage: ${windowStage}`);
},
onWindowStageActive(ability, windowStage) {
console.log(`AbilityLifecycleCallback onWindowStageActive ability: ${ability}`);
console.log(`AbilityLifecycleCallback onWindowStageActive windowStage: ${windowStage}`);
},
onWindowStageInactive(ability, windowStage) {
console.log(`AbilityLifecycleCallback onWindowStageInactive ability: ${ability}`);
console.log(`AbilityLifecycleCallback onWindowStageInactive windowStage: ${windowStage}`);
},
onWindowStageDestroy(ability, windowStage) {
console.log(`AbilityLifecycleCallback onWindowStageDestroy ability: ${ability}`);
console.log(`AbilityLifecycleCallback onWindowStageDestroy windowStage: ${windowStage}`);
},
onAbilityDestroy(ability) {
console.log(`AbilityLifecycleCallback onAbilityDestroy ability: ${ability}`);
},
onAbilityForeground(ability) {
console.log(`AbilityLifecycleCallback onAbilityForeground ability: ${ability}`);
},
onAbilityBackground(ability) {
console.log(`AbilityLifecycleCallback onAbilityBackground ability: ${ability}`);
},
onAbilityContinue(ability) {
console.log(`AbilityLifecycleCallback onAbilityContinue ability: ${ability}`);
}
}
// 1. Obtain applicationContext through the context property.
let applicationContext = this.context.getApplicationContext();
try {
// 2. Use applicationContext.on() to subscribe to the 'abilityLifecycle' event.
lifecycleId = applicationContext.on('abilityLifecycle', AbilityLifecycleCallback);
} catch (paramError) {
console.error(`error: ${(paramError as BusinessError).code}, ${(paramError as BusinessError).message}`);
}
console.log(`registerAbilityLifecycleCallback lifecycleId: ${lifecycleId}`);
}
}
ApplicationContext.off(‘abilityLifecycle’)
off(type: ‘abilityLifecycle’, callbackId: number, callback: AsyncCallback<void>): void
Unregisters the listener that monitors the ability lifecycle of the application. This API uses an asynchronous callback to return the result. It can be called only by the main thread.
Atomic service API: This API can be used in atomic services since API version 11.
System capability: SystemCapability.Ability.AbilityRuntime.Core
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
type | ‘abilityLifecycle’ | Yes | Event type. |
callbackId | number | Yes | ID of the listener to unregister. |
callback | AsyncCallback<void> | Yes | Callback used to return the result. If the deregistration is successful, 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 { UIAbility } from '@kit.AbilityKit';
import { BusinessError } from '@kit.BasicServicesKit';
let lifecycleId: number;
export default class EntryAbility extends UIAbility {
onDestroy() {
let applicationContext = this.context.getApplicationContext();
console.log(`stage applicationContext: ${applicationContext}`);
try {
applicationContext.off('abilityLifecycle', lifecycleId, (error, data) => {
if (error) {
console.error(`unregisterAbilityLifecycleCallback fail, err: ${JSON.stringify(error)}`);
} else {
console.log(`unregisterAbilityLifecycleCallback success, data: ${JSON.stringify(data)}`);
}
});
} catch (paramError) {
console.error(`error: ${(paramError as BusinessError).code}, ${(paramError as BusinessError).message}`);
}
}
}
ApplicationContext.off(‘abilityLifecycle’)
off(type: ‘abilityLifecycle’, callbackId: number): Promise<void>
Unregisters the listener that monitors the ability lifecycle of the application. This API uses a promise to return the result. It can be called only by the main thread.
Atomic service API: This API can be used in atomic services since API version 11.
System capability: SystemCapability.Ability.AbilityRuntime.Core
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
type | ‘abilityLifecycle’ | Yes | Event type. |
callbackId | number | Yes | ID of the listener to unregister. |
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. |
Example
import { UIAbility } from '@kit.AbilityKit';
import { BusinessError } from '@kit.BasicServicesKit';
let lifecycleId: number;
export default class MyAbility extends UIAbility {
onDestroy() {
let applicationContext = this.context.getApplicationContext();
console.log(`stage applicationContext: ${applicationContext}`);
try {
applicationContext.off('abilityLifecycle', lifecycleId);
} catch (paramError) {
console.error(`error: ${(paramError as BusinessError).code}, ${(paramError as BusinessError).message}`);
}
}
}
ApplicationContext.on(‘environment’)
on(type: ‘environment’, callback: EnvironmentCallback): number
Registers a listener for system environment changes. This API uses an asynchronous callback to return the result. It can be called only by the main thread.
Atomic service API: This API can be used in atomic services since API version 11.
System capability: SystemCapability.Ability.AbilityRuntime.Core
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
type | ‘environment’ | Yes | Event type. |
callback | EnvironmentCallback | Yes | Callback used to return the system environment changes. |
Return value
Type | Description |
---|---|
number | ID of the registered listener. The ID is incremented by 1 each time the listener is registered. When the ID exceeds 2^63-1, -1 is returned. |
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 { UIAbility, EnvironmentCallback } from '@kit.AbilityKit';
import { BusinessError } from '@kit.BasicServicesKit';
let callbackId: number;
export default class EntryAbility extends UIAbility {
onCreate() {
console.log('MyAbility onCreate')
let environmentCallback: EnvironmentCallback = {
onConfigurationUpdated(config) {
console.log(`onConfigurationUpdated config: ${JSON.stringify(config)}`);
},
onMemoryLevel(level) {
console.log(`onMemoryLevel level: ${level}`);
}
};
// 1. Obtain an applicationContext object.
let applicationContext = this.context.getApplicationContext();
try {
// 2. Use applicationContext.on() to subscribe to the 'environment' event.
callbackId = applicationContext.on('environment', environmentCallback);
} catch (paramError) {
console.error(`error: ${(paramError as BusinessError).code}, ${(paramError as BusinessError).message}`);
}
console.log(`registerEnvironmentCallback callbackId: ${callbackId}`);
}
}
ApplicationContext.off(‘environment’)
off(type: ‘environment’, callbackId: number, callback: AsyncCallback<void>): void
Unregisters the listener for system environment changes. This API uses an asynchronous callback to return the result. It can be called only by the main thread.
Atomic service API: This API can be used in atomic services since API version 11.
System capability: SystemCapability.Ability.AbilityRuntime.Core
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
type | ‘environment’ | Yes | Event type. |
callbackId | number | Yes | ID of the listener to unregister. |
callback | AsyncCallback<void> | Yes | Callback used to return the result. If the deregistration is successful, 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 { UIAbility } from '@kit.AbilityKit';
import { BusinessError } from '@kit.BasicServicesKit';
let callbackId: number;
export default class EntryAbility extends UIAbility {
onDestroy() {
let applicationContext = this.context.getApplicationContext();
try {
applicationContext.off('environment', callbackId, (error, data) => {
if (error) {
console.error(`unregisterEnvironmentCallback fail, err: ${JSON.stringify(error)}`);
} else {
console.log(`unregisterEnvironmentCallback success, data: ${JSON.stringify(data)}`);
}
});
} catch (paramError) {
console.error(`error: ${(paramError as BusinessError).code}, ${(paramError as BusinessError).message}`);
}
}
}
ApplicationContext.off(‘environment’)
off(type: ‘environment’, callbackId: number): Promise<void>
Unregisters the listener for system environment changes. This API uses a promise to return the result. It can be called only by the main thread.
Atomic service API: This API can be used in atomic services since API version 11.
System capability: SystemCapability.Ability.AbilityRuntime.Core
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
type | ‘environment’ | Yes | Event type. |
callbackId | number | Yes | ID of the listener to unregister. |
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. |
Example
import { UIAbility } from '@kit.AbilityKit';
import { BusinessError } from '@kit.BasicServicesKit';
let callbackId: number;
export default class MyAbility extends UIAbility {
onDestroy() {
let applicationContext = this.context.getApplicationContext();
try {
applicationContext.off('environment', callbackId);
} catch (paramError) {
console.error(`error: ${(paramError as BusinessError).code}, ${(paramError as BusinessError).message}`);
}
}
}
ApplicationContext.on(‘applicationStateChange’)10+
on(type: ‘applicationStateChange’, callback: ApplicationStateChangeCallback): void
Registers a listener for application foreground/background state changes. This API uses an asynchronous callback to return the result. It can be called only by the main thread.
Atomic service API: This API can be used in atomic services since API version 11.
System capability: SystemCapability.Ability.AbilityRuntime.Core
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
type | ‘applicationStateChange’ | Yes | Event type. |
callback | ApplicationStateChangeCallback | Yes | Callback used to return the result. You can define a callback for switching from the background to the foreground and a callback for switching from the foreground to the background. |
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 { UIAbility, ApplicationStateChangeCallback } from '@kit.AbilityKit';
import { BusinessError } from '@kit.BasicServicesKit';
export default class MyAbility extends UIAbility {
onCreate() {
console.log('MyAbility onCreate');
let applicationStateChangeCallback: ApplicationStateChangeCallback = {
onApplicationForeground() {
console.info('applicationStateChangeCallback onApplicationForeground');
},
onApplicationBackground() {
console.info('applicationStateChangeCallback onApplicationBackground');
}
}
// 1. Obtain an applicationContext object.
let applicationContext = this.context.getApplicationContext();
try {
// 2. Use applicationContext.on() to subscribe to the 'applicationStateChange' event.
applicationContext.on('applicationStateChange', applicationStateChangeCallback);
} catch (paramError) {
console.error(`error: ${(paramError as BusinessError).code}, ${(paramError as BusinessError).message}`);
}
console.log('Register applicationStateChangeCallback');
}
}
ApplicationContext.off(‘applicationStateChange’)10+
off(type: ‘applicationStateChange’, callback?: ApplicationStateChangeCallback): void
Unregisters the listener for application foreground/background state changes. This API uses an asynchronous callback to return the result. It can be called only by the main thread.
NOTE
A listener must have been registered by calling ApplicationContext.on(‘applicationStateChange’).
Atomic service API: This API can be used in atomic services since API version 11.
System capability: SystemCapability.Ability.AbilityRuntime.Core
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
type | ‘applicationStateChange’ | Yes | Event type. |
callback | ApplicationStateChangeCallback | No | Callback used to return the result. The value can be a callback defined by ApplicationContext.on(‘applicationStateChange’) or empty. - If a defined callback is passed in, the listener for that callback is unregistered. - If no value is passed in, all the listeners for the corresponding event are unregistered. |
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
Assume that ApplicationContext.on(‘applicationStateChange’) is used to register a callback named applicationStateChangeCallback. The following example shows how to unregister the corresponding listener.
import { UIAbility, ApplicationStateChangeCallback } from '@kit.AbilityKit';
import { BusinessError } from '@kit.BasicServicesKit';
let applicationStateChangeCallback: ApplicationStateChangeCallback = {
onApplicationForeground() {
console.info('applicationStateChangeCallback onApplicationForeground');
},
onApplicationBackground() {
console.info('applicationStateChangeCallback onApplicationBackground');
}
};
export default class MyAbility extends UIAbility {
onDestroy() {
let applicationContext = this.context.getApplicationContext();
try {
// In this example, the callback field is set to applicationStateChangeCallback.
// If no value is passed in for the callback field, all the listeners registered for the application foreground/background state change event are canceled.
applicationContext.off('applicationStateChange', applicationStateChangeCallback);
} catch (paramError) {
console.error(`error: ${(paramError as BusinessError).code}, ${(paramError as BusinessError).message}`);
}
}
}
ApplicationContext.getRunningProcessInformation
getRunningProcessInformation(): Promise<Array<ProcessInformation>>
Obtains information about the running processes. This API uses a promise to return the result.
Atomic service API: This API can be used in atomic services since API version 11.
System capability: SystemCapability.Ability.AbilityRuntime.Core
Return value
Type | Description |
---|---|
Promise<Array<ProcessInformation>> | Promise used to return the API call result and the process running information. You can perform error handling or custom processing in this callback. |
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 { UIAbility } from '@kit.AbilityKit';
import { BusinessError } from '@kit.BasicServicesKit';
export default class MyAbility extends UIAbility {
onForeground() {
let applicationContext = this.context.getApplicationContext();
applicationContext.getRunningProcessInformation().then((data) => {
console.log(`The process running information is: ${JSON.stringify(data)}`);
}).catch((error: BusinessError) => {
console.error(`error: ${JSON.stringify(error)}`);
});
}
}
ApplicationContext.getRunningProcessInformation
getRunningProcessInformation(callback: AsyncCallback<Array<ProcessInformation>>): void
Obtains information about the running processes. This API uses an asynchronous callback to return the result.
Atomic service API: This API can be used in atomic services since API version 11.
System capability: SystemCapability.Ability.AbilityRuntime.Core
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
callback | AsyncCallback<Array<ProcessInformation>> | Yes | Callback used to return the information about the running processes. |
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 { UIAbility } from '@kit.AbilityKit';
export default class MyAbility extends UIAbility {
onForeground() {
let applicationContext = this.context.getApplicationContext();
applicationContext.getRunningProcessInformation((err, data) => {
if (err) {
console.error(`getRunningProcessInformation faile, err: ${JSON.stringify(err)}`);
} else {
console.log(`The process running information is: ${JSON.stringify(data)}`);
}
})
}
}
ApplicationContext.killAllProcesses
killAllProcesses(): Promise<void>
Kills all processes of this application. The application will not go through the normal lifecycle when exiting. This API uses a promise to return the result. It can be called only by the main thread.
NOTE
This API is used to forcibly exit an application in abnormal scenarios. To exit an application properly, call terminateSelf().
Atomic service API: This API can be used in atomic services since API version 11.
System capability: SystemCapability.Ability.AbilityRuntime.Core
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. |
Example
import { UIAbility } from '@kit.AbilityKit';
export default class MyAbility extends UIAbility {
onBackground() {
let applicationContext = this.context.getApplicationContext();
applicationContext.killAllProcesses();
}
}
ApplicationContext.killAllProcesses14+
killAllProcesses(clearPageStack: boolean): Promise<void>
Kills all processes of this application. The application will not go through the normal lifecycle when exiting. This API uses a promise to return the result. It can be called only by the main thread.
NOTE
This API is used to forcibly exit an application in abnormal scenarios. To exit an application properly, call terminateSelf().
Atomic service API: This API can be used in atomic services since API version 14.
System capability: SystemCapability.Ability.AbilityRuntime.Core
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
clearPageStack | boolean | Yes | Whether to clear the page stack. The value true means to clear the page stack, and false means the opposite. |
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 | If the input parameter is not valid parameter. |
16000011 | The context does not exist. |
Example
import { UIAbility } from '@kit.AbilityKit';
let isClearPageStack = false;
export default class MyAbility extends UIAbility {
onBackground() {
let applicationContext = this.context.getApplicationContext();
applicationContext.killAllProcesses(isClearPageStack);
}
}
ApplicationContext.killAllProcesses
killAllProcesses(callback: AsyncCallback<void>)
Kills all processes of this application. The application will not go through the normal lifecycle when exiting. This API uses an asynchronous callback to return the result. It can be called only by the main thread.
NOTE
This API is used to forcibly exit an application in abnormal scenarios. To exit an application properly, call terminateSelf().
Atomic service API: This API can be used in atomic services since API version 11.
System capability: SystemCapability.Ability.AbilityRuntime.Core
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
callback | AsyncCallback<void> | Yes | Callback used to return the result. If all the processes are killed, 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. |
Example
import { UIAbility } from '@kit.AbilityKit';
export default class MyAbility extends UIAbility {
onBackground() {
let applicationContext = this.context.getApplicationContext();
applicationContext.killAllProcesses(error => {
if (error) {
console.error(`killAllProcesses fail, error: ${JSON.stringify(error)}`);
}
});
}
}
ApplicationContext.setColorMode11+
setColorMode(colorMode: ConfigurationConstant.ColorMode): void
Sets the color mode for the application. It can be called only by the main thread.
Atomic service API: This API can be used in atomic services since API version 11.
System capability: SystemCapability.Ability.AbilityRuntime.Core
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
colorMode | ConfigurationConstant.ColorMode | Yes | Target color mode, including dark mode, light mode, and system theme mode (no setting). |
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 { UIAbility, ConfigurationConstant } from '@kit.AbilityKit';
export default class MyAbility extends UIAbility {
onCreate() {
let applicationContext = this.context.getApplicationContext();
applicationContext.setColorMode(ConfigurationConstant.ColorMode.COLOR_MODE_DARK);
}
}
ApplicationContext.setLanguage11+
setLanguage(language: string): void
Sets the language for the application. This API can be called only by the main thread.
Atomic service API: This API can be used in atomic services since API version 11.
System capability: SystemCapability.Ability.AbilityRuntime.Core
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
language | string | Yes | Target language. The list of supported languages can be obtained by calling getSystemLanguages(). |
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 { UIAbility } from '@kit.AbilityKit';
export default class MyAbility extends UIAbility {
onCreate() {
let applicationContext = this.context.getApplicationContext();
applicationContext.setLanguage('zh-cn');
}
}
ApplicationContext.clearUpApplicationData11+
clearUpApplicationData(): Promise<void>
Clears up the application data and revokes the permissions that the application has requested from users. This API uses a promise to return the result. It can be called only by the main thread.
NOTE
This API stops the application process. After the application process is stopped, all subsequent callbacks will not be triggered.
System capability: SystemCapability.Ability.AbilityRuntime.Core
Return value
Type | Description |
---|---|
Promise<void> | Promise that returns no value. |
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 { UIAbility } from '@kit.AbilityKit';
export default class MyAbility extends UIAbility {
onBackground() {
let applicationContext = this.context.getApplicationContext();
applicationContext.clearUpApplicationData();
}
}
ApplicationContext.clearUpApplicationData11+
clearUpApplicationData(callback: AsyncCallback<void>): void
Clears up the application data and revokes the permissions that the application has requested from users. This API uses an asynchronous callback to return the result. It can be called only by the main thread.
NOTE
This API stops the application process. After the application process is stopped, all subsequent callbacks will not be triggered.
System capability: SystemCapability.Ability.AbilityRuntime.Core
Parameters |Name |Type |Mandatory|Description | |————-|——–|—-|————————–| |callback|AsyncCallback<void>|Yes|Callback used to return the result. If the application data is cleared up, error is undefined; otherwise, error 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 { UIAbility } from '@kit.AbilityKit';
export default class MyAbility extends UIAbility {
onBackground() {
let applicationContext = this.context.getApplicationContext();
applicationContext.clearUpApplicationData(error => {
if (error) {
console.error(`clearUpApplicationData fail, error: ${JSON.stringify(error)}`);
}
});
}
}
ApplicationContext.restartApp12+
restartApp(want: Want): void
Restarts the application and starts the specified UIAbility. The onDestroy callback is not triggered during the restart. It can be called only by the main thread, and the application to restart must be active.
Atomic service API: This API can be used in atomic services since API version 12.
System capability: SystemCapability.Ability.AbilityRuntime.Core
Parameters |Name |Type |Mandatory|Description | |————-|——–|—-|————————–| |want|Want|Yes|Want information about the UIAbility to start. No verification is performed on the bundle name passed in.|
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. |
16000050 | Internal error. |
16000053 | The ability is not on the top of the UI. |
16000063 | The target to restart does not belong to the current application or is not a UIAbility. |
16000064 | Restart too frequently. Try again at least 3s later. |
Example
import { UIAbility, Want } from '@kit.AbilityKit';
export default class MyAbility extends UIAbility {
onForeground() {
let applicationContext = this.context.getApplicationContext();
let want: Want = {
bundleName: 'com.example.myapp',
abilityName: 'EntryAbility'
};
try {
applicationContext.restartApp(want);
} catch (error) {
console.error(`restartApp fail, error: ${JSON.stringify(error)}`);
}
}
}
ApplicationContext.getCurrentAppCloneIndex12+
getCurrentAppCloneIndex(): number
Obtains the index of the current application clone.
Atomic service API: This API can be used in atomic services since API version 12.
System capability: SystemCapability.Ability.AbilityRuntime.Core
Return value
Type | Description |
---|---|
number | Index of the current application clone. |
Error codes
ID | Error Message |
---|---|
16000011 | The context does not exist. |
16000071 | The MultiAppMode is not {@link APP_CLONE}. |
For details about the error codes, see Ability Error Codes.
Example
import { UIAbility } from '@kit.AbilityKit';
export default class MyAbility extends UIAbility {
onBackground() {
let applicationContext = this.context.getApplicationContext();
try {
let appCloneIndex = applicationContext.getCurrentAppCloneIndex();
} catch (error) {
console.error(`getCurrentAppCloneIndex fail, error: ${JSON.stringify(error)}`);
}
}
}
ApplicationContext.setFont12+
setFont(font: string): void
Sets the font for this application. This API can be called only by the main thread.
NOTE
This API can be called only after a page window is created. That is, this API must be called after the lifecycle callback onWindowStageCreate().
System capability: SystemCapability.Ability.AbilityRuntime.Core
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
font | string | Yes | Font, which can be registered by calling UIContext.registerFont. |
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 { font } from '@kit.ArkUI';
import { common } from '@kit.AbilityKit';
@Entry
@Component
struct Index {
@State message: string = 'Hello World';
context = this.getUIContext().getHostContext() as common.UIAbilityContext;
aboutToAppear() {
this.getUIContext().getFont().registerFont({
familyName: 'fontName',
familySrc: $rawfile('font/medium.ttf')
})
this.context.getApplicationContext().setFont("fontName");
}
build() {
Row() {
Column() {
Text(this.message)
.fontSize(50)
.fontWeight(50)
}
.width('100%')
}
.height('100%')
}
}
ApplicationContext.setSupportedProcessCache12+
setSupportedProcessCache(isSupported : boolean): void
Sets whether the application itself supports process cache, which enables quick startup after caching. It can be called only by the main thread.
NOTE
- This API only sets the application to be ready for quick startup after caching. It does not mean that quick startup will be triggered. Other conditions must be considered to determine whether to trigger quick startup.
- The process cache support status takes effect for a single application process instance. The setting does not affect other process instances. After a process instance is destroyed, the status is not retained and can be reset.
- To support process cache, you must call this API, with true passed in, in the onCreate() lifecycle of all AbilityStages in the same process.
Model restriction: This API can be used only in the stage model.
System capability: SystemCapability.Ability.AbilityRuntime.Core
Parameters |Name |Type |Mandatory|Description | |————-|——–|—-|————————–| |isSupported|boolean|Yes|Whether process cache is supported. The value true means that process cache is supported, and false means the opposite.|
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. |
801 | Capability not supported. |
16000011 | The context does not exist. |
16000050 | Internal error. |
Example
import { AbilityStage, Want } from '@kit.AbilityKit';
import { BusinessError } from '@kit.BasicServicesKit';
class MyAbilityStage extends AbilityStage {
onCreate() {
let applicationContext = this.context.getApplicationContext();
try {
applicationContext.setSupportedProcessCache(true);
} catch (error) {
let code = (error as BusinessError).code;
let message = (error as BusinessError).message;
console.error(`setSupportedProcessCache fail, code: ${code}, msg: ${message}`);
}
}
}
ApplicationContext.setFontSizeScale13+
setFontSizeScale(fontSizeScale: number): void
Sets the scale ratio for the font size of this application. It can be called only by the main thread.
Atomic service API: This API can be used in atomic services since API version 13.
System capability: SystemCapability.Ability.AbilityRuntime.Core
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
fontSizeScale | number | Yes | Font scale ratio. The value is a non-negative number. When the application’s fontSizeScale is set to followSystem and the value set here exceeds the value of fontSizeMaxScale, the value of fontSizeMaxScale takes effect. |
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. |
Example
import { UIAbility } from '@kit.AbilityKit';
import { window } from '@kit.ArkUI';
export default class MyAbility extends UIAbility {
onWindowStageCreate(windowStage: window.WindowStage) {
windowStage.loadContent('pages/Index', (err, data) => {
if (err.code) {
return;
}
let applicationContext = this.context.getApplicationContext();
applicationContext.setFontSizeScale(2);
});
}
}
ApplicationContext.getCurrentInstanceKey14+
getCurrentInstanceKey(): string
Obtains the unique instance ID of this application. It can be called only by the main thread.
NOTE
This API is valid only for 2-in-1 devices.
System capability: SystemCapability.Ability.AbilityRuntime.Core
Return value
Type | Description |
---|---|
string | Unique instance ID of the application. |
Error codes
For details about the error codes, see Universal Error Codes.
ID | Error Message |
---|---|
16000011 | The context does not exist. |
16000078 | The multi-instance is not supported. |
Example
import { AbilityStage } from '@kit.AbilityKit';
import { BusinessError } from '@kit.BasicServicesKit';
class MyAbilityStage extends AbilityStage {
onCreate() {
let applicationContext = this.context.getApplicationContext();
let currentInstanceKey = '';
try {
currentInstanceKey = applicationContext.getCurrentInstanceKey();
} catch (error) {
let code = (error as BusinessError).code;
let message = (error as BusinessError).message;
console.error(`getCurrentInstanceKey fail, code: ${code}, msg: ${message}`);
}
console.log(`currentInstanceKey: ${currentInstanceKey}`);
}
}
ApplicationContext.getAllRunningInstanceKeys14+
getAllRunningInstanceKeys(): Promise<Array<string>>;
Obtains the unique instance IDs of all multi-instances of this application. This API uses a promise to return the result. It can be called only by the main thread.
NOTE
This API is valid only for 2-in-1 devices.
System capability: SystemCapability.Ability.AbilityRuntime.Core
Return value
Type | Description |
---|---|
Promise<Array<string>> | Promise used to return the unique instance IDs of all multi-instances of the application. |
Error codes
For details about the error codes, see Universal Error Codes.
ID | Error Message |
---|---|
16000011 | The context does not exist. |
16000050 | Internal error. |
16000078 | The multi-instance is not supported. |
Example
import { AbilityStage } from '@kit.AbilityKit';
import { BusinessError } from '@kit.BasicServicesKit';
class MyAbilityStage extends AbilityStage {
onCreate() {
let applicationContext = this.context.getApplicationContext();
try {
applicationContext.getAllRunningInstanceKeys();
} catch (error) {
let code = (error as BusinessError).code;
let message = (error as BusinessError).message;
console.error(`getAllRunningInstanceKeys fail, code: ${code}, msg: ${message}`);
}
}
}
你可能感兴趣的鸿蒙文章
harmony 鸿蒙AbilityAccessControl
harmony 鸿蒙OH_NativeBundle_ApplicationInfo
harmony 鸿蒙OH_NativeBundle_ElementName
- 所属分类: 后端技术
- 本文标签:
热门推荐
-
2、 - 优质文章
-
3、 gate.io
-
8、 golang
-
9、 openharmony
-
10、 Vue中input框自动聚焦