检测设备何时重启或启动 Windows Phone 8.1

Detect when device Rebooting or Starting in Windows Phone 8.1

我需要检测重启或设备启动,为此我遵循了这个主题 (Detecting reboot programmatically in Windows Phone 8.1),但在我的例子中,取消的方法从未在后台任务中调用。

当我开始调试时,在强制更改时区后调用我的方法:

builder.SetTrigger(new SystemTrigger(SystemTriggerType.TimeZoneChange, false));

我的后台任务是:

public void Run(IBackgroundTaskInstance taskInstance)
        {
            BackgroundTaskDeferral defferal = taskInstance.GetDeferral();
            taskInstance.Canceled += TaskInstance_Canceled;
            defferal.Complete();
        }

        private async void TaskInstance_Canceled(IBackgroundTaskInstance sender, BackgroundTaskCancellationReason reason)
        {
            BackgroundTaskDeferral defferal = sender.GetDeferral();
            try
            {
                StorageFolder localFolder = Windows.Storage.ApplicationData.Current.LocalFolder;
                await localFolder.CreateFileAsync("bruno.txt", CreationCollisionOption.OpenIfExists);

            }
            catch (Exception e)
            {
                Debug.WriteLine("Fail to create File test: " + e);
            }

            defferal.Complete();
        }

我知道它永远不会被调用,因为下一个例程始终为 false(它在应用程序启动 MainPage 方法时起作用):

在这个问题上使用了几个小时后,我找到了解决方案,Windows Phone 有一个在设备启动时调用的系统触发器类型,名为:SessionConnected。

因此,只需要简单的改动:

builder.SetTrigger(新系统触发器(SystemTriggerType.SessionConnected, false));