Beispiel #1
0
        public static bool SetAutoCameraUpload(bool onOff)
        {
            var resourceIntensiveTask = ScheduledActionService.Find("ScheduledCameraUploadTaskAgent") as ResourceIntensiveTask;

            // If the task already exists and background agents are enabled for the
            // application, you must remove the task and then add it again to update
            // the schedule.
            if (resourceIntensiveTask != null)
            {
                try
                {
                    ScheduledActionService.Remove("ScheduledCameraUploadTaskAgent");
                }
                catch (InvalidOperationException e)
                {
                    LogService.Log(MLogLevel.LOG_LEVEL_ERROR, "Error disabling CAMERA UPLOADS service", e);
                    return(false);
                }
            }

            if (!onOff)
            {
                LogService.Log(MLogLevel.LOG_LEVEL_INFO, "Disable CAMERA UPLOADS service");
                return(false);
            }

            resourceIntensiveTask = new ResourceIntensiveTask("ScheduledCameraUploadTaskAgent")
            {
                // The description is required for periodic agents. This is the string that the user
                // will see in the background services Settings page on the device.
                Description = AppMessages.ResourceIntensiveTaskDescription
            };

            // Place the call to Add in a try block in case the user has disabled agents.
            Exception exception;

            try
            {
                ScheduledActionService.Add(resourceIntensiveTask);

                // If debugging is enabled, use LaunchForTest to launch the agent in one minute.
                #if DEBUG
                ScheduledActionService.LaunchForTest("ScheduledCameraUploadTaskAgent", TimeSpan.FromSeconds(5));
                #endif

                LogService.Log(MLogLevel.LOG_LEVEL_INFO, "Enable CAMERA UPLOADS service");
                return(true);
            }
            catch (InvalidOperationException e)
            {
                exception = e;
                if (e.Message.Contains("BNS Error: The action is disabled"))
                {
                    new CustomMessageDialog(AppMessages.BackgroundAgentDisabled_Title,
                                            AppMessages.BackgroundAgentDisabled, App.AppInformation).ShowDialog();
                }
            }
            catch (SchedulerServiceException e) { exception = e; }
            catch (ArgumentException e) { exception = e; }

            LogService.Log(MLogLevel.LOG_LEVEL_ERROR, "Error enabling CAMERA UPLOADS service", exception);
            return(false);
        }