locked
BackgroundTask Register RRS feed

  • Question

  • I was developing application with Geofencing, when stuck at issue:

    ArgumentException (Value does not fall within the expected range)
    when I am trying to register background task for geofencing.

    Sample from MSDN has the same problem. I will show code from sample for clarity (link to sample: http://code.msdn.microsoft.com/windowsapps/Geolocation-2483de66#content
    use scenario 5, 
    http://msdn.microsoft.com/en-us/library/windows/apps/dn440583.aspx - instruction how to test).

    All what I have done:
    Build my app in Visual Studio.
    Deploy the app locally first and add the app to the lock screen in Settings. 
    Close your app that is running locally.
    Launch your app in the Visual Studio simulator. 
    Call RegisterBackgroundTask(..)  (simply pressed button register in scenario 5)

    There is code from MSDN sample that I have commented for successful app deploying in Simulator - tagged with ////// [my changes] ////////

            async private void RegisterBackgroundTask(object sender, RoutedEventArgs e)
            {
                try
                {

                    // Get permission for a background task from the user. If the user has already answered once,
                    // this does nothing and the user must manually update their preference via PC Settings.
                    //BackgroundAccessStatus backgroundAccessStatus = await BackgroundExecutionManager.RequestAccessAsync();  ////// [my changes] ////////

                    // Regardless of the answer, register the background task. If the user later adds this application
                    // to the lock screen, the background task will be ready to run.
                    // Create a new background task builder
                    BackgroundTaskBuilder geofenceTaskBuilder = new BackgroundTaskBuilder();

                    geofenceTaskBuilder.Name = SampleBackgroundTaskName;
                    geofenceTaskBuilder.TaskEntryPoint = SampleBackgroundTaskEntryPoint;

                    // Create a new location trigger
                    var trigger = new LocationTrigger(LocationTriggerType.Geofence);

                    // Associate the locationi trigger with the background task builder
                    geofenceTaskBuilder.SetTrigger(trigger);

                    // If it is important that there is user presence and/or
                    // internet connection when OnCompleted is called
                    // the following could be called before calling Register()
                    // SystemCondition condition = new SystemCondition(SystemConditionType.UserPresent | SystemConditionType.InternetAvailable);
                    // geofenceTaskBuilder.AddCondition(condition);

                    // Register the background task
                    geofenceTask = geofenceTaskBuilder.Register();

                    // Associate an event handler with the new background task
                    geofenceTask.Completed += new BackgroundTaskCompletedEventHandler(OnCompleted);

                    UpdateButtonStates(/*registered:*/ true);

                    ////// [my changes] ////////
                    //switch (backgroundAccessStatus)
                    //{
                    //    case BackgroundAccessStatus.Unspecified:
                    //    case BackgroundAccessStatus.Denied:
                    //        rootPage.NotifyUser("This application must be added to the lock screen before the background task will run.", NotifyType.ErrorMessage);
                    //        break;

                    //    default:
                    //        // Ensure we have presented the location consent prompt (by asynchronously getting the current
                    //        // position). This must be done here because the background task cannot display UI.
                    //        GetGeopositionAsync();
                    //        break;
                    //}
                    ////// [my changes] ////////
                }
                catch (Exception ex)
                {
                    // HRESULT_FROM_WIN32(ERROR_NOT_SUPPORTED) == 0x80070032
                    const int RequestNotSupportedHResult = unchecked((int)0x80070032);

                    if (ex.HResult == RequestNotSupportedHResult)
                    {
                        rootPage.NotifyUser("Location Simulator not supported.  Could not get permission to add application to the lock screen, this application must be added to the lock screen before the background task will run.", NotifyType.StatusMessage);
                    }
                    else
                    {
                        rootPage.NotifyUser(ex.ToString(), NotifyType.ErrorMessage);
                    }

                    UpdateButtonStates(/*registered:*/ false);
                }
            }

    Exception falls on string: geofenceTask = geofenceTaskBuilder.Register();

    Can anybody help me?
    Monday, November 18, 2013 6:07 PM

All replies