openharmony 鸿蒙 js-apis-app-form-formObserver-sys

2025-06-12 浏览 (1)

@ohos.app.form.formObserver (formObserver) (System API)

The formObserver module provides APIs related to widget listeners. You can use the APIs to subscribe to and unsubscribe from widget addition, removal, and visibility change events, and obtain information about running widgets.

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 provided by this module are system APIs.

Modules to Import

import { formObserver } from '@kit.FormKit';

on('formAdd')

on(type: 'formAdd', observerCallback: Callback<formInfo.RunningFormInfo>): void

Subscribes to widget addition events. This API uses an asynchronous callback to return the information about the new widget.

Required permissions: ohos.permission.OBSERVE_FORM_RUNNING

System capability: SystemCapability.Ability.Form

Parameters

NameTypeMandatoryDescription
typestringYesEvent type. The value 'formAdd' indicates a widget addition event.
observerCallbackCallback<formInfo.RunningFormInfo>YesCallback used to return the information about the new widget.

Error codes

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

IDError Message
202The application is not a system application.
401Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types; 3.Parameter verification failed.

Example

import { formInfo, formObserver } from '@kit.FormKit';

let callback = (data: formInfo.RunningFormInfo) => {
  console.log(`a new form added, data: ${JSON.stringify(data)}`);
}

formObserver.on('formAdd', callback);

on('formAdd')

on(type: 'formAdd', hostBundleName: string, observerCallback: Callback<formInfo.RunningFormInfo>): void

Subscribes to widget addition events. This API uses an asynchronous callback to return the information about the new widget.

Required permissions: ohos.permission.OBSERVE_FORM_RUNNING

System capability: SystemCapability.Ability.Form

Parameters

NameTypeMandatoryDescription
typestringYesEvent type. The value 'formAdd' indicates a widget addition event.
hostBundleNamestringYesName of the bundle that functions as the widget host. If no value is passed in, widget addition events of all widget hosts are subscribed to.
observerCallbackCallback<formInfo.RunningFormInfo>YesCallback used to return the information about the new widget.

Error codes

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

IDError Message
202The application is not a system application.
401Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types; 3.Parameter verification failed.

Example

import { formInfo, formObserver } from '@kit.FormKit';

let bundleName: string = 'ohos.samples.FormApplication';

let callback = (data: formInfo.RunningFormInfo) => {
  console.log(`a new form added, data: ${JSON.stringify(data)}`);
}

formObserver.on('formAdd', bundleName, callback);

off('formAdd')

off(type: "formAdd", hostBundleName?: string, observerCallback?: Callback<formInfo.RunningFormInfo>): void

Unsubscribes from widget addition events. This API uses an asynchronous callback to return the information about the new widget.

Required permissions: ohos.permission.OBSERVE_FORM_RUNNING

System capability: SystemCapability.Ability.Form

Parameters

NameTypeMandatoryDescription
typestringYesEvent type. The value 'formAdd' indicates a widget addition event.
hostBundleNamestringNoName of the bundle that functions as the widget host.
To cancel the subscription for a given bundle name, this parameter must be set to the same value as bundleName in on('formAdd').
If no value is passed in, the subscriptions for all the widget hosts are canceled.
observerCallbackCallback<formInfo.RunningFormInfo>NoCallback used to return the information about the new widget. If no value is passed in, all the subscriptions to the specified event are canceled.
To cancel the subscription with a given callback, this parameter must be set to the same value as callback in on('formAdd').

Error codes

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

IDError Message
202The application is not a system application.
401Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types; 3.Parameter verification failed.

Example

import { formInfo, formObserver } from '@kit.FormKit';

let bundleName: string = 'ohos.samples.FormApplication';

let callback = (data: formInfo.RunningFormInfo) => {
  console.log(`a new form added, data: ${JSON.stringify(data)}`);
}

formObserver.off('formAdd', bundleName, callback);

NOTE

on('formAdd', callback) and off('formAdd', callback) must be used in pairs. on('formAdd', bundleName, callback) and off('formAdd', bundleName, callback) must be used in pairs. To cancel the subscription with a given callback or for a given bundle name, the callback or bundleName parameter in off() must be set to the same value as that in on().

on('formRemove')

on(type: 'formRemove', observerCallback: Callback<formInfo.RunningFormInfo>): void

Subscribes to widget removal events. This API uses an asynchronous callback to return the information about the widget removed.

Required permissions: ohos.permission.OBSERVE_FORM_RUNNING

System capability: SystemCapability.Ability.Form

Parameters

NameTypeMandatoryDescription
typestringYesEvent type. The value 'formRemove' indicates a widget removal event.
observerCallbackCallback<formInfo.RunningFormInfo>YesCallback used to return the information about the widget removed.

Error codes

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

IDError Message
202The application is not a system application.
401Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types; 3.Parameter verification failed.

Example

import { formInfo, formObserver } from '@kit.FormKit';

let callback = (data: formInfo.RunningFormInfo) => {
  console.log(`form deleted, data: ${JSON.stringify(data)}`);
}

formObserver.on('formRemove', callback);

on('formRemove')

on(type: 'formRemove', hostBundleName: string, observerCallback: Callback<formInfo.RunningFormInfo>): void

Subscribes to widget addition events. This API uses an asynchronous callback to return the information about the widget removed.

Required permissions: ohos.permission.OBSERVE_FORM_RUNNING

System capability: SystemCapability.Ability.Form

Parameters

NameTypeMandatoryDescription
typestringYesEvent type. The value 'formRemove' indicates a widget removal event.
hostBundleNamestringYesName of the bundle that functions as the widget host. If no value is passed in, widget removal events of all widget hosts are subscribed to.
observerCallbackCallback<formInfo.RunningFormInfo>YesCallback used to return the information about the widget removed.

Error codes

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

IDError Message
202The application is not a system application.
401Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types; 3.Parameter verification failed.

Example

import { formInfo, formObserver } from '@kit.FormKit';

let bundleName: string = 'ohos.samples.FormApplication';

let callback = (data: formInfo.RunningFormInfo) => {
  console.log(`form deleted, data: ${JSON.stringify(data)}`);
}

formObserver.on('formRemove', bundleName, callback);

off('formRemove')

off(type: "formRemove", hostBundleName?: string, observerCallback?: Callback<formInfo.RunningFormInfo>): void

Unsubscribes from widget removal events. This API uses an asynchronous callback to return the information about the widget removed.

Required permissions: ohos.permission.OBSERVE_FORM_RUNNING

System capability: SystemCapability.Ability.Form

Parameters

NameTypeMandatoryDescription
typestringYesEvent type. The value 'formRemove' indicates a widget removal event.
hostBundleNamestringNoName of the bundle that functions as the widget host.
To cancel the subscription for a given bundle name, this parameter must be set to the same value as bundleName in on('formAdd').
If no value is passed in, the subscriptions for all the widget hosts are canceled.
observerCallbackCallback<formInfo.RunningFormInfo>NoCallback used to return the information about the widget removed. If no value is passed in, all the subscriptions to the specified event are canceled.
To cancel the subscription with a given callback, this parameter must be set to the same value as callback in on('formRemove').

Error codes

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

IDError Message
202The application is not a system application.
401Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types; 3.Parameter verification failed.

Example

import { formInfo, formObserver } from '@kit.FormKit';

let bundleName: string = 'ohos.samples.FormApplication';

let callback = (data: formInfo.RunningFormInfo) => {
  console.log(`a new form added, data: ${JSON.stringify(data)}`);
}

formObserver.off('formRemove', bundleName, callback);

NOTE

on('formRemove', callback) and off('formRemove', callback) must be used in pairs. on('formRemove', bundleName, callback) and off('formRemove', bundleName, callback) must be used in pairs. To cancel the subscription with a given callback or for a given bundle name, the callback or bundleName parameter in off() must be set to the same value as that in on().

on('notifyVisible')

on(type: 'notifyVisible', observerCallback: Callback<Array<formInfo.RunningFormInfo>>): void

Subscribes to events indicating that a widget becomes visible. This API uses an asynchronous callback to return the result.

​The event is triggered when notifyVisibleForms is called to notify that the widget becomes visible.

Required permissions: ohos.permission.OBSERVE_FORM_RUNNING

System capability: SystemCapability.Ability.Form

Parameters

NameTypeMandatoryDescription
typestringYesEvent type. This value 'notifyVisible' indicates a widget visibility event.
observerCallbackCallback <Array<formInfo.RunningFormInfo>>YesCallback used to return an array of widgets that have subscribed to the event.

Error codes

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

IDError Message
202The application is not a system application.
401Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types; 3.Parameter verification failed.

Example

import { formInfo, formObserver } from '@kit.FormKit';

let callback = (data: formInfo.RunningFormInfo[]) => {
  console.log(`form change visibility, data: ${JSON.stringify(data)}`);
}

formObserver.on('notifyVisible', callback);

on('notifyVisible')

on(type: 'notifyVisible', hostBundleName: string, observerCallback: Callback<Array<formInfo.RunningFormInfo>>): void

Subscribes to events indicating that a widget becomes visible for a given bundle that functions as the widget host. This API uses an asynchronous callback to return the result.

​The event is triggered when notifyVisibleForms is called to notify that the widget becomes visible.

Required permissions: ohos.permission.OBSERVE_FORM_RUNNING

System capability: SystemCapability.Ability.Form

Parameters

NameTypeMandatoryDescription
typestringYesEvent type. This value 'notifyVisible' indicates a widget visibility event.
hostBundleNamestringYesName of the bundle that functions as the widget host, on which the widget visibility state changes are subscribed.
observerCallbackCallback <Array<formInfo.RunningFormInfo>>YesCallback used to return an array of widgets that have subscribed to the event.

Error codes

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

IDError Message
202The application is not a system application.
401Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types; 3.Parameter verification failed.

Example

import { formInfo, formObserver } from '@kit.FormKit';

let bundleName: string = 'ohos.samples.FormApplication';

let callback = (data: formInfo.RunningFormInfo[]) => {
  console.log(`form change visibility, data: ${JSON.stringify(data)}`);
}

formObserver.on('notifyVisible', bundleName, callback);

off('notifyVisible')

off(type: "notifyVisible", hostBundleName?: string, observerCallback?: Callback<Array<formInfo.RunningFormInfo>>): void

Unsubscribes from events indicating that a widget becomes visible. This API uses an asynchronous callback to return the result.

Required permissions: ohos.permission.OBSERVE_FORM_RUNNING

System capability: SystemCapability.Ability.Form

Parameters

NameTypeMandatoryDescription
typestringYesEvent type. This value 'notifyVisible' indicates a widget visibility event.
hostBundleNamestringNoName of the bundle that functions as the widget host, on which the widget visibility state changes are subscribed.
To cancel the subscription for a given bundle name, this parameter must be set to the same value as bundleName in on('notifyVisible').
observerCallbackCallback <Array<formInfo.RunningFormInfo>>NoCallback used to return an array of widgets that have unsubscribed from the event. If no value is passed in, all the subscriptions to the specified event are canceled.
To cancel the subscription with a given callback, this parameter must be set to the same value as callback in on('notifyVisible').

Error codes

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

IDError Message
202The application is not a system application.
401Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types; 3.Parameter verification failed.

Example

import { formInfo, formObserver } from '@kit.FormKit';

let bundleName: string = 'ohos.samples.FormApplication';

let callback = (data: formInfo.RunningFormInfo[]) => {
  console.log(`form change visibility, data: ${JSON.stringify(data)}`);
}

formObserver.off('notifyVisible', bundleName, callback);

NOTE

on('notifyVisible', callback) and off('notifyVisible', callback) must be used in pairs. on('notifyVisible', bundleName, callback) and off('notifyVisible', bundleName, callback) must be used in pairs. To cancel the subscription with a given callback or for a given bundle name, the callback or bundleName parameter in off() must be set to the same value as that in on().

on('notifyInvisible')

on(type: 'notifyInvisible', observerCallback: Callback<Array<formInfo.RunningFormInfo>>): void

Subscribes to events indicating that a widget becomes invisible. This API uses an asynchronous callback to return the result.

​The event is triggered when notifyInvisibleForms is called to notify that the widget becomes invisible.

Required permissions: ohos.permission.OBSERVE_FORM_RUNNING

System capability: SystemCapability.Ability.Form

Parameters

NameTypeMandatoryDescription
typestringYesEvent type. This value 'notifyInvisible' indicates a widget invisibility event.
observerCallbackCallback <Array<formInfo.RunningFormInfo>>YesCallback used to return an array of widgets that have subscribed to the event.

Error codes

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

IDError Message
202The application is not a system application.
401Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types; 3.Parameter verification failed.

Example

import { formInfo, formObserver } from '@kit.FormKit';

let callback = (data: formInfo.RunningFormInfo[]) => {
  console.log(`form change invisibility, data: ${JSON.stringify(data)}`);
}

formObserver.on('notifyInvisible', callback);

on('notifyInvisible')

on(type: 'notifyInvisible', hostBundleName: string, observerCallback: Callback<Array<formInfo.RunningFormInfo>>): void

Subscribes to events indicating that a widget becomes invisible for a given bundle, which functions as the widget host. This API uses an asynchronous callback to return the result.

​The event is triggered when notifyInvisibleForms is called to notify that the widget becomes invisible.

Required permissions: ohos.permission.OBSERVE_FORM_RUNNING

System capability: SystemCapability.Ability.Form

Parameters

NameTypeMandatoryDescription
typestringYesEvent type. This value 'notifyInvisible' indicates a widget invisibility event.
hostBundleNamestringYesName of the bundle that functions as the widget host, on which the widget visibility state changes are subscribed.
observerCallbackCallback <Array<formInfo.RunningFormInfo>>YesCallback used to return an array of widgets that have subscribed to the event.

Error codes

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

IDError Message
202The application is not a system application.
401Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types; 3.Parameter verification failed.

Example

import { formInfo, formObserver } from '@kit.FormKit';

let bundleName: string = 'ohos.samples.FormApplication';

let callback = (data: formInfo.RunningFormInfo[]) => {
  console.log(`form change invisibility, data: ${JSON.stringify(data)}`);
}

formObserver.on('notifyInvisible', bundleName, callback);

off('notifyInvisible')

off(type: "notifyInvisible", hostBundleName?: string, observerCallback?: Callback<Array<formInfo.RunningFormInfo>>): void

Unsubscribes from events indicating that a widget becomes invisible. This API uses an asynchronous callback to return the result.

Required permissions: ohos.permission.OBSERVE_FORM_RUNNING

System capability: SystemCapability.Ability.Form

Parameters

NameTypeMandatoryDescription
typestringYesEvent type. This value 'notifyInvisible' indicates a widget invisibility event.
hostBundleNamestringNoName of the bundle that functions as the widget host, on which the widget visibility state changes are subscribed.
To cancel the subscription for a given bundle name, this parameter must be set to the same value as bundleName in on('notifyVisible').
observerCallbackCallback <Array<formInfo.RunningFormInfo>>NoCallback used to return an array of widgets that have unsubscribed from the event. If no value is passed in, all the subscriptions to the specified event are canceled.
To cancel the subscription with a given callback, this parameter must be set to the same value as callback in on('notifyInvisible').

Error codes

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

IDError Message
202The application is not a system application.
401Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types; 3.Parameter verification failed.

Example

import { formInfo, formObserver } from '@kit.FormKit';

let bundleName: string = 'ohos.samples.FormApplication';

let callback = (data: formInfo.RunningFormInfo[]) => {
  console.log(`form change invisibility, data: ${JSON.stringify(data)}`);
}

formObserver.off('notifyInvisible', bundleName, callback);

NOTE

on('notifyInvisible', callback) and off('notifyInvisible', callback) must be used in pairs. on('notifyInvisible', bundleName, callback) and off('notifyInvisible', bundleName, callback) must be used in pairs. To cancel the subscription with a given callback or for a given bundle name, the callback or bundleName parameter in off() must be set to the same value as that in on().

getRunningFormInfos

getRunningFormInfos(callback: AsyncCallback<Array<formInfo.RunningFormInfo>>, hostBundleName?: string): void

Obtains the information about all non-temporary widgets running on the device. This API uses an asynchronous callback to return the result.

Required permissions: ohos.permission.OBSERVE_FORM_RUNNING

System capability: SystemCapability.Ability.Form

Parameters

NameTypeMandatoryDescription
callbackAsyncCallback<Array<formInfo.RunningFormInfo>>YesCallback used to return the information about all non-temporary widgets. If the widget information is obtained, error is undefined, and data is the information obtained.
hostBundleNamestringNoName of the bundle that functions as the widget host. If a value is passed in, only the information about the non-temporary widgets that are running under the widget host is returned.
If no value is passed in, information about all running non-temporary widgets on the device is returned.

Error codes

For details about the error codes, see Universal Error Codes and Form Error Codes.

IDError Message
201Permissions denied.
202The application is not a system application.
401Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types; 3.Parameter verification failed.
16500050IPC connection error.
16500060Service connection error.

Example

import { formInfo, formObserver } from '@kit.FormKit';
import { BusinessError } from '@kit.BasicServicesKit';

try {
  formObserver.getRunningFormInfos((error: BusinessError, data: formInfo.RunningFormInfo[]) => {
    if (error) {
      console.error(`error, code: ${error.code}, message: ${error.message}`);
    } else {
      console.log(`formObserver getRunningFormInfos, data: ${JSON.stringify(data)}`);
    }
  }, 'com.example.ohos.formjsdemo');
} catch(error) {
  console.error(`catch error, code: ${(error as BusinessError).code}, message: ${(error as BusinessError).message}`);
}

getRunningFormInfos11+

getRunningFormInfos(callback: AsyncCallback<Array<formInfo.RunningFormInfo>>, isUnusedIncluded: boolean, hostBundleName?: string): void

Obtains the information about all non-temporary widgets running on the device. This API uses an asynchronous callback to return the result.

Required permissions: ohos.permission.OBSERVE_FORM_RUNNING

System capability: SystemCapability.Ability.Form

Parameters

NameTypeMandatoryDescription
callbackAsyncCallback<Array<formInfo.RunningFormInfo>>YesCallback used to return the information about all non-temporary widgets. If the widget information is obtained, error is undefined, and data is the information obtained.
isUnusedIncludedbooleanYesWhether an unused widget is included.
hostBundleNamestringNoName of the bundle that functions as the widget host. If a value is passed in, only the information about the non-temporary widgets that are running under the widget host is returned.
If no value is passed in, information about all running non-temporary widgets on the device is returned.

Error codes

For details about the error codes, see Universal Error Codes and Form Error Codes.

IDError Message
201Permissions denied.
202The application is not a system application.
401Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types; 3.Parameter verification failed.
16500050IPC connection error.
16500060Service connection error.

Example

import { formInfo, formObserver } from '@kit.FormKit';
import { BusinessError } from '@kit.BasicServicesKit';

try {
  formObserver.getRunningFormInfos((error: BusinessError, data: formInfo.RunningFormInfo[]) => {
    if (error) {
      console.error(`error, code: ${error.code}, message: ${error.message}`);
    } else {
      console.log(`formObserver getRunningFormInfos, data: ${JSON.stringify(data)}`);
    }
  }, true, 'com.example.ohos.formjsdemo');
} catch(error) {
  console.error(`catch error, code: ${(error as BusinessError).code}, message: ${(error as BusinessError).message}`);
}

getRunningFormInfos

getRunningFormInfos(hostBundleName?: string): Promise<Array<formInfo.RunningFormInfo>>

Obtains the information about all non-temporary widgets running on the device. This API uses a promise to return the result.

Required permissions: ohos.permission.OBSERVE_FORM_RUNNING

System capability: SystemCapability.Ability.Form

Parameters

NameTypeMandatoryDescription
hostBundleNamestringNoName of the bundle that functions as the widget host. If a value is passed in, only the information about the non-temporary widgets that are running under the widget host is returned.
If no value is passed in, information about all running non-temporary widgets on the device is returned.

Return value

TypeDescription
Promise<Array<formInfo.RunningFormInfo>>Promise used to return the information about all non-temporary widgets.

Error codes

For details about the error codes, see Universal Error Codes and Form Error Codes.

IDError Message
201Permissions denied.
202The application is not a system application.
401Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types; 3.Parameter verification failed.
16500050IPC connection error.
16500060Service connection error.

Example

import { formInfo, formObserver } from '@kit.FormKit';
import { BusinessError } from '@kit.BasicServicesKit';

try {
  formObserver.getRunningFormInfos('com.example.ohos.formjsdemo').then((data: formInfo.RunningFormInfo[]) => {
    console.log(`formObserver getRunningFormInfos, data: ${JSON.stringify(data)}`);
  }).catch((error: BusinessError) => {
    console.error(`error, code: ${error.code}, message: ${error.message}`);
  });
} catch(error) {
  console.error(`catch error, code: ${(error as BusinessError).code}, message: ${(error as BusinessError).message}`);
}

getRunningFormInfos11+

getRunningFormInfos(isUnusedIncluded: boolean, hostBundleName?: string): Promise<Array<formInfo.RunningFormInfo>>

Obtains the information about all non-temporary widgets running on the device. This API uses a promise to return the result.

Required permissions: ohos.permission.OBSERVE_FORM_RUNNING

System capability: SystemCapability.Ability.Form

Parameters

NameTypeMandatoryDescription
isUnusedIncludedbooleanYesWhether an unused widget is included.
hostBundleNamestringNoName of the bundle that functions as the widget host. If a value is passed in, only the information about the non-temporary widgets that are running under the widget host is returned.
If no value is passed in, information about all running non-temporary widgets on the device is returned.

Return value

TypeDescription
Promise<Array<formInfo.RunningFormInfo>>Promise used to return the information about all non-temporary widgets.

Error codes

For details about the error codes, see Universal Error Codes and Form Error Codes.

IDError Message
201Permissions denied.
202The application is not a system application.
401Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types; 3.Parameter verification failed.
16500050IPC connection error.
16500060Service connection error.

Example

import { formInfo, formObserver } from '@kit.FormKit';
import { BusinessError } from '@kit.BasicServicesKit';

try {
  formObserver.getRunningFormInfos(true, 'com.example.ohos.formjsdemo').then((data: formInfo.RunningFormInfo[]) => {
    console.log(`formObserver getRunningFormInfos, data: ${JSON.stringify(data)}`);
  }).catch((error: BusinessError) => {
    console.error(`error, code: ${error.code}, message: ${error.message}`);
  });
} catch(error) {
  console.error(`catch error, code: ${(error as BusinessError).code}, message: ${(error as BusinessError).message}`);
}

getRunningFormInfosByFilter

getRunningFormInfosByFilter(formProviderFilter: formInfo.FormProviderFilter): Promise<Array<formInfo.RunningFormInfo>>

Obtains the information about widgets based on the widget provider. This API uses a promise to return the result.

Model restriction: This API can be used only in the stage model.

Required permissions: ohos.permission.OBSERVE_FORM_RUNNING

System capability: SystemCapability.Ability.Form

Parameters

NameTypeMandatoryDescription
formProviderFilterformInfo.FormProviderFilterYesInformation about the widget provider.

Return value

TypeDescription
Promise<Array<formInfo.RunningFormInfo>>Promise used to return an array of the widgets.

Error codes

For details about the error codes, see Universal Error Codes and Form Error Codes.

IDError Message
201Permissions denied.
202The application is not a system application.
401Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types; 3.Parameter verification failed.
16500050IPC connection error.
16500100Failed to obtain the configuration information.
16501000An internal functional error occurred.
import { formInfo, formObserver } from '@kit.FormKit';
import { BusinessError } from '@kit.BasicServicesKit';

let formInstanceFilter: formInfo.FormProviderFilter = {
  bundleName: "com.example.formprovide",
  abilityName: "EntryFormAbility",
  formName: "widget",
  moduleName: "entry"
}
try {
  formObserver.getRunningFormInfosByFilter(formInstanceFilter).then((data: formInfo.RunningFormInfo[]) => {
    console.info('formObserver getRunningFormInfosByFilter success, data:' + JSON.stringify(data));
  }).catch((error: BusinessError) => {
    console.error(`error, code: ${error.code}, message: ${error.message}`);
  });
} catch(error) {
  console.error(`catch error, code: ${(error as BusinessError).code}, message: ${(error as BusinessError).message}`);
}

getRunningFormInfosByFilter

getRunningFormInfosByFilter(formProviderFilter: formInfo.FormProviderFilter, callback: AsyncCallback<Array<formInfo.RunningFormInfo>>): void

Obtains the information about widgets based on the widget provider. This API uses an asynchronous callback to return the result.

Model restriction: This API can be used only in the stage model.

Required permissions: ohos.permission.OBSERVE_FORM_RUNNING

System capability: SystemCapability.Ability.Form

Parameters

NameTypeMandatoryDescription
formProviderFilterformInfo.FormProviderFilterYesInformation about the widget provider.
callbackAsyncCallback<Array<formInfo.RunningFormInfo>>YesCallback used to used to return an array of the widgets. If the widget information is obtained, error is undefined, and data is the information obtained. Otherwise, error is an error object.

Error codes

For details about the error codes, see Universal Error Codes and Form Error Codes.

IDError Message
201Permissions denied.
202The application is not a system application.
401Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types; 3.Parameter verification failed.
16500050IPC connection error.
16500100Failed to obtain the configuration information.
16501000An internal functional error occurred.

Example

import { formInfo, formObserver } from '@kit.FormKit';
import { BusinessError } from '@kit.BasicServicesKit';

let formInstanceFilter: formInfo.FormProviderFilter = {
  bundleName: "com.example.formprovide",
  abilityName: "EntryFormAbility",
  formName: "widget",
  moduleName: "entry"
}
try {
  formObserver.getRunningFormInfosByFilter(formInstanceFilter,(error: BusinessError, data: formInfo.RunningFormInfo[]) => {
    if (error) {
      console.error(`error, code: ${error.code}, message: ${error.message}`);
    } else {
      console.log(`formObserver getRunningFormInfosByFilter, data: ${JSON.stringify(data)}`);
    }
  });
} catch(error) {
  console.error(`catch error, code: ${(error as BusinessError).code}, message: ${(error as BusinessError).message}`);
}

getRunningFormInfoById

getRunningFormInfoById(formId: string): Promise<formInfo.RunningFormInfo>

Obtains the information about the widget based on the widget ID. This API uses a promise to return the result.

Model restriction: This API can be used only in the stage model.

Required permissions: ohos.permission.OBSERVE_FORM_RUNNING

System capability: SystemCapability.Ability.Form

Parameters

NameTypeMandatoryDescription
formIdstringYesWidget ID.

Return value

TypeDescription
Promise<formInfo.RunningFormInfo>Promise used to return the widget information.

Error codes

For details about the error codes, see Universal Error Codes and Form Error Codes.

IDError Message
201Permissions denied.
202The application is not a system application.
401Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types; 3.Parameter verification failed.
16500050IPC connection error.
16500100Failed to obtain the configuration information.
16501000An internal functional error occurred.

Example

import { formInfo, formObserver } from '@kit.FormKit';
import { BusinessError } from '@kit.BasicServicesKit';

let formId: string = '12400633174999288';
try {
  formObserver.getRunningFormInfoById(formId).then((data: formInfo.RunningFormInfo) => {
    console.info('formObserver getRunningFormInfoById success, data:' + JSON.stringify(data));
  }).catch((error: BusinessError) => {
    console.error(`error, code: ${error.code}, message: ${error.message}`);
  });
} catch(error) {
  console.error(`catch error, code: ${(error as BusinessError).code}, message: ${(error as BusinessError).message}`);
}

getRunningFormInfoById11+

getRunningFormInfoById(formId: string, isUnusedIncluded: boolean): Promise<formInfo.RunningFormInfo>

Obtains the information about the widget based on the widget ID. This API uses a promise to return the result.

Model restriction: This API can be used only in the stage model.

Required permissions: ohos.permission.OBSERVE_FORM_RUNNING

System capability: SystemCapability.Ability.Form

Parameters

NameTypeMandatoryDescription
formIdstringYesWidget ID.
isUnusedIncludedbooleanYesWhether an unused widget is included.

Return value

TypeDescription
Promise<formInfo.RunningFormInfo>Promise used to return the widget information.

Error codes

For details about the error codes, see Universal Error Codes and Form Error Codes.

IDError Message
201Permissions denied.
202The application is not a system application.
401Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types; 3.Parameter verification failed.
16500050IPC connection error.
16500100Failed to obtain the configuration information.
16501000An internal functional error occurred.

Example

import { formInfo, formObserver } from '@kit.FormKit';
import { BusinessError } from '@kit.BasicServicesKit';

let formId: string = '12400633174999288';
try {
  formObserver.getRunningFormInfoById(formId, true).then((data: formInfo.RunningFormInfo) => {
    console.info('formObserver getRunningFormInfoById success, data:' + JSON.stringify(data));
  }).catch((error: BusinessError) => {
    console.error(`error, code: ${error.code}, message: ${error.message}`);
  });
} catch(error) {
  console.error(`catch error, code: ${(error as BusinessError).code}, message: ${(error as BusinessError).message}`);
}

getRunningFormInfoById

getRunningFormInfoById(formId: string, callback: AsyncCallback<formInfo.RunningFormInfo>): void

Obtains the information about the widget based on the widget ID. This API uses an asynchronous callback to return the result.

Model restriction: This API can be used only in the stage model.

Required permissions: ohos.permission.OBSERVE_FORM_RUNNING

System capability: SystemCapability.Ability.Form

Parameters

NameTypeMandatoryDescription
formIdstringYesWidget ID.
callbackAsyncCallback<formInfo.RunningFormInfo>YesCallback used to used to return the widget information. If the widget information is obtained, error is undefined, and data is the information obtained. Otherwise, error is an error object.

Error codes

For details about the error codes, see Universal Error Codes and Form Error Codes.

IDError Message
201Permissions denied.
202The application is not a system application.
401Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types; 3.Parameter verification failed.
16500050IPC connection error.
16500100Failed to obtain the configuration information.
16501000An internal functional error occurred.

Example

import { formInfo, formObserver } from '@kit.FormKit';
import { BusinessError } from '@kit.BasicServicesKit';

let formId: string = '12400633174999288';
try {
  formObserver.getRunningFormInfoById(formId,(error: BusinessError, data: formInfo.RunningFormInfo) => {
    if (error) {
      console.error(`error, code: ${error.code}, message: ${error.message}`);
    } else {
      console.log(`formObserver getRunningFormInfoById, data: ${JSON.stringify(data)}`);
    }
  });
} catch(error) {
  console.error(`catch error, code: ${(error as BusinessError).code}, message: ${(error as BusinessError).message}`);
}

getRunningFormInfoById11+

getRunningFormInfoById(formId: string, isUnusedIncluded: boolean, callback: AsyncCallback<formInfo.RunningFormInfo>): void

Obtains the information about the widget based on the widget ID. This API uses an asynchronous callback to return the result.

Model restriction: This API can be used only in the stage model.

Required permissions: ohos.permission.OBSERVE_FORM_RUNNING

System capability: SystemCapability.Ability.Form

Parameters

NameTypeMandatoryDescription
formIdstringYesWidget ID.
isUnusedIncludedbooleanYesWhether an unused widget is included.
callbackAsyncCallback<formInfo.RunningFormInfo>YesCallback used to used to return the widget information. If the widget information is obtained, error is undefined, and data is the information obtained. Otherwise, error is an error object.

Error codes

For details about the error codes, see Universal Error Codes and Form Error Codes.

IDError Message
201Permissions denied.
202The application is not a system application.
401Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types; 3.Parameter verification failed.
16500050IPC connection error.
16500100Failed to obtain the configuration information.
16501000An internal functional error occurred.

Example

import { formInfo, formObserver } from '@kit.FormKit';
import { BusinessError } from '@kit.BasicServicesKit';

let formId: string = '12400633174999288';
try {
  formObserver.getRunningFormInfoById(formId, true, (error: BusinessError, data: formInfo.RunningFormInfo) => {
    if (error) {
      console.error(`error, code: ${error.code}, message: ${error.message}`);
    } else {
      console.log(`formObserver getRunningFormInfoById, data: ${JSON.stringify(data)}`);
    }
  });
} catch(error) {
  console.error(`catch error, code: ${(error as BusinessError).code}, message: ${(error as BusinessError).message}`);
}

on('router')11+

on(type: 'router', observerCallback: Callback<formInfo.RunningFormInfo>): void

Subscribes to widget router events. This API uses an asynchronous callback to return the information of the widget that triggers the router event.

Required permissions: ohos.permission.OBSERVE_FORM_RUNNING

System capability: SystemCapability.Ability.Form

Parameters

NameTypeMandatoryDescription
typestringYesEvent type. The value 'router' indicates a widget router event.
observerCallbackCallback<formInfo.RunningFormInfo>YesCallback used to return the widget information.

Error codes

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

IDError Message
202The application is not a system application.
401Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types; 3.Parameter verification failed.

Example

import { formInfo, formObserver } from '@kit.FormKit';

let callback = (data: formInfo.RunningFormInfo) => {
  console.log('Router event listening in registered form.' + JSON.stringify(data));
};
formObserver.on('router', callback);

on('router')11+

on(type: 'router', hostBundleName: string, observerCallback: Callback<formInfo.RunningFormInfo>): void

Subscribes to widget router events for a given bundle that functions as the widget host. This API uses an asynchronous callback to return the information of the widget that triggers the router event.

Required permissions: ohos.permission.OBSERVE_FORM_RUNNING

System capability: SystemCapability.Ability.Form

Parameters

NameTypeMandatoryDescription
typestringYesEvent type. The value 'router' indicates a widget router event.
hostBundleNamestringYesName of the bundle that functions as the widget host. If no value is passed in, widget router events of all widget hosts are subscribed to.
observerCallbackCallback<formInfo.RunningFormInfo>YesCallback used to return the widget information.

Error codes

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

IDError Message
202The application is not a system application.
401Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types; 3.Parameter verification failed.

Example

import { formInfo, formObserver } from '@kit.FormKit';

let hostBundleName: string = 'ohos.samples.FormApplication';
let callback = (data: formInfo.RunningFormInfo) => {
  console.log('Router event listening in registered form.' + JSON.stringify(data));
};
formObserver.on('router', hostBundleName, callback);

off('router')11+

off(type: "router", hostBundleName?: string, observerCallback?: Callback<formInfo.RunningFormInfo>): void

Unsubscribes from widget router events. This API uses an asynchronous callback to return the information of the widget that triggers the router event.

Required permissions: ohos.permission.OBSERVE_FORM_RUNNING

System capability: SystemCapability.Ability.Form

Parameters

NameTypeMandatoryDescription
typestringYesEvent type. The value 'router' indicates a widget router event.
hostBundleNamestringNoName of the bundle that functions as the widget host.
To cancel the subscription for a given bundle name, this parameter must be set to the same value as bundleName in on('router').
If no value is passed in, the subscriptions for all the widget hosts are canceled.
observerCallbackCallback<formInfo.RunningFormInfo>NoCallback used to return the widget information. If no value is passed in, all the subscriptions to the specified event are canceled.
To cancel the subscription with a given callback, this parameter must be set to the same value as callback in on('router').

Error codes

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

IDError Message
202The application is not a system application.
401Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types; 3.Parameter verification failed.

Example

import { formInfo, formObserver } from '@kit.FormKit';

let hostBundleName: string = 'ohos.samples.FormApplication';
let callback = (data: formInfo.RunningFormInfo) => {
  console.log('Unregister form router event Listening.' + JSON.stringify(data));
};
formObserver.off('router', hostBundleName, callback);

on('message')11+

on(type: 'message', observerCallback: Callback<formInfo.RunningFormInfo>): void

Subscribes to widget message events. This API uses an asynchronous callback to return the information of the widget that triggers the message event.

Required permissions: ohos.permission.OBSERVE_FORM_RUNNING

System capability: SystemCapability.Ability.Form

Parameters

NameTypeMandatoryDescription
typestringYesEvent type. This value 'message' indicates a widget message event.
observerCallbackCallback<formInfo.RunningFormInfo>YesCallback used to return the widget information.

Error codes

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

IDError Message
202The application is not a system application.
401Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types; 3.Parameter verification failed.

Example

import { formInfo, formObserver } from '@kit.FormKit';

let callback = (data: formInfo.RunningFormInfo) => {
  console.log('Message event listening in registered form.' + JSON.stringify(data));
};
formObserver.on('message', callback);

on('message')11+

on(type: 'message', hostBundleName: string, observerCallback: Callback<formInfo.RunningFormInfo>): void

Subscribes to widget message events for a given bundle that functions as the widget host. This API uses an asynchronous callback to return the information of the widget that triggers the message event.

Required permissions: ohos.permission.OBSERVE_FORM_RUNNING

System capability: SystemCapability.Ability.Form

Parameters

NameTypeMandatoryDescription
typestringYesEvent type. This value 'message' indicates a widget message event.
hostBundleNamestringYesName of the bundle that functions as the widget host. If no value is passed in, widget message events of all widget hosts are subscribed to.
observerCallbackCallback<formInfo.RunningFormInfo>YesCallback used to return the widget information.

Error codes

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

IDError Message
202The application is not a system application.
401Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types; 3.Parameter verification failed.

Example

import { formInfo, formObserver } from '@kit.FormKit';

let hostBundleName: string = 'ohos.samples.FormApplication';
let callback = (data: formInfo.RunningFormInfo) => {
  console.log('Message event listening in registered form.' + JSON.stringify(data));
};
formObserver.on('message', hostBundleName, callback);

off('message')11+

off(type: "message", hostBundleName?: string, observerCallback?: Callback<formInfo.RunningFormInfo>): void

Unsubscribes from widget message events. This API uses an asynchronous callback to return the information of the widget that triggers the message event.

Required permissions: ohos.permission.OBSERVE_FORM_RUNNING

System capability: SystemCapability.Ability.Form

Parameters

NameTypeMandatoryDescription
typestringYesEvent type. This value 'message' indicates a widget message event.
hostBundleNamestringNoName of the bundle that functions as the widget host.
To cancel the subscription for a given bundle name, this parameter must be set to the same value as bundleName in on('message').
If no value is passed in, the subscriptions for all the widget hosts are canceled.
observerCallbackCallback<formInfo.RunningFormInfo>NoCallback used to return the widget information. If no value is passed in, all the subscriptions to the specified event are canceled.
To cancel the subscription with a given callback, this parameter must be set to the same value as callback in on('message').

Error codes

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

IDError Message
202The application is not a system application.
401Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types; 3.Parameter verification failed.

Example

import { formInfo, formObserver } from '@kit.FormKit';

let hostBundleName: string = 'ohos.samples.FormApplication';
let callback = (data: formInfo.RunningFormInfo) => {
  console.log('Unregister form Message event Listening.' + JSON.stringify(data));
};
formObserver.off('message', hostBundleName, callback);

on('call')11+

on(type: 'call', observerCallback: Callback<formInfo.RunningFormInfo>): void

Subscribes to widget call events. This API uses an asynchronous callback to return the information of the widget that triggers the call event.

Required permissions: ohos.permission.OBSERVE_FORM_RUNNING

System capability: SystemCapability.Ability.Form

Parameters

NameTypeMandatoryDescription
typestringYesEvent type. This value 'call' indicates a widget call event.
observerCallbackCallback<formInfo.RunningFormInfo>YesCallback used to return the widget information.

Error codes

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

IDError Message
202The application is not a system application.
401Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types; 3.Parameter verification failed.

Example

import { formInfo, formObserver } from '@kit.FormKit';

let callback = (data: formInfo.RunningFormInfo) => {
  console.log('Call event listening in registered form.' + JSON.stringify(data));
};
formObserver.on('call', callback);

on('call')11+

on(type: 'call', hostBundleName: string, observerCallback: Callback<formInfo.RunningFormInfo>): void

Subscribes to widget call events for a given bundle that functions as the widget host. This API uses an asynchronous callback to return the information of the widget that triggers the call event.

Required permissions: ohos.permission.OBSERVE_FORM_RUNNING

System capability: SystemCapability.Ability.Form

Parameters

NameTypeMandatoryDescription
typestringYesEvent type. This value 'call' indicates a widget call event.
hostBundleNamestringYesName of the bundle that functions as the widget host. If no value is passed in, widget call events of all widget hosts are subscribed to.
observerCallbackCallback<formInfo.RunningFormInfo>YesCallback used to return the widget information.

Error codes

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

IDError Message
202The application is not a system application.
401Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types; 3.Parameter verification failed.

Example

import { formInfo, formObserver } from '@kit.FormKit';

let hostBundleName: string = 'ohos.samples.FormApplication';
let callback = (data: formInfo.RunningFormInfo) => {
  console.log('Call event listening in registered form.' + JSON.stringify(data));
};
formObserver.on('call', hostBundleName, callback);

off('call')11+

off(type: "call", hostBundleName?: string, observerCallback?: Callback<formInfo.RunningFormInfo>): void

Unsubscribes from widget call events. This API uses an asynchronous callback to return the information of the widget that triggers the call event.

Required permissions: ohos.permission.OBSERVE_FORM_RUNNING

System capability: SystemCapability.Ability.Form

Parameters

NameTypeMandatoryDescription
typestringYesEvent type. This value 'call' indicates a widget call event.
hostBundleNamestringNoName of the bundle that functions as the widget host.
To cancel the subscription for a given bundle name, this parameter must be set to the same value as bundleName in on('message').
If no value is passed in, the subscriptions for all the widget hosts are canceled.
observerCallbackCallback<formInfo.RunningFormInfo>NoCallback used to return the widget information. If no value is passed in, all the subscriptions to the specified event are canceled.
To cancel the subscription with a given callback, this parameter must be set to the same value as callback in on('call').

Error codes

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

IDError Message
202The application is not a system application.
401Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types; 3.Parameter verification failed.

Example

import { formInfo, formObserver } from '@kit.FormKit';

let hostBundleName: string = 'ohos.samples.FormApplication';
let callback = (data: formInfo.RunningFormInfo) => {
  console.log('Unregister form Call event Listening.' + JSON.stringify(data));
};
formObserver.off('call', hostBundleName, callback);

你可能感兴趣的鸿蒙文章

harmony 鸿蒙Form Kit

harmony 鸿蒙Form Error Codes

harmony 鸿蒙@ohos.app.form.formAgent (FormAgent) (System API)

harmony 鸿蒙@ohos.app.form.formBindingData (formBindingData)

harmony 鸿蒙@ohos.app.form.FormEditExtensionAbility (FormEditExtensionAbility)

harmony 鸿蒙@ohos.app.form.FormExtensionAbility (FormExtensionAbility) (System API)

harmony 鸿蒙@ohos.app.form.FormExtensionAbility (FormExtensionAbility)

harmony 鸿蒙@ohos.app.form.formHost (formHost) (System API)

harmony 鸿蒙@ohos.app.form.formInfo (formInfo) (System API)

harmony 鸿蒙@ohos.app.form.formInfo (formInfo)

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