Longer battery life Always reachable apps VoIP IM Mail RTC apps that need to be always reachable.

Download Report

Transcript Longer battery life Always reachable apps VoIP IM Mail RTC apps that need to be always reachable.

Longer battery life
Always reachable apps
VoIP
IM
Mail
RTC apps that need to be always reachable
App
VoIP
IM
Mail
Network Trigger
System Trigger
Time Trigger
Background Task Infrastructure
OS
Building an always
reachable IM app
Building an IM app on Windows 8
VoIP
IM
Mail
Network
Trigger
System Trigger
Time Trigger
Background Task Infrastructure
Receive an IM when app is in the background
Receive an IM after user logs in
Execution Pattern
App
App registers for
trigger
OS waits
Trigger fires
Background task
executes
OS
Network Trigger
using Windows.Networking.Sockets;
StreamSocket sock = new StreamSocket();
Open your
socket
// Connect the streamsocket
…
// Create a Notification Channel with Push capability
NotificationChannel nc = new NotificationChannel(
"channelOne",
“MyApp.Background.KeepAliveTask",
“MyApp.Background.PushNotifyTask",
ServerKATimeInMins);
// Associate the socket with the notification channel
channelStatus = nc.UsingTransport(sock);
Add Control
Channel
metadata
Register your
socket
Network Trigger
using Windows.ApplicationModel.Background;
public sealed class PushNotifyTask : IBackgroundTask {
public void Run(IBackgroundTaskInstance taskInstance)
{
// Handle Incoming message
// Throwing a toast code
}
}
}
}
Network Trigger
using Windows.ApplicationModel.Background;
public sealed class KeepAliveTask : IBackgroundTask {
public void Run(IBackgroundTaskInstance taskInstance)
{
// Send Keepalive message to remote server
}
}}
}
}
System Trigger
using Windows.ApplicationModel.Background;
// Specify the trigger
IBackgroundTrigger trigger =
new SystemTrigger(SystemTriggerType.SessionStart, false);
Create user
login trigger
Associate
trigger with app
code
// Associate app code with the trigger
BackgroundTaskBuilder taskBuilder = new BackgroundTaskBuilder();
taskBuilder.TaskEntryPoint = “MyApp.Background.RegisterForUserLogin";
taskBuilder.SetTrigger(trigger);
taskBuilder.Name = “OnUserPresent";
// Register the task for background execution
IBackgroundTaskRegistration taskRegistration =
taskBuilder.Register();
Register trigger
System Trigger
using Windows.ApplicationModel.Background;
public sealed class RegisterForUserLogin: IBackgroundTask {
public void Run(IBackgroundTaskInstance taskInstance)
{
// Your app code to create notification channel
}
}
Always connected
Mobile broadband
(e.g. 3G, 4G, LTE, etc.)
Network Connectivity
Building a POP mail client
with periodic updates
Building a POP mail app on Windows 8
VoIP
IM
Mail
Network
Trigger
System Trigger
Time Trigger
Background Task Infrastructure
Periodic Mail Sync
Time Trigger
using Windows.ApplicationModel.Background;
Create time
trigger
// Specify the trigger
TimeTrigger trigger = new TimeTrigger(UpdateInterval, Periodic);
// Associate app code with the trigger
BackgroundTaskBuilder taskBuilder = new BackgroundTaskBuilder();
taskBuilder.TaskEntryPoint = “MyApp.Background.UpdateEmailTask";
taskBuilder.SetTrigger(trigger);
taskBuilder.Name = “TimerExpiry";
// Register the task for background execution
IBackgroundTaskRegistration taskRegistration =
taskBuilder.Register();
Associate
trigger with app
code
Register trigger
Time Trigger
using Windows.ApplicationModel.Background;
public sealed class UpdateEmailTask : IBackgroundTask {
public void Run(IBackgroundTaskInstance taskInstance)
{
// Your app code to update email
}
}
}
Review
Longer Battery Life
Always Reachable Apps
http://go.microsoft.com/fwlink/?LinkID=227329&clcid=0x409
[email protected]
http://forums.dev.windows.com
http://bldw.in/SessionFeedback
Backup
void AddToLockScreen() {
IAsyncOperation<LockScreenState> operation = LockScreen.AddToLockScreenAsync();
operation.Completed = OnAddToLockScreenOperationCompleted;
operation.Start();
}
void OnAddToLockScreenOperationCompleted(IAsyncOperation<LockScreenState> operation) {
if (operation.Status == AsyncStatus.Completed)
{
LockScreenState result = operation.GetResults();
switch (result)
{
case LockScreenState.NotOnLockScreen:
break;
case LockScreenState.OnLockScreen:
break;
case LockScreenState.DefaultNotOnLockScreen:
break;
}
}
else if (operation.Status == AsyncStatus.Error){
// Report error
}
}
System Event
using Windows.ApplicationModel.Background;
// Specify the trigger
IBackgroundTrigger trigger =
new SystemTrigger(SystemTriggerType.NetworkStateChange, false);
// Associate app code with the trigger
BackgroundTaskBuilder taskBuilder = new BackgroundTaskBuilder();
taskBuilder.TaskEntryPoint = “MyAppBackground.NetworkStateChangeTask";
taskBuilder.SetTrigger(trigger);
taskBuilder.Name = “OnNetworkStateChange";
// Register the task for background execution
IBackgroundTaskRegistration taskRegistration = taskBuilder.Register();
System Event
using Windows.ApplicationModel.Background;
public sealed class NetworkStateChangeTask : IBackgroundTask {
public void Run(IBackgroundTaskInstance taskInstance)
{
// Re-register Notification Channel
}
}