Bruno Gabriel dos Santos Bruno Gabriel dos Santos - 1 month ago 11
C# Question

Detect when device Rebooting or Starting in Windows Phone 8.1

I need detect rebooting or device starting, to do this I followed this topic (Detecting reboot programmatically in Windows Phone 8.1), but in my case the method canceled is never called in background task.

When I starting debug, my method is called after forcing changes in timezone that made by:

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


My background task is:

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();
}


I know that is never called, because the next routine is always in false (it works when app is starting MainPage method):

Answer

After using some hours in this problem, I found the solution, Windows Phone have a System Trigger type that is called when device is booted, named: SessionConnected.

Thus, only need simple changes:

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