在Windows Phone 8.1中以编程方式检测重新启动 [英] Detecting reboot programmatically in Windows Phone 8.1

查看:75
本文介绍了在Windows Phone 8.1中以编程方式检测重新启动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个WP 8.1运行时,它将启动DeviceUseTrigger后台任务.问题在于,每当电话重启时,该任务显然会取消,但是任务注册仍然存在.因此,当我下次启动应用程序时,后台任务似乎会运行,而实际上却没有.我想要某种方法来检测电话何时重新启动和/或以某种方式检测任务是否正在实际运行.我用来检查后台任务注册的代码如下:

I have a WP 8.1 Runtime which launches a DeviceUseTrigger background task. The problem is that whenever the phone reboots, this task obviously cancels, but the task registration remains in place. So when I launch my app the next time the background task appears to be running when in reality it isn't. I want some way of detecting whenever the phone reboots and/or detect in some way whether or not the task is actually running or not. The code I'm using to check background task registration is as follows:

foreach(IBackgroundTaskRegistration task in BackgroundTaskRegistration.AllTasks.Values)
        {
            if ((task as BackgroundTaskRegistration).Name == myTaskName)
            {
                Debug.WriteLine("Task is already running");
            }
        }

推荐答案

我能够以几乎令人尴尬的简单方式解决该问题.当手机关闭时,后台任务会取消,因此我在后台任务的taskInstane.Canceled事件中附加了一个事件处理程序,并向其中添加了两行:

I was able to solve the problem in an almost embarrassingly simple way. The background task cancels when the phone is shutting down so I attached an event handler to the taskInstane.Canceled event in my background task and just added two lines to it:

StorageFile file = await ApplicationData.Current.LocalFolder.CreateFileAsync("TaskCancelling.txt" CreateCollisionOption.OpenIfExists);
deferral.Complete();

然后,在前台应用程序中,每当应用程序启动时,以下代码就会运行:

Then, in the foreground app, the following code runs whenever the app launches:

foreach(IBackgroundTaskRegistration task in BackgroundTaskRegistration.AllTasks.Values)
{
   if ((task as BackgroundTaskRegistration).Name == myTaskName)
   {
      if (await IsFilePresentInLocalDirectory("TaskCancelling.txt"))
      {
         //Task registration is present, but task isn't actually running.
         //Unregister the useless task
         (task as BackgroundTaskRegistration).Unregister(true);
         //Delete the file
         StorageFile file = await ApplicationData.Current.LocalFolder.GetFileAsync("TaskCancelling.txt");
         await file.DeleteAsync();
         //Relaunch the DeviceUseTrigger task
         RelaunchBackgroundTask();
      }
   }
}

private async Task<bool> IsFilePresentInLocalDirectory(string fileName)
{
   try
   {
      StorageFile file = await ApplicationData.Current.LocalFolder.GetFileAsync(fileName);
      return true;
   }
   catch (Exception exc)
   {
      return false;
   }
}

不言自明,我只是创建一个空文本文件来创建某种任务取消日志,并且每次我的应用启动时,我都会检查文件是否存在.如果是,则重新启动任务并删除文件.

Pretty self-explanatory, I just create an empty text file to create a sort of log of task cancellation and each time my app launches I check to see it the file is present. If it is, the task is relaunched and the file is deleted.

这篇关于在Windows Phone 8.1中以编程方式检测重新启动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆