Background Task in Universal Windows Program - Part One

Background task is one of the main concepts in mobile programming. Background task is mainly used for notification to a user. Whether or not the user is present in the application, we can give the notification to the screen; or without the notification, we can update the processing of the data in the background.

Let us see how this concept can be implemented in our Application.

Creating Background task is divided into three types:

  1. Creating Background task component
  2. Register Background task
  3. Handling Background task

Creating Background Task Component

Basically, the background task is the small component run in the background. This component's main task is tied up with the system, to get the input primarily based on the condition. We need to create a component: UWP programing. We need to use Windows run time component for the background component.

IBackgroundTask

It is an interface between the system and the Application. Whenever the trigger is executed, the system will trigger the run method.



The diagram describes the flow of the component,

  1. Create Windows runtime component project
  2. Implement the IBackgroundTask interface and the run method

Build the component and it is ready to use, but it has to be registered into the system, until then we can’t use it.

Register Background task

Below classes are used to register the Windows run time component as the background task,

  • BackgroundExecutionManager
  • IBackgroundTaskRegistration&BackgroundTaskRegistration
  • BackgroundTaskBuilder

Steps to implement the background task in our Application are shown below:




Permission (BackgroundExecutionManager)

This class is used to manage all the background component permissions.

Our Application class first checks with the BackgroundExecutionManager, whether it has required permission or not. This function returns the BackgroundAccessStatus enum. If permission has been denied, we can’t register the background task component.

Registered or Not (BackgroundTaskRegistration)

This class maintains all the system background task information if an Application has a permission check; whether this component is already registered or not. If it is already registered, there is no need to register again, get the object and implement the call back events.

Register Background Task (BackgroundTaskBuilder)

This class is helpful to register our component into BackgroundTaskRegistrationClass.

Create an object for this class with the background task name (Name of the BG task name) and the task entry name: Windows Runtime component (Namespace with Class Name).

Assign the trigger in which synchronization of our background task should receive the event from the system and we can add the condition, which is optional.

Finally, register this object in the BackgroundTaskRegistrationClass.

Implement Events

Once class is registered successfully, implement the call back in our Application to receive the event from the Windows run time component.

Overall System OS -> (trigger) -> Windows Runtime component -> (trigger) -> Application.

Our application receives the input from the background task and the answer is no. Therefore, we have to do some changes in the Package.appxmanifest file also. In the next article, we will see in detail about this, with an example.

Up Next
    Ebook Download
    View all
    Learn
    View all