我想在我的應用程序啓動時啓動我的IBackgroundTask。WinRT - 在通用應用程序中啓動/註冊IBackgroundTask
我已經加入我的任務到.appxmanifest.xml文件,現在我的擴展標記看起來像這樣
<Extensions>
<Extension Category="windows.backgroundTasks" EntryPoint="CordovaApp.Library.UploadTask">
<BackgroundTasks>
<Task Type="systemEvent" />
<Task Type="timer" />
</BackgroundTasks>
</Extension>
</Extensions>
我IBackgroundTask類被稱爲UploadTask並在其中有outtype設置爲另一個項目舉行Windows運行時組件。
下面是代碼的削減版本,所以你可以看到命名空間等
namespace CordovaApp.Library
{
public sealed class UploadTask : IBackgroundTask
{
public async void Run(IBackgroundTaskInstance taskInstance)
{
var connectionProfile = NetworkInformation.GetInternetConnectionProfile();
// connectionProfile can be null (e.g. airplane mode)
if (connectionProfile != null && connectionProfile.IsWlanConnectionProfile)
{
// custom code here
}
}
}
}
我已經加入到這個項目中的引用,我的通用運行時組件項目。
一切都很好。
現在開始申請,我想我必須使用WinJs,所以我有以下代碼。
var uploadTaskName = 'UploadTask';
var tasks = Windows.ApplicationModel.Background.BackgroundTaskRegistration.allTasks;
var uploadTaskFound = false;
for (var i = 0; i < tasks.length; i++) {
if (tasks[i].Value.name == uploadTaskName) {
successCallback();
return;
}
}
Windows.ApplicationModel.Background.BackgroundExecutionManager.requestAccessAsync().then(function() {
var builder = new Windows.ApplicationModel.Background.BackgroundTaskBuilder();
builder.name = "Upload Task";
builder.taskEntryPoint = "CordovaApp.Library.UploadTask";
builder.setTrigger(new Windows.ApplicationModel.Background.TimeTrigger(15, false));
return builder.register();
}).done(function() {
successCallback();
}, function(err) {
errorCallback(err);
});
現在requestAccessAsync方法總是拋出的
0x80004005 - JavaScript runtime error: Unspecified error WinRT information: The application is not lock-screen capable.
一個例外,其註冊的一切是否正確?我在筆記本電腦上通過Visual Studio 2013運行此操作。
檢查是否http://stackoverflow.com/a/14258877/3956290幫助 –