Windows 8 学习笔记(十九)--.后台任务BackgroundTask(II)

 BackgroundTask and LockScreen

 LockScreen锁屏,我们在使用电脑时经常会锁屏,当我们锁屏时,我们也可以看到一些消息,如未读邮件数、某聊天软件的新消息数等,这就是BackgroundTask与LockScreen之  间的共同实现了~

 一般LockScreen的信息包括以下几部分:

(1) 日期与时间
(2) 网络状态
(3)电池量
 这是最基本的几部分,当然还有系统自动设置的锁屏应用,如邮箱、日历、信息等,那我们自己创建的应用如何实现呢?

 

 在这之前,先了解LockScreen的基本内容~
   1、 什么情况下需要将应用程序设置为锁屏应用?
      锁屏应用一般用于向用户报告重要或有意义的消息,且消息内容简洁实时,使用户一瞥屏幕,就能看到最新的信息
   2、 声明LockScreen应用能力
     在应用程序的Package.appxmanifest中需要声明锁屏功能和屏幕显示的徽章,具体如下 :

      

   3、在程序中可以提示用于是否将应用放置于锁屏上,通过BackgroundExecutionManager.RequestAccessAsync()会出现一个对话框,当选择“allow”,程序将会放置于锁屏中,但锁屏应用最多只能有7个,当超过七个,会询问用户替换哪个应用。

   我们也可以手动将瓦片程序设置为锁屏应用:

     

   

   下面我们实现一个简单的定时推送信息至LockScreen APP:

   第一步:新建一个空白页面PushNotification.xaml,上面放置三个按钮,分别实现设置APP到LockScreen、开启后台定时推送消息、注销后台任务三个按钮

    

    第二步:新建一个后台任务类NotificationBackTask.cs文件,功能就是一人简单的定时器,实现定时推送,代码与前一篇的很相似,通过    BackgroundTaskProgressEventHandler去触发,目前我还没找到其它的方式去触发主程序,还望高手指教一下~

    第三步:设置主工程中的Package.appxmanifest文件

     

    同样需要在声明中添加后台任务 :

     

    第四步:实现PushNotification.xaml的后台代码
   点击”Set LockScreen App”按钮:   

    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;
            }}

  若APP没有加入LockScreen,会出现以下画面:

    

    点击“Send Badge BackTask”按钮,同样需要先注册task   

    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;

 

    task_Progress事件中实现消息的推送:   

 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))
          {//BadgeNumericNotificationContent 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;
            }
         });
}  

    

 

以上实现了两种推送,一种是简单badge,还有一种是Tile信息带文本信息的,对于后一种带详细信息,需要在PC Settings里手动设置,文本信息才会在LOCKSCREEN中显示,如下:

 

   以上就是LockScreen的简单介绍,可能还有些不足之处,还望高手指点~ 

 

posted on 2012-09-01 22:44  ShinyTang  阅读(2418)  评论(3编辑  收藏  举报

导航

作者:LucyTangLucyTang's Blog on 博客园
出处:http://www.cnblogs.com/jing870812/

本作品由LucyTang创作,采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。 任何转载必须保留完整文章,在显要地方显示署名以及原文链接。如您有任何疑问或者授权方面的协商,请给我留言