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的簡單介紹,可能還有些不足之處,還望高手指點~ 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM