后台代理任务错误 [英] Background Agent Task Bug

查看:54
本文介绍了后台代理任务错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先感谢您提供了出色的模板!我对此有疑问.在手机设置中手动禁用后台任务后,我将无法打开它.我一直在寻找答案,但没有成功.我一直在收到消息 打开应用程序后显示一个框,告诉我后台代理已被用户禁用.请帮忙.


wilsantiago

First all thanks for a great template! I am having an issue with it.  I can not turn on background task once I manually disable it in the phone's settings. I have looked for an answer everywhere but with no success.  I just keep getting the message box once opening the app telling me that the background agent has been disabled by user. Please help.


wilsantiago

推荐答案

这不会被视为错误.原因如下,

That won't be characterized as a bug. The reason is as following,

var oldTask = ScheduledActionService.Find(taskName)as PeriodicTask;
oldTask.已启用

var oldTask = ScheduledActionService.Find(taskName) as PeriodicTask;
oldTask.IsEnabled;

在应用程序中使用只读属性会告诉您是否启用或禁用了后台任务(您正在谈论的手动情况是由于后台任务执行中的连续失败,还是由于相关原因应用程式未针对开启 持续一段时间-有一个时间限制),这是有目的的,以便用户可以随时返回并手动禁用任务.

The use of this ready only property in the app tells whether the background task is enabled or disabled (that manual case you are talking about or due to continuous failures in background task execution or given that related app is not opened for last some time - there is a time limit on that) and this is kept by purpose such that the user can always go back and manually disable a task.

在这种情况下,您始终可以删除现有任务(已禁用)并添加新任务(从您的应用程序中).像这样的东西

In such a scenario, you can always remove the existing task (which is disabled) and add a new one (from your app). Something like this,

      私有void AddOrRefreshBackgroundAgent()
       {
          试试
           {
                             //如果任务存在,请将其删除
                             var oldTask = ScheduledActionService.Find(taskName)as PeriodicTask;
                             if(oldTask!= null)
                             {
                    ScheduledActionService.Remove(taskName);
                             }

        private void AddOrRefreshBackgroundAgent()
        {
            try
            {
                // If the task exists, remove it
                var oldTask = ScheduledActionService.Find(taskName) as PeriodicTask;
                if (oldTask != null)
                {
                    ScheduledActionService.Remove(taskName);
                }

               //创建新的任务
                             PeriodicTask task = new PeriodicTask("Task Name");

                // Create the new Task
                PeriodicTask task = new PeriodicTask("Task Name");

               //说明为必填项
                             task.Description =此处描述";

                // Description is required
                task.Description = "Description here";

               //将其添加到服务中以执行
                             ScheduledActionService.Add(task);

                // Add it to the service to execute
                ScheduledActionService.Add(task);

               //ScheduledActionService.LaunchForTest(taskName,TimeSpan.FromMilliseconds(3000));
           }
          捕获(InvalidOperationException异常)
           {
                             if(exception.Message.Contains("BNS错误:操作已禁用"))
                             {
                    MessageBox.Show(该应用程序的背景代理已被用户禁用.","Information",MessageBoxButton.OK);
                             }
                             if(exception.Message.Contains("BNS错误:此类型的ScheduledAction的最大数量已被添加."))
                             {
                    //无需用户执行任何操作.当达到定期任务的硬限制时,系统会提示用户.
                             }
           }
          捕获(SchedulerServiceException)
           {
                             //无需用户操作.
           }
       }

                //ScheduledActionService.LaunchForTest(taskName,TimeSpan.FromMilliseconds(3000));
            }
            catch (InvalidOperationException exception)
            {
                if (exception.Message.Contains("BNS Error: The action is disabled"))
                {
                    MessageBox.Show("Background agents for this application have been disabled by the user.","Information",MessageBoxButton.OK);
                }
                if (exception.Message.Contains("BNS Error: The maximum number of ScheduledActions of this type have already been added."))
                {
                    // No user action required. The system prompts the user when the hard limit of periodic tasks has been reached.
                }
            }
            catch (SchedulerServiceException)
            {
                // No user action required.
            }
        }


这篇关于后台代理任务错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆