Windows 8 Study Notes (19th)-. Background task backgroundtask (II)

Source: Internet
Author: User

Backgroundtask and lockscreen

Lockscreen: We often lock screen when using computers. When we lock screen, we can also see some messages, such as the number of unread emails and the number of new messages in a chat software, this is the common implementation between backgroundtask and lockscreen ~

General lockscreen information includes the following parts:

(1) date and time (2) network status (3) battery volume this is the most basic part. Of course, there are also lock screen applications automatically set by the system, such as mailboxes, calendars, and information, how can we implement our own applications?

 

Before that, first understand the basic content of lockscreen ~ 1. Under what circumstances do I need to set the application as a screen lock application? The lock screen application is generally used to report important or meaningful messages to users, and the message content is concise and real-time, giving users a glimpse of the screen, the latest information is displayed. 2. Declare the lockscreen application capability in the package of the application. in appxmanifest, You need to declare the screen lock function and the badge displayed on the screen. The details are as follows:

3. In the program, you can prompt whether to place the application on the screen lock and use backgroundexecutionmanager. requestaccessasync () will display a dialog box. When "allow" is selected, the program will be placed in the screen lock, but there can be up to seven screen lock applications. When there are more than seven, the user is asked which application to replace.

We can also manually set the tile program as a screen lock application:

Below we implement a simple timed push information to the lockscreen app:

Step 1: Create a blank page pushnotification. XAML. Put the three buttons above to set the app to lockscreen, enable the background scheduled push message, and cancel the background task.

Step 2:Create a background task class icationicationbacktask. the CS file provides a simple timer for one person to implement timed push. The code is similar to the previous one. It is triggered by backgroundtaskprogresseventhandler. Currently, I have not found any other method to trigger the main program, hope you can teach me with high fingers ~

Step 3:Set the package. appxmanifest file in the main project

You also need to add background tasks in the Declaration:

Step 4:Background code for implementing pushnotification. XAML
Click the set lockscreen app button:

Async private void btnsetlockscreen_click (Object sender, routedeventargs E)
{
Backgroundaccessstatus status = await backgroundexecutionmanager. requestaccessasync ();
Switch (Status)
{
Case backgroundaccessstatus. allowedwithalwaysonrealtimeconnectivity:
Tbinfo. Text = "this app is on the lock screen and has access to always-on real time connectivity .";
Btnsendbadge. isenabled = true;
Break;
Case backgroundaccessstatus. allowedmayuseactiverealtimeconnectivity:
Tbinfo. Text = "this app is on the lock screen and has access to active real time connectivity .";
Btnsendbadge. isenabled = true;
Break;
Case backgroundaccessstatus. Denied:
Tbinfo. Text = "this app is not on the lock screen .";
Break;
Case backgroundaccessstatus. Unspecified:
Tbinfo. Text = "the user has not yet taken any action. This is the default setting and the app is not on the lock screen .";
Break;
Default:
Break;
}}

If the app is not added with lockscreen, the following screen is displayed:

Click "Send badge backtask". You also need to register the task first.

VaR builder = new backgroundtaskbuilder ();

Builder. Name = samplebackgroundtaskname;
Builder. taskentrypoint = samplebackgroundtaskentrypoint;
Systemtrigger trigger = new systemtrigger (systemtriggertype. useraway, false );
Builder. settrigger (trigger );
Systemcondition condition = new systemcondition (systemconditiontype. userpresent );
If (condition! = NULL)
{
Builder. addcondition (condition );
}
Task = builder. Register ();
Task. Completed + = task_completed;
Task. Progress + = task_progress;

 

Push messages in the task_progress event:

Void task_progress (backgroundtaskregistration sender, backgroundtaskprogresseventargs ARGs)

{

Dispatcher. runasync (coredispatcherpriority. Normal, () =>
{
VaR taskregistration = sender as ibackgroundtaskregistration;
VaR progressargs = ARGs as backgroundtaskprogresseventargs;

If (taskregistration! = NULL) & (progressargs! = NULL ))
{// Badgenumericnotifnotifcontent badgecontent = new badgenumericnotificationcontent (progressargs. Progress );
// Badgeupdatemanager. createbadgeupdaterforapplication (). Update (badgecontent. createnotification ());

Itilewidesmallimageandtext03 tilecontent = tilecontentfactory. createtilewidesmallimageandtext03 ();
Tilecontent. textbodywrap. Text = "this tile notification has an image, but it won't be displayed on the lock screen ";
Tilecontent. image. src = "MS-appx: // assets/tile-sdk.png ";
Tilecontent. requiresquarecontent = false;
}
});
}

 

The preceding two types of push are implemented. One is simple badge and the other is tile information with text information. For the latter type with detailed information, you must manually set it in PC settings, the text is displayed in lockscreen as follows:

 

The above is a brief introduction to lockscreen, which may have some shortcomings ~

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.