How to start background job on device / app startup for Windows WinRT / C #

How do I start a background job that implements IBackgroundTask

in a Universal Windows / Windows Phone app?

I am using cordova to create an app for ios, android, wp8 and windows. Each platform seems to run its own class before starting the application, so you can add code here to start the task / service. When a windows project is cordlessly created, it is created as a javascript project, so there is no C # file to start with.

Is the only option to add code winJs

to run the background task?

+3


source to share


2 answers


In the end, I got around this by creating it as a cordova plugin. So my plugin has a method called init that I call when the application starts up. Using the plugin, I can make calls to iOS, Android or Windows services.

Once I created the plugin, I started the Windows service from the WindowProxy.js file, which is part of the cordova plugin.

Note that your service itself must be in a separate library, and that library must be of the output type specified as a Windows Runtime component.



Sample code to start a service looks like this

Code in WindowsProxy.js file

init: function (successCallback, errorCallback) {

        var taskRegistered = false;
        var taskName = "Your Background Task Name";
        var background = Windows.ApplicationModel.Background;
        var iter = background.BackgroundTaskRegistration.allTasks.first();

        // check if service already started
        while (iter.hasCurrent) {
            var task = iter.current.value;

            if (task.name === taskName) {
                taskRegistered = true;
                break;
            }

            iter.moveNext();
        }

        if (taskRegistered) {
            successCallback();
        } else {
            Windows.ApplicationModel.Background.BackgroundExecutionManager.requestAccessAsync().then(function () {
                var builder = new Windows.ApplicationModel.Background.BackgroundTaskBuilder();

                builder.name = taskName;
                builder.taskEntryPoint = "CordovaApp.Library.UploadTask"; // namespace of my windows runtime component library
                builder.setTrigger(new Windows.ApplicationModel.Background.TimeTrigger(15, false));
                builder.addCondition(new Windows.ApplicationModel.Background.SystemCondition(Windows.ApplicationModel.Background.SystemConditionType.internetAvailable));

                return builder.register();
            }).done(function () {
                successCallback();
            }, function (err) {
                errorCallback(err);
            });
        }

    },

      

+4


source


If you need to choose a class to implement ... I suggest writing your application natively.

If you want to create a core logic written in javascript and use this class for one task that you need to do in the background, you can write the functionality in native languange for windows and then follow this guide to wrap it in a plugin cordova.



I personally suggest this solution, writing a cordova plugin is pretty simple and very useful.

0


source







All Articles