背水一战 Windows 10 (115) - 后台任务: 通过 toast 激活后台任务, 定时激活后台任务
2018-06-22 08:29
399 查看
背水一战 Windows 10 (115) - 后台任务: 通过 toast 激活后台任务, 定时激活后台任务
作者:webabcd
介绍
背水一战 Windows 10 之 后台任务
- 通过 toast 激活后台任务
- 定时激活后台任务
示例
1、演示如何通过 toast 激活后台任务
/BackgroundTaskLib/BackgroundTaskToast.cs
/* * 后台任务,用于演示如何通过 toast 激活后台任务 * * ToastNotificationActionTriggerDetail - toast 触发器信息 * Argument - 由 toast 传递过来的参数 * UserInput - 由 toast 传递过来的输入框数据 */ using System; using Windows.ApplicationModel.Background; using Windows.Storage; using Windows.UI.Notifications; namespace BackgroundTaskLib { public sealed class BackgroundTaskToast : IBackgroundTask { public async void Run(IBackgroundTaskInstance taskInstance) { BackgroundTaskDeferral deferral = taskInstance.GetDeferral(); try { // 获取 ToastNotificationActionTriggerDetail 对象 ToastNotificationActionTriggerDetail toastDetail = taskInstance.TriggerDetails as ToastNotificationActionTriggerDetail; if (toastDetail != null) { string result = ""; result = "argument: " + toastDetail.Argument; result += Environment.NewLine; foreach (string key in toastDetail.UserInput.Keys) { result += $"key:{key}, value:{toastDetail.UserInput[key]}"; result += Environment.NewLine; } // 将获取到的 toast 信息保存为文件 StorageFile file = await ApplicationData.Current.LocalFolder.CreateFileAsync(@"webabcdBackgroundTask\toast.txt", CreationCollisionOption.ReplaceExisting); await FileIO.WriteTextAsync(file, result); } } finally { deferral.Complete(); } } } }
BackgroundTask/Toast.xaml
<Page x:Class="Windows10.BackgroundTask.Toast" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:Windows10.BackgroundTask" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"> <Grid Background="Transparent"> <StackPanel Margin="10 0 10 10"> <Button Name="buttonShowToast" Content="显示 toast(点击 toast 框或点击 toast 中的按钮则可激活后台任务)" Click="buttonShowToast_Click" Margin="5" /> </StackPanel> </Grid> </Page>
BackgroundTask/Toast.xaml.cs
/* * 演示如何通过 toast 激活后台任务 * * 注: * 1、需要引用后台任务项目,相关代码参见 BackgroundTaskLib/BackgroundTaskToast.cs * 2、需要在 Package.appxmanifest 添加“后台任务”声明,支持的任务类型选择“常规”,并指定 EntryPoint(后台任务的类全名),类似如下: * <Extension Category="windows.backgroundTasks" EntryPoint="BackgroundTaskLib.BackgroundTaskToast"> * <BackgroundTasks> * <Task Type="general" /> * </BackgroundTasks> * </Extension> * * * 本例的 toast 的 xml 说明: * activationType - 通过点击 toast 激活 app 时的激活方式,background 代表后台方式激活 * 其他 toast 的相关知识点请参见:/Notification/Toast/ */ using System; using System.Collections.Generic; using Windows.ApplicationModel; using Windows.ApplicationModel.Background; using Windows.Data.Xml.Dom; using Windows.Storage; using Windows.UI.Notifications; using Windows.UI.Popups; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Navigation; namespace Windows10.BackgroundTask { public sealed partial class Toast : Page { private string _taskName = "Toast"; private string _taskEntryPoint = "BackgroundTaskLib.BackgroundTaskToast"; public Toast() { this.InitializeComponent(); } protected async override void OnNavigatedTo(NavigationEventArgs e) { base.OnNavigatedTo(e); // 在注册后台任务之前,需要调用 BackgroundExecutionManager.RequestAccessAsync(),如果是更新过的 app 则在之前还需要调用 BackgroundExecutionManager.RemoveAccess() string appVersion = $"{Package.Current.Id.Version.Major}.{Package.Current.Id.Version.Minor}.{Package.Current.Id.Version.Build}.{Package.Current.Id.Version.Revision}"; if ((string)ApplicationData.Current.LocalSettings.Values["AppVersion"] != appVersion) { // 对于更新的 app 来说先要调用这个方法 BackgroundExecutionManager.RemoveAccess(); // 注册后台任务之前先要调用这个方法,并获取 BackgroundAccessStatus 状态 BackgroundAccessStatus status = await BackgroundExecutionManager.RequestAccessAsync(); if (status == BackgroundAccessStatus.Unspecified || status == BackgroundAccessStatus.DeniedBySystemPolicy || status == BackgroundAccessStatus.DeniedByUser) { // 无权限注册后台任务 await new MessageDialog("没有权限注册后台任务").ShowAsync(); } else { // 有权限注册后台任务 ApplicationData.Current.LocalSettings.Values["AppVersion"] = appVersion; } } // 如果任务已注册,则注销 foreach (KeyValuePair<Guid, IBackgroundTaskRegistration> t in BackgroundTaskRegistration.AllTasks) { if (t.Value.Name == _taskName) { t.Value.Unregister(true); } } BackgroundTaskBuilder builder = new BackgroundTaskBuilder { Name = _taskName, TaskEntryPoint = _taskEntryPoint }; // 指定后台任务的触发器类型为 ToastNotificationActionTrigger(即通过 toast 激活) builder.SetTrigger(new ToastNotificationActionTrigger()); // 注册后台任务 BackgroundTaskRegistration task = builder.Register(); } // 弹出 toast 通知(点击 toast 框或点击 toast 中的按钮则可激活后台任务) private void buttonShowToast_Click(object sender, RoutedEventArgs e) { string toastXml = @" <toast activationType='background' launch='launch arguments'> <visual> <binding template='ToastGeneric'> <text>toast - title</text> <text>toast - content</text> </binding> </visual> <actions> <input type='text' id='message1' title='title1' /> <action content='确认' activationType='background' arguments='action arguments'/> </actions> </toast>"; XmlDocument toastDoc = new XmlDocument(); toastDoc.LoadXml(toastXml); ToastNotification toast = new ToastNotification(toastDoc); ToastNotificationManager.CreateToastNotifier().Show(toast); } } }
2、演示后台任务的应用(定时激活后台任务)
/BackgroundTaskLib/BackgroundTaskTime.cs
/* * 后台任务,用于演示如何定时激活后台任务 */ using System; using Windows.ApplicationModel.Background; using Windows.Storage; namespace BackgroundTaskLib { // 实现 IBackgroundTask 接口,其只有一个方法,即 Run() public sealed class BackgroundTaskTime : IBackgroundTask { public async void Run(IBackgroundTaskInstance taskInstance) { // 异步操作 BackgroundTaskDeferral deferral = taskInstance.GetDeferral(); try { // 写入相关数据到文件 StorageFile file = await ApplicationData.Current.LocalFolder.CreateFileAsync(@"webabcdBackgroundTask\time.txt", CreationCollisionOption.ReplaceExisting); await FileIO.AppendTextAsync(file, "background task timeTrigger or maintenanceTrigger: " + DateTime.Now.ToString() + Environment.NewLine); } finally { // 完成异步操作 deferral.Complete(); } } } }
BackgroundTask/Time.xaml
<Page x:Class="Windows10.BackgroundTask.Time" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:Windows10.BackgroundTask" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"> <Grid Background="Transparent"> <StackPanel Margin="10 0 10 10"> <Button Name="btnRegister" Content="注册一个后台任务" Margin="5" Click="btnRegister_Click" /> <Button Name="btnUnregister" Content="注销指定的后台任务" Margin="5" Click="btnUnregister_Click" /> </StackPanel> </Grid> </Page>
BackgroundTask/Time.xaml.cs
/* * 演示后台任务的应用(定时激活后台任务) * * 注: * 1、需要引用后台任务项目,相关代码参见 BackgroundTaskLib/BackgroundTaskTime.cs * 2、需要在 Package.appxmanifest 添加“后台任务”声明,支持的任务类型选择“计时器”,并指定 EntryPoint(后台任务的类全名),类似如下: * <Extension Category="windows.backgroundTasks" EntryPoint="BackgroundTaskLib.BackgroundTaskTime"> * <BackgroundTasks> * <Task Type="timer" /> * </BackgroundTasks> * </Extension> */ using System; using System.Collections.Generic; using Windows.ApplicationModel; using Windows.ApplicationModel.Background; using Windows.Storage; using Windows.UI.Core; using Windows.UI.Popups; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Navigation; namespace Windows10.BackgroundTask { public sealed partial class Time : Page { // 所注册的后台任务的名称 private string _taskName = "Time"; // 所注册的后台任务的 EntryPoint,即后台任务的类全名 private string _taskEntryPoint = "BackgroundTaskLib.BackgroundTaskTime"; // 后台任务是否已在系统中注册 private bool _taskRegistered = false; public Time() { this.InitializeComponent(); } protected override void OnNavigatedTo(NavigationEventArgs e) { // 遍历所有已注册的后台任务 foreach (KeyValuePair<Guid, IBackgroundTaskRegistration> task in BackgroundTaskRegistration.AllTasks) { if (task.Value.Name == _taskName) { _taskRegistered = true; break; } } UpdateUI(); } private async void btnRegister_Click(object sender, RoutedEventArgs e) { // 在注册后台任务之前,需要调用 BackgroundExecutionManager.RequestAccessAsync(),如果是更新过的 app 则在之前还需要调用 BackgroundExecutionManager.RemoveAccess() string appVersion = $"{Package.Current.Id.Version.Major}.{Package.Current.Id.Version.Minor}.{Package.Current.Id.Version.Build}.{Package.Current.Id.Version.Revision}"; if ((string)ApplicationData.Current.LocalSettings.Values["AppVersion"] != appVersion) { // 对于更新的 app 来说先要调用这个方法 BackgroundExecutionManager.RemoveAccess(); // 注册后台任务之前先要调用这个方法,并获取 BackgroundAccessStatus 状态 BackgroundAccessStatus status = await BackgroundExecutionManager.RequestAccessAsync(); if (status == BackgroundAccessStatus.Unspecified || status == BackgroundAccessStatus.DeniedBySystemPolicy || status == BackgroundAccessStatus.DeniedByUser) { // 无权限注册后台任务 await new MessageDialog("没有权限注册后台任务").ShowAsync(); } else { // 有权限注册后台任务 ApplicationData.Current.LocalSettings.Values["AppVersion"] = appVersion; } } // 用于构造一个后台任务 BackgroundTaskBuilder builder = new BackgroundTaskBuilder() { Name = _taskName, TaskEntryPoint = _taskEntryPoint }; // 指定后台任务的触发器类型为 MaintenanceTrigger(定时激活,最小周期 15 分钟) builder.SetTrigger(new MaintenanceTrigger(15, false)); // 注册后台任务 BackgroundTaskRegistration task = builder.Register(); _taskRegistered = true; UpdateUI(); } private void btnUnregister_Click(object sender, RoutedEventArgs e) { // 遍历所有已注册的后台任务 foreach (KeyValuePair<Guid, IBackgroundTaskRegistration> task in BackgroundTaskRegistration.AllTasks) { if (task.Value.Name == _taskName) { // 从系统中注销指定的后台任务。唯一一个参数代表如果当前后台任务正在运行中,是否需要将其取消 task.Value.Unregister(true); break; } } _taskRegistered = false; UpdateUI(); } private async void UpdateUI() { await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { btnRegister.IsEnabled = !_taskRegistered; btnUnregister.IsEnabled = _taskRegistered; }); } } }
OK
[源码下载]
相关文章推荐
- 背水一战 Windows 10 (116) - 后台任务: 前台程序激活后台任务
- 背水一战 Windows 10 (120) - 后台任务: 后台上传任务
- 背水一战 Windows 10 (121) - 后台任务: 推送通知
- 背水一战 Windows 10 (106) - 通知(Toast): 通过 toast 打开协议, 通过 toast 选择在指定的时间之后延迟提醒或者取消延迟提醒
- 背水一战 Windows 10 (114) - 后台任务: 后台任务的 Demo(与 app 不同进程), 后台任务的 Demo(与 app 相同进程)
- 背水一战 Windows 10 (117) - 后台任务: 后台下载任务
- 背水一战 Windows 10 (118) - 后台任务: 后台下载任务(任务分组,并行或串行执行,组完成后通知)
- 背水一战 Windows 10 (119) - 后台任务: 后台下载任务(任务分组,组完成后触发后台任务)
- windows计划任务后台定时执行java的jar文件
- 背水一战 Windows 10 (25) - MVVM: 通过 x:Bind 实现 MVVM(不用 Command)
- 背水一战 Windows 10 (104) - 通知(Toast): 纯文本 toast, 短时 toast, 长时 toast, 图文 toast
- 背水一战 Windows 10 (22) - 绑定: 通过 Binding 绑定对象, 通过 x:Bind 绑定对象, 通过 Binding 绑定集合, 通过 x:Bind 绑定集合, 通过 Binding 绑定字典表
- 背水一战 Windows 10 (122) - 其它: 通过 Windows.System.Profile 命名空间下的类获取信息, 查找指定类或接口的所在程序集的所有子类和子接口
- 背水一战 Windows 10 (65) - 控件(WebView): 对 WebView 中的内容截图, 通过 Share Contract 分享 WebView 中的被选中的内容
- 背水一战 Windows 10 (105) - 通知(Toast): 带按钮的 toast, 带输入的 toast(文本输入框,下拉选择框)
- 背水一战 Windows 10 (24) - MVVM: 通过 Binding 或 x:Bind 结合 Command 实现,通过非 ButtonBase 触发命令
- Sql server定时执行某个sql 通过Windows 计划任务(非代理Job方式)
- 背水一战 Windows 10 (25) - MVVM: 通过 x:Bind 实现 MVVM(不用 Command)
- 背水一战 Windows 10 (101) - 应用间通信: 通过协议打开指定的 app 并传递数据以及获取返回数据, 将本 app 沙盒内的文件共享给其他 app 使用
- 背水一战 Windows 10 (107) - 通知(Toast): 提示音, 特定场景