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

时间:2016-10-17 12:23:20

标签: c# windows-phone-8.1 windows-store-apps background-process windows-store

我需要检测重启或设备启动,为此我遵循了这个主题(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(当app启动MainPage方法时它会起作用):

1 个答案:

答案 0 :(得分:0)

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

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

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