This section provides the development guidelines for passive update. For details about the update process, see Passive Update.
Form Kit provides the following methods for interval-based update:
Setting the update interval: The widget content will be automatically updated at the specified interval by calling onUpdateForm. The interval is specified by the updateDuration field in the form_config.json file. For example, the value of the updateDuration field can be set to 2, indicating that the update interval is 1 hour (2 periods of 30 minutes).
{
"forms": [
{
"name": "UpdateDuration",
"description": "$string:widget_updateduration_desc",
"src": "./ets/updateduration/pages/UpdateDurationCard.ets",
"uiSyntax": "arkts",
"window": {
"designWidth": 720,
"autoDesignWidth": true
},
"isDefault": true,
"updateEnabled": true,
"scheduledUpdateTime": "10:30",
"updateDuration": 2,
"defaultDimension": "2*2",
"supportDimensions": [
"2*2"
]
}
]
}
To use interval-based update, set the updateEnabled field to true in the form_config.json file.
Setting the next update time: The widget will be updated at the next specified time, which is specified by calling setFormNextRefreshTime. The minimum refresh interval is 5 minutes. For example, you can set the widget to refresh 5 minutes after the API is called.
// entry/src/main/ets/updatebytimeformability/UpdateByTimeFormAbility.ts
import { formBindingData, FormExtensionAbility, formInfo, formProvider } from '@kit.FormKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { BusinessError } from '@kit.BasicServicesKit';
import { Want } from '@kit.AbilityKit';
const TAG: string = 'UpdateByTimeFormAbility';
const FIVE_MINUTE: number = 5;
const DOMAIN_NUMBER: number = 0xFF00;
export default class UpdateByTimeFormAbility extends FormExtensionAbility {
onAddForm(want: Want): formBindingData.FormBindingData {
// Called when the widget host creates a widget. The widget data binding class is returned.
let formData: Record<string, Object | string> = {};
return formBindingData.createFormBindingData(formData);
}
// ...
onFormEvent(formId: string, message: string): void {
// Called when the message event of the postCardAction API of the widget provider is triggered.
hilog.info(DOMAIN_NUMBER, TAG, `FormAbility onFormEvent, formId = ${formId}, message: ${JSON.stringify(message)}`);
try {
// Configure the widget to update in 5 minutes.
formProvider.setFormNextRefreshTime(formId, FIVE_MINUTE, (err: BusinessError) => {
if (err) {
hilog.error(DOMAIN_NUMBER, TAG,
`Failed to setFormNextRefreshTime. Code: ${err.code}, message: ${err.message}`);
return;
} else {
hilog.info(DOMAIN_NUMBER, TAG, 'Succeeded in setFormNextRefreshTime.');
}
});
} catch (err) {
hilog.error(DOMAIN_NUMBER, TAG,
`Failed to setFormNextRefreshTime. Code: ${(err as BusinessError).code},
message: ${(err as BusinessError).message}`);
}
}
onAcquireFormState(want: Want): formInfo.FormState {
// Called when the widget host queries the widget state. The initial state is returned by default.
return formInfo.FormState.READY;
}
} After an interval-based or next update is triggered, the system calls the onUpdateForm lifecycle callback of FormExtensionAbility. In this callback, you can use the updateForm API to refresh the widget content. For details about the onUpdateForm lifecycle callback, see Managing ArkTS Widget Lifecycle.
Constraints
Form Kit provides the following methods for time-specific widget update:
Setting a single update time: The widget content will be automatically updated at a designated time every day. This time is specified by the scheduledUpdateTime field in the form_config.json file. For example, you can configure the widget to update at 10:30 every day.
{
"forms": [
{
"name": "ScheduledUpdateTime",
"description": "$string:widget_scheduledUpdateTime_desc",
"src": "./ets/scheduledupdatetime/pages/ScheduledUpdateTimeCard.ets",
"uiSyntax": "arkts",
"window": {
"designWidth": 720,
"autoDesignWidth": true
},
"isDefault": true,
"updateEnabled": true,
"scheduledUpdateTime": "10:30",
"updateDuration": 0,
"defaultDimension": "2*2",
"supportDimensions": [
"2*2"
]
}
]
} Setting multiple update times: The widget content will be automatically updated at several specific times every day. These times are specified by the multiScheduledUpdateTime field in the form_config.json file. For example, you can configure the widget to update at 11:30 and 16:30 every day.
{
"forms": [
{
"name": "ScheduledUpdateTime",
"description": "$string:widget_scheduledUpdateTime_desc",
"src": "./ets/scheduledupdatetime/pages/ScheduledUpdateTimeCard.ets",
"uiSyntax": "arkts",
"window": {
"designWidth": 720,
"autoDesignWidth": true
},
"isDefault": true,
"updateEnabled": true,
"scheduledUpdateTime": "10:30",
"multiScheduledUpdateTime": "11:30,16:30",
"updateDuration": 0,
"defaultDimension": "2*2",
"supportDimensions": [
"2*2"
]
}
]
} After a time-specific update is triggered, the system calls the onUpdateForm lifecycle callback of FormExtensionAbility. In this callback, you can use the updateForm API to refresh the widget content. For details about the onUpdateForm lifecycle callback, see Managing ArkTS Widget Lifecycle.
Constraints
Form Kit provides the following methods for conditional updates:
A widget update is triggered when the device transitions from a no-network state to a connected state. However, an update is not triggered when a user switches between networks (for example, switching between different Wi-Fi networks or between Wi-Fi and cellular data) or goes from Internet connection to no Internet connection.
To minimize the frequency of widget process initiations during frequent network on/off scenarios, a no-network condition is determined after the network has been continuously unavailable for 10 minutes. Upon reconnection, a network-based update is then triggered.
{
"forms": [
{
"name": "UpdateDuration",
"description": "$string:widget_updateduration_desc",
"src": "./ets/updateduration/pages/UpdateDurationCard.ets",
"uiSyntax": "arkts",
"window": {
"designWidth": 720,
"autoDesignWidth": true
},
"isDefault": true,
"updateEnabled": true,
"scheduledUpdateTime": "10:30",
"updateDuration": 2,
"defaultDimension": "2*2",
"supportDimensions": [
"2*2"
],
"conditionUpdate": [
"network"
]
}
]
}