从应用中触发后台任务

了解如何使用 ApplicationTrigger 从应用中激活后台任务。

有关如何创建应用程序触发器的示例,请参阅此示例

本主题假定你有一个你想要从应用程序激活的后台任务。 如果你还没有后台任务,BackgroundActivity.cs 中有一个示例后台任务。 或者,请按照创建和注册进程外后台任务中的步骤创建一个任务。

为什么使用应用程序触发器

使用 ApplicationTrigger 在与前台应用不同的进程中运行代码。 如果你的应用有工作要在后台完成,则 ApplicationTrigger 是适合的,即使用户关闭前台应用也是如此。 如果应用关闭时应暂停后台工作,或应该将后台工作与前台进程状态绑定,则应使用扩展执行

创建应用程序触发器

创建新的 ApplicationTrigger。 你可以像下面的代码段一样,将它存储在一个字段中。 这是为方便起见,这样我们稍后在想要对触发器发送信号时就不必创建新实例。 但你可以使用任何 ApplicationTrigger 实例来向触发器发送信号。

// _AppTrigger is an ApplicationTrigger field defined at a scope that will keep it alive
// as long as you need to trigger the background task.
// Or, you could create a new ApplicationTrigger instance and use that when you want to
// trigger the background task.
_AppTrigger = new ApplicationTrigger();
// _AppTrigger is an ApplicationTrigger field defined at a scope that will keep it alive
// as long as you need to trigger the background task.
// Or, you could create a new ApplicationTrigger instance and use that when you want to
// trigger the background task.
Windows::ApplicationModel::Background::ApplicationTrigger _AppTrigger;
// _AppTrigger is an ApplicationTrigger field defined at a scope that will keep it alive
// as long as you need to trigger the background task.
// Or, you could create a new ApplicationTrigger instance and use that when you want to
// trigger the background task.
ApplicationTrigger ^ _AppTrigger = ref new ApplicationTrigger();

(可选)添加条件

你可以创建一个后台任务条件以控制任务何时运行。 条件会阻止后台任务运行,直到条件满足为止。 有关详细信息,请参阅设置后台任务的运行条件

在此示例中,条件设置为“InternetAvailable”,以便在触发之后,仅在 Internet 访问可用时运行任务。 有关可能条件的列表,请参阅 SystemConditionType

SystemCondition internetCondition = new SystemCondition(SystemConditionType.InternetAvailable);
Windows::ApplicationModel::Background::SystemCondition internetCondition{
    Windows::ApplicationModel::Background::SystemConditionType::InternetAvailable };
SystemCondition ^ internetCondition = ref new SystemCondition(SystemConditionType::InternetAvailable)

有关条件和后台触发器类型的更多深入信息,请参阅使用后台任务支持应用

调用 RequestAccessAsync()

注册 ApplicationTrigger 后台任务前,应调用 RequestAccessAsync 以确定用户允许的后台活动级别,因为用户可能为你的应用禁用了后台活动。 有关用户可用来控制后台活动设置的方法的详细信息,请参阅优化后台活动

var requestStatus = await Windows.ApplicationModel.Background.BackgroundExecutionManager.RequestAccessAsync();
if (requestStatus != BackgroundAccessStatus.AlwaysAllowed)
{
   // Depending on the value of requestStatus, provide an appropriate response
   // such as notifying the user which functionality won't work as expected
}

注册后台任务

通过调用后台任务注册函数注册后台任务。 有关注册后台任务的详细信息以及下面示例代码中的 RegisterBackgroundTask() 方法定义,请参阅注册后台任务

如果你正在考虑使用应用程序触发器延长前台进程的生命周期,请考虑使用扩展执行。 应用程序触发器用于创建单独托管的进程以便执行工作。 下面代码片段注册进程外后台触发器。

string entryPoint = "Tasks.ExampleBackgroundTaskClass";
string taskName   = "Example application trigger";

BackgroundTaskRegistration task = RegisterBackgroundTask(entryPoint, taskName, appTrigger, internetCondition);
std::wstring entryPoint{ L"Tasks.ExampleBackgroundTaskClass" };
std::wstring taskName{ L"Example application trigger" };

Windows::ApplicationModel::Background::BackgroundTaskRegistration task{
    RegisterBackgroundTask(entryPoint, taskName, appTrigger, internetCondition) };
String ^ entryPoint = "Tasks.ExampleBackgroundTaskClass";
String ^ taskName   = "Example application trigger";

BackgroundTaskRegistration ^ task = RegisterBackgroundTask(entryPoint, taskName, appTrigger, internetCondition);

后台任务注册参数在注册时验证。 如果有任何注册参数无效,则会返回一个错误。 确保你的应用能够流畅地处理后台任务注册失败的情况,否则,如果你的应用依赖于在尝试注册任务后具备有效注册对象,则它可能会崩溃。

触发后台任务

触发后台任务之前,请使用 BackgroundTaskRegistration 来验证注册了后台任务。 在应用启动时就是验证所有后台任务都已注册的好时机。

通过调用 ApplicationTrigger.RequestAsync 触发后台任务。 任何 ApplicationTrigger 实例都可以。

请注意,ApplicationTrigger.RequestAsync 不能从后台任务本身调用,或应用处于后台运行状态时也不能调用(请参阅 应用生命周期 了解有关应用程序状态的详细信息)。 如果用户已设置了阻止应用执行后台活动的能量或隐私策略,它可能会返回 DisabledByPolicy。 同样,只有一个 AppTrigger 可以运行一次。 如果你尝试运行 AppTrigger 时已有另一个触发器已经在运行,则将返回函数 CurrentlyRunning

var result = await _AppTrigger.RequestAsync();

管理后台任务的资源

使用 BackgroundExecutionManager.RequestAccessAsync 确定用户是否已决定应限制你应用的后台活动。 注意电池使用情况,并且仅当有必要完成用户想要执行的操作时再在后台运行应用。 有关用户可用来控制后台活动设置的方法的详细信息,请参阅优化后台活动

  • 内存:调整应用内存和能耗使用情况是确保操作系统允许后台任务运行的关键。 使用内存管理 API 查看你的后台任务所使用的内存大小。 当有其他应用在前台运行时,你的后台任务所使用的内存越多,操作系统就越难保持其运行。 用户最终控制你的应用可以执行的所有后台活动,并且可以看到你的应用对电池使用情况的影响。
  • CPU 时间:后台任务受其基于触发器类型获取的时钟时间使用的限制。 应用程序触发器触发的后台任务限制为只能运行约 10 分钟。

有关适用于后台任务的资源限制,请参阅使用后台任务支持应用

注解

从 Windows 10 开始,用户无需再将其应用添加到锁屏界面即可利用后台任务。

如果你已先调用 RequestAccessAsync,后台任务将仅使用 ApplicationTrigger 运行。