/// <summary>
        /// Register a SampleBackgroundTaskWithCondition.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void RegisterButton_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                //Save current internet profile and network adapter ID globally
                var connectionProfile = NetworkInformation.GetInternetConnectionProfile();
                if (connectionProfile != null)
                {
                    internetProfile = connectionProfile.ProfileName;
                    var networkAdapterInfo = connectionProfile.NetworkAdapter;
                    if (networkAdapterInfo != null)
                    {
                        networkAdapter = networkAdapterInfo.NetworkAdapterId.ToString();
                    }
                    else
                    {
                        networkAdapter = "Not connected to Internet";
                    }
                }
                else
                {
                    internetProfile = "Not connected to Internet";
                    networkAdapter  = "Not connected to Internet";
                }

#if WINDOWS_PHONE_APP
                // On Windows Phone, we need to request access from the BackgroundExecutionManager in order for our background task to run
                RequestBackgroundAccess();
#endif

                var task = BackgroundTaskSample.RegisterBackgroundTask(BackgroundTaskSample.SampleBackgroundTaskEntryPoint,
                                                                       BackgroundTaskSample.SampleBackgroundTaskWithConditionName,
                                                                       new SystemTrigger(SystemTriggerType.NetworkStateChange, false),
                                                                       new SystemCondition(SystemConditionType.InternetAvailable));

                //Associate background task completed event handler with background task.
                task.Completed += new BackgroundTaskCompletedEventHandler(OnCompleted);
                rootPage.NotifyUser("Registered for NetworkStatusChange background task with Internet present condition\n", NotifyType.StatusMessage);
                UpdateButton(true);
            }
            catch (Exception ex)
            {
                rootPage.NotifyUser(ex.ToString(), NotifyType.ErrorMessage);
            }
        }
Beispiel #2
0
 /// <summary>
 /// Unregister a SampleBackgroundTaskWithCondition.
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 private void UnregisterButton_Click(object sender, RoutedEventArgs e)
 {
     BackgroundTaskSample.UnregisterBackgroundTasks(BackgroundTaskSample.SampleBackgroundTaskWithConditionName);
     rootPage.NotifyUser("Unregistered for NetworkStatusChange background task with Internet present condition\n", NotifyType.StatusMessage);
     UpdateButton(false);
 }