ILifecycle, Contextpublic abstract class WorkScheduler extends Ability
You must implement onWorkStart(ohos.workscheduler.WorkInfo) and onWorkStop(ohos.workscheduler.WorkInfo) for each work. This ensures that the service
logic can be successfully implemented if the triggering condition is met after a work is started by
WorkSchedulerService. Once WorkInfo is defined, you can manage the work status using
methods such as stopWork(ohos.workscheduler.WorkInfo) and stopAndCancelWork(ohos.workscheduler.WorkInfo).
SystemCapability.ResourceSchedule#WORK_SCHEDULERAbility.LayoutParamsHelper, Ability.WindowAttributeHelperFORM_INVISIBLE, FORM_VISIBLECONTEXT_IGNORE_SECURITY, CONTEXT_INCLUDE_CODE, CONTEXT_RESOUCE_ONLY, CONTEXT_RESTRICTED, MODE_APPEND, MODE_PRIVATE| 构造器 | 说明 |
|---|---|
WorkScheduler() |
| 限定符和类型 | 方法 | 说明 |
|---|---|---|
static WorkInfo |
getWorkStatus(int workID) |
Obtains the latest status of a work.
|
static boolean |
isLastWorkTimeOut(int workId) |
Checks whether the last execution of the specified piece of work is a timeout operation.
|
static List<WorkInfo> |
obtainAllWorks() |
Obtains all pieces of work associated with the current application.
|
abstract void |
onWorkStart(WorkInfo work) |
Implements the part of service logic upon startup of a work.
|
abstract void |
onWorkStop(WorkInfo work) |
Implements the part of service logic upon stopping of a work.
|
static boolean |
startWork(WorkInfo work,
boolean startNow) |
Notifies
WorkSchedulerService to add a work to the execution queue. |
static boolean |
stopAndCancelWork(WorkInfo work) |
Notifies
WorkSchedulerService that a work can be stopped and canceled. |
static boolean |
stopAndClearWorks() |
Stops and cancels all pieces of work associated with the current application.
|
static boolean |
stopWork(WorkInfo work) |
Notifies
WorkSchedulerService that a work can be stopped. |
addActionRoute, batchInsert, call, cancelBackgroundRunning, connectAbility, continueAbility, continueAbility, continueAbilityReversibly, continueAbilityReversibly, createPipeFile, delete, denormalizeUri, disconnectAbility, dispatchAccessibilityEventInfo, executeBatch, executeOperation, findComponentById, getAbilityName, getAbilityPackage, getAdmissionTransitionConcurEnabled, getAdmissionTransitionObject, getAVController, getBackTransitionConcurEnabled, getBackTransitionObject, getContinuationRegisterManager, getContinuationState, getCurrentFocus, getFileTypes, getIntent, getLayoutParams, getLifecycle, getOriginalDeviceId, getQuitTransitionObject, getReadmissionTransitionObject, getSharedItemAdmissionTransitionObject, getSharedItemBackTransitionObject, getSharedItemQuitTransitionObject, getSharedItemReadmissionTransitionObject, getState, getType, getVolumeTypeAdjustedByKey, getWindow, hasWindowFocus, insert, isTemporary, isTerminating, keepBackgroundRunning, normalizeUri, onAbilityResult, onAcquireFormState, onActive, onBackground, onBackPressed, onCastTempForm, onCommand, onCommand, onConfigurationUpdated, onCreateForm, onCreateForm, onDeleteForm, onDeleteForm, onEventDispatch, onEventNotify, onForeground, onHotkeyTriggered, onInactive, onInitialized, onKeyDown, onKeyPressAndHold, onKeyUp, onLeaveForeground, onMemoryLevel, onMissionDeleted, onNewDescription, onNewIntent, onOrientationChanged, onPostActive, onPostStart, onReconnect, onRequestPermissionsFromUserResult, onRestoreAbilityState, onSaveAbilityState, onSetCaller, onStart, onStop, onStoreDataWhenConfigChange, onTopActiveAbilityChanged, onTouchEvent, onTriggerFormEvent, onTriggerFormEvent, onUnloadForm, onUpdateForm, onUpdateForm, onWindowFocusChanged, openFile, openRawFile, parsePredictionArgsReference, parseValuesBucketReference, postTask, query, reload, reverseContinueAbility, setAbilitySliceAnimator, setAdmissionTransitionConcurEnabled, setAdmissionTransitionObject, setAVController, setBackTransitionConcurEnabled, setBackTransitionObject, setFormNextRefreshTime, setIntent, setIsAmbientMode, setLayoutParams, setMainRoute, setQuitTransitionObject, setReadmissionTransitionObject, setResult, setSharedItemAdmissionTransitionObject, setSharedItemBackTransitionObject, setSharedItemQuitTransitionObject, setSharedItemReadmissionTransitionObject, setSwipeToDismiss, setUIContent, setUIContent, setVolumeTypeAdjustedByKey, setWindowBackgroundColor, shutdown, startAbility, startAbility, startAbility, startAbility, startAbilityForResult, startAbilityForResult, startAssistAbility, stopAbility, stopAssistAbility, supportHighPerformanceUI, terminateAbility, update, updateForm, updateForm, updateForm, updateFormattachBaseContext, canRequestPermission, compelVerifyCallerPermission, compelVerifyCallerUriPermission, compelVerifyPermission, compelVerifyPermission, compelVerifyUriPermission, compelVerifyUriPermission, compelVerifyUriPermission, createBundleContext, createParallelTaskDispatcher, createSerialTaskDispatcher, deleteFile, displayUnlockMissionMessage, getAbilityInfo, getAbilityManager, getAbilityPackageContext, getApplicationContext, getApplicationInfo, getAppType, getBundleCodePath, getBundleManager, getBundleName, getBundleResourcePath, getCacheDir, getCaller, getCallingAbility, getCallingBundle, getClassloader, getCodeCacheDir, getColor, getColorMode, getContext, getDatabaseDir, getDataDir, getDir, getDisplayOrientation, getDistributedDir, getElementName, getExternalCacheDir, getExternalCacheDirs, getExternalFilesDir, getExternalFilesDirs, getExternalMediaDirs, getFilesDir, getGlobalTaskDispatcher, getHapModuleInfo, getIntArray, getLastStoredDataWhenConfigChanged, getLocalClassName, getMainTaskDispatcher, getNoBackupFilesDir, getPattern, getPreferencesDir, getProcessInfo, getProcessName, getResourceManager, getResourceManager, getString, getString, getStringArray, getTheme, getThemeId, getUITaskDispatcher, init, isCredentialEncryptedStorage, isDeviceEncryptedStorage, isUpdatingConfigurations, lockMission, printDrawnCompleted, requestPermissionsFromUser, restart, setColorMode, setDisplayOrientation, setPattern, setShowOnLockScreen, setTheme, setTransitionAnimation, setWakeUpScreen, startAbilities, startAbility, startAbility, startAbility, switchToCredentialEncryptedStorageContext, switchToDeviceEncryptedStorageContext, terminateAbility, terminateAbilityResult, unlockMission, verifyCallingOrSelfPermission, verifyCallingPermission, verifyPermission, verifySelfPermissionpublic abstract void onWorkStart(WorkInfo work)
WorkSchedulerService calls this method to execute the work defined by WorkInfo.
Note: A work runs in the background after being started and therefore it must comply with the the rules of
BackgroundTaskManager. Once WorkSchedulerService calls this method to start a work,
BackgroundTaskManager marks the work as a background task, collects statistics on its running time,
and keeps checking whether the work meets the requirement for running in the background. For more details,
see BackgroundTaskManager. And you should acquire an running lock for yourself, if you don't want be halt
by system suspend when your work is running
work - Indicates the work to be started by WorkSchedulerService. Ensure that WorkInfo of
the work is valid.public abstract void onWorkStop(WorkInfo work)
WorkSchedulerService calls this method to notify the work to stop running.
Note: WorkSchedulerService will call this method when detecting that the preset triggering condition
of the work is no longer met. The work should end the service logic being executed and save key information in
time to prevent data loss.
work - Indicates the work to be stopped by WorkSchedulerService. Ensure that WorkInfo of
the work is valid.public static final boolean startWork(WorkInfo work, boolean startNow) throws RemoteException
WorkSchedulerService to add a work to the execution queue.
Note: A work can be executed only when it meets the preset triggering condition and complies with the rules
of BackgroundTaskManager.
work - Indicates the work to be added to the execution queue. Ensure that the specified value is valid.
Otherwise, a failure occurs when calling this method.startNow - Indicates execute the work immediately. Note: A work can be executed immediately if the
associated application is running in the foreground. Otherwise, it must meet the preset
triggering condition and comply with the rules of BackgroundTaskManager.true if the work is successfully added to the execution queue; returns false
otherwise.RemoteException - Throws this exception if the connection to WorkSchedulerService fails.public static final boolean stopWork(WorkInfo work) throws RemoteException
WorkSchedulerService that a work can be stopped.
Note: When this method is called, WorkSchedulerService removes the work from the execution queue and
notifies BackgroundTaskManager that the work can be stopped. At this time, the wakeup lock applied by
WorkSchedulerService will be removed.
work - Indicates the work that can be stopped. Ensure that the specified value is valid. Otherwise, a
failure occurs when calling the method.true if the notification is successful; returns false otherwise.RemoteException - Throws this exception if the connection to WorkSchedulerService fails.public static final boolean stopAndCancelWork(WorkInfo work) throws RemoteException
WorkSchedulerService that a work can be stopped and canceled.
Note: When this method is called, WorkSchedulerService stops and cancels the work. To start the work,
you need to explicitly call startWork(ohos.workscheduler.WorkInfo, boolean) again.
work - Indicates the work to be stopped and canceled. Ensure that the specified value is valid. Otherwise,
a failure occurs when calling the method.true if the notification is successful; returns false otherwise.RemoteException - Throws this exception if the connection to WorkSchedulerService fails.public static final WorkInfo getWorkStatus(int workID) throws RemoteException
workID - Indicates the ID of the work. Ensure that the specified value is valid.WorkSchedulerService if the specified workID
is valid; returns null otherwise.RemoteException - Throws this exception if the connection to WorkSchedulerService fails.public static final List<WorkInfo> obtainAllWorks() throws RemoteException
RemoteException - Throws this exception if the connection to the WorkScheduler service fails.public static final boolean stopAndClearWorks()
throws RemoteException
true if all pieces of work are stopped and canceled successfully;
returns false otherwise.RemoteException - Throws this exception if the connection to the WorkScheduler service fails.public static final boolean isLastWorkTimeOut(int workId)
throws RemoteException
workId - Identifies the piece of work to check.true if the last execution of the specified piece of work is a timeout operation;
returns false otherwise.RemoteException - Throws this exception if the connection to the WorkScheduler service fails.