private async void RegistTask() { // Otherwise request access var status = await BackgroundExecutionManager.RequestAccessAsync(); if (status == BackgroundAccessStatus.DeniedBySystemPolicy || status == BackgroundAccessStatus.Unspecified) { string title = "应用后台活动被禁止"; string content = "由于系统限制,应用程序无法在后台活动。" + "若希望使此应用在后台活动,请尝试更改电源设置以及插入外部电源"; ToastManagement tm = new ToastManagement(title, content, ToastManagement.ErrorMessage); tm.ToastPush(120); } else { foreach (var i in BackgroundTaskRegistration.AllTasks.Values) { if (i.Name.Equals("TimeBackgroundTrigger")) { i.Unregister(true);//将之前的时间触发器任务注销 } } //注册新的时间触发器 BackgroundTaskBuilder timeBuilder = new BackgroundTaskBuilder(); timeBuilder.Name = "TimeBackgroundTrigger"; timeBuilder.SetTrigger(new TimeTrigger(Convert.ToUInt32(c.time), true)); timeBuilder.IsNetworkRequested = true; timeBuilder.AddCondition(new SystemCondition(SystemConditionType.InternetAvailable)); BackgroundTaskRegistration task = timeBuilder.Register(); } }
private async void BeginExtendedExecution() { // The previous Extended Execution must be closed before a new one can be requested. ClearExtendedExecution(); var newSession = new ExtendedExecutionSession(); newSession.Reason = ExtendedExecutionReason.Unspecified; newSession.Description = "Raising periodic toasts"; newSession.Revoked += SessionRevoked; ExtendedExecutionResult result = await newSession.RequestExtensionAsync(); switch (result) { case ExtendedExecutionResult.Allowed: session = newSession; break; default: case ExtendedExecutionResult.Denied: newSession.Dispose(); //建立Toast通知 string title = "Pixiv Wallpaper for Windows 10活动被禁止"; string content = "由于系统限制,应用程序无法在后台继续活动。"; ToastManagement tm = new ToastManagement(title, content, ToastManagement.ErrorMessage); tm.ToastPush(120); break; } }
private async void SessionRevoked(object sender, ExtendedExecutionRevokedEventArgs args) { //session被系统回收时记录原因,session被回收则无法保持后台运行 await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { switch (args.Reason) { case ExtendedExecutionRevokedReason.Resumed: Debug.WriteLine("Extended execution revoked due to returning to foreground."); break; case ExtendedExecutionRevokedReason.SystemPolicy: Debug.WriteLine("Extended execution revoked due to system policy."); string title = "Pixiv Wallpaper for Windows 10后台活动被系统关闭"; string content = "系统回收了应用的会话资源,应用程序在后台时将会被挂起无法继续运行。" + "若希望使此应用在后台活动,请尝试插入外部电源或更改电源设置允许应用运行后台任务"; ToastManagement tm = new ToastManagement(title, content, ToastManagement.ExtendedRevoked); tm.ToastPush(120); break; } }); }
public async void SetWallpaper(bool done) { if (done) { var dialog = new MessageDialog(""); if (!UserProfilePersonalizationSettings.IsSupported()) { string title = "您的设备不支持自动更换壁纸"; string content = " "; ToastManagement tm = new ToastManagement(title, content, ToastManagement.ErrorMessage); tm.ToastPush(60); return; } UserProfilePersonalizationSettings settings = UserProfilePersonalizationSettings.Current; StorageFile file = null; try { file = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appdata:///local/" + img.imgId + ".jpg")); } catch (Exception) { timer.Interval = TimeSpan.FromSeconds(2); timer.Start(); } if (c.lockscr) { //更换锁屏 bool lockscr = await settings.TrySetLockScreenImageAsync(file); if (!lockscr) { string title = "更换锁屏操作失败"; string content = " "; ToastManagement tm = new ToastManagement(title, content, ToastManagement.ErrorMessage); tm.ToastPush(60); } } //更换壁纸 bool deskscr = await settings.TrySetWallpaperImageAsync(file); if (!deskscr) { string title = "更换壁纸操作失败"; string content = " "; ToastManagement tm = new ToastManagement(title, content, ToastManagement.ErrorMessage); tm.ToastPush(60); } else { //推送Toast通知 string title = "成功更换壁纸"; string content = img.imgName + "\r\n" + "id" + img.imgId + "\r\n" + "作者: " + img.userName; string image = file.Path; ToastManagement tm = new ToastManagement(title, content, ToastManagement.WallpaperUpdate, image); tm.ToastPush(10); } } }