如何在启动时启动后台任务 - Windows应用商店应用

时间:2015-08-26 14:26:37

标签: c# windows-store-apps background-task

我的平板电脑运行Windows 8.1专业版。

它有一个后台任务,每15'由一个时间触发器触发。它很有效,很公平。

问题是我需要在设备的每次启动(启动应用)时自动启动后台任务。

我通过以下代码注册了我的bg:

       builder.Name = "bikePositionUpdate";
        builder.TaskEntryPoint = "BackgroundTaskGps.BikeGPSPositionUpdateBackgroundTask";
        builder.SetTrigger(new TimeTrigger(15, false)); // 

        // adding condition
        SystemCondition internetCondition = new SystemCondition(SystemConditionType.InternetAvailable);
        SystemCondition userPresentCondition = new SystemCondition(SystemConditionType.UserPresent); 

        builder.AddCondition(internetCondition);
        builder.AddCondition(userPresentCondition);
        BackgroundTaskRegistration taskRegistration = builder.Register();

我的应用已锁定屏幕访问

         await BackgroundExecutionManager.RequestAccessAsync();

我怎样才能做到这一点?我错过了什么吗?

5 个答案:

答案 0 :(得分:6)

您必须添加SystemConditionType.SessionConnected条件,每次用户登录Windows时都会发生这种情况。

应用程序必须放在锁定屏幕上才能使用此触发器类型成功注册后台任务。

编辑:

在此网址上,您可以找到有关您需要的官方文档以及如何使用它:

https://msdn.microsoft.com/en-us/library/windows/apps/xaml/hh977056.aspx

https://msdn.microsoft.com/en-us/library/windows/apps/xaml/windows.applicationmodel.background.systemtriggertype.aspx

答案 1 :(得分:2)

I think you should add SystemConditionType.SessionConnected condition,where this condition will check every time theuser log on to Windows

答案 2 :(得分:1)

您是否尝试将其添加到注册表中的启动时运行?

我没有8.1检查,但如果没有从win7更改路径应该是HKEY_CURRENT_USER \ Software \ Microsoft \ Windows \ CurrentVersion \ Run(或HKEY_LOCAL_MACHINE)只需创建一个新的字符串值与您的应用程序的路径,它将是在Windows启动时运行

答案 3 :(得分:1)

await BackgroundExecutionManager.RequestAccessAsync();的结果应该与AllowedWithAlwaysOnRealTimeConnectivity类似。

这意味着:用户在对话框中选择“允许”。该应用已添加到锁定屏幕,可以设置后台任务

这个BackgroundTaskRegistration taskRegistration = builder.Register(); 你在await BackgroundExecutionManager.RequestAccessAsync();

之后骂了一下电话

答案 4 :(得分:-2)

您是否尝试将应用程序添加到Windows任务计划程序作为安装过程的一部分?

相关问题