Windows 8 学习笔记(十九)--.后台任务BackgroundTask(II)
BackgroundTask and LockScreen
一般LockScreen的信息包括以下几部分:
3、在程序中可以提示用于是否将应用放置于锁屏上,通过BackgroundExecutionManager.RequestAccessAsync()会出现一个对话框,当选择“allow”,程序将会放置于锁屏中,但锁屏应用最多只能有7个,当超过七个,会询问用户替换哪个应用。
下面我们实现一个简单的定时推送信息至LockScreen APP:
第二步:新建一个后台任务类NotificationBackTask.cs文件,功能就是一人简单的定时器,实现定时推送,代码与前一篇的很相似,通过 BackgroundTaskProgressEventHandler去触发,目前我还没找到其它的方式去触发主程序,还望高手指教一下~
同样需要在声明中添加后台任务 :
第四步:实现PushNotification.xaml的后台代码
点击”Set LockScreen App”按钮:
{
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.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事件中实现消息的推送:
{
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 阅读(2423) 评论(3) 编辑 收藏 举报