public class BackgroundTaskManager extends Object
After an application runs in the background for a period of time, it will enter the suspended state and cannot
execute tasks. If the application needs to continue unfinished tasks in the background, it can send a request to
BackgroundTaskManager to delay transition to the suspended state. This class provides the methods to request
and cancel delayed transition to the suspended state and to query the remaining time before an application enters
the suspended state.
SystemCapability.ResourceSchedule#BACKGROUND_TASK_MANAGER| 构造器 | 说明 |
|---|---|
BackgroundTaskManager(Context context) |
A constructor used to create a
BackgroundTaskManager instance for initializing background task
management objects based on the specified application context. |
| 限定符和类型 | 方法 | 说明 |
|---|---|---|
void |
cancelSuspendDelay(int requestId) |
Cancels delayed transition to the suspended state.
|
int |
getRemainingDelayTime(int requestId) |
Obtains the remaining time before an application enters the suspended state.
|
DelaySuspendInfo |
requestSuspendDelay(String reason,
ExpiredCallback cb) |
Requests delayed transition to the suspended state.
|
public BackgroundTaskManager(Context context)
BackgroundTaskManager instance for initializing background task
management objects based on the specified application context.context - Indicates the application context. This parameter cannot be null.public DelaySuspendInfo requestSuspendDelay(String reason, ExpiredCallback cb) throws RemoteException
If an application needs to execute tasks in the background, it must send a request for delayed transition to
the suspended state and describe the reason. When the requested delay duration times out,
ExpiredCallback will be called to notify the application.
reason - Indicates the reason for delayed transition to the suspended state. The parameter cannot be null.cb - Indicates the callback for timeout of the delay duration. The parameter cannot be null.DelaySuspendInfo carrying the result of requesting delayed transition to the
suspended state.RemoteException - Throws this exception when a remote communication error occurs. In this case, you are
advised to call this method again.public void cancelSuspendDelay(int requestId)
throws RemoteException
If an application finishes a task before the delay duration times out, the application can request to terminate the delayed transition to the suspended state.
requestId - Uniquely identifies the request for delayed transition to the suspended state. You can call
DelaySuspendInfo#getRequestId() to obtain requestId. Note that an application can query only
requestId of the requests it initiated.RemoteException - Throws this exception when a remote communication error occurs. In this case, you are
advised to call this method again.public int getRemainingDelayTime(int requestId)
throws RemoteException
requestId - Uniquely identifies the request for delayed transition to the suspended state. You can call
DelaySuspendInfo#getRequestId() to obtain requestId. Note that an application can query only
requestId of the requests it initiated.RemoteException - Throws this exception when a remote communication error occurs. In this case, you are
advised to call this method again.