Search code examples
c#.netwindows-phone-8.1background-task

Excute a program from a BackgroundTask


I have made a background task made it listening on NetworkStateChange, and that works fine.

I'm trying to launch a program via Windows.System.Launcher.LaunchUriAsync But I get an Access is denied exception when trying:

Here is the source for the task:

namespace Tasks
{
    public sealed class Upload : IBackgroundTask
    {
        BackgroundTaskDeferral _deferral = null;
        private bool wifiConnected = false;

        public bool WiFiConnected
        {
            get
            {
                return wifiConnected;
            }
            set
            {
                if (value == wifiConnected)
                    return;

                wifiConnected = value;
            }
        }


        public void Run(IBackgroundTaskInstance taskInstance)
        {

            var InternetConnectionProfile = NetworkInformation.GetInternetConnectionProfile();
            WiFiConnected = InternetConnectionProfile != null;

            if (!WiFiConnected)
            {
                LaunchFlexCheck();
                return;
            }


            _deferral = taskInstance.GetDeferral();
            _deferral.Complete();
        }

        private async void LaunchFlexCheck()
        {
            Debug.WriteLine("BEFORE LaunchFlexCheck()");
            bool result = false;
            try
            {
                result = Windows.System.Launcher.LaunchUriAsync(new System.Uri("myapp:MainPage")).GetResults();
            }
            catch (Exception e)
            {
                result = false;
                Debug.WriteLine("LaunchFlexCheck exception:" +  e.Message );
            }

            Debug.WriteLine("LaunchFlexCheck succes:" + (result ? "true" : "false"));
            Debug.WriteLine("AFTER LaunchFlexCheck()");
        }

    }
}

Is is posible to do so?


Solution

  • Launching an application from a background task means that the user would be interrupted in the middle of what he's doing, to launch an application he hasn't requested. That's in complete contradiction with Windows Phone's design guidelines, in which the user must stay in control at all time. As such, I'm confident that it's not possible.