follow-app bootcamp 2: windows phone fast application switching
TRANSCRIPT
Fast Application Switching & Tombstoning
Windows Phone
Topics
The Windows Phone execution model
Application State Management
Fast Application Switching
Dormant programs and Tombstoning
Application Navigation and Application Switching
2
Demo
Demo 1: Fast Application Switching
Windows Phone
Application Lifecycle - Dormant
running
deactivated
dormant
activated
Phone resources
detached
Threads & timers
suspended
Fast App Resume
Save State! State preserved! e.IsApplicationInstancePreserved
== true
4
Windows Phone
Application Lifecycle - Tombstoned
running
deactivated
dormant Phone resources detached
Threads & timers suspended
Restore state! e.IsApplicationInstancePreserved
== false
Resuming .. .
Tombstone
the oldest
app
Tombstoned
activated
5
Windows Phone
Methods & Events
6 6
Windows Phone
Finding the Resume type
The Activation handler can test a flag to determine the type of resume taking place
private void Application_Activated(object sender, ActivatedEventArgs e) { if (e.IsApplicationInstancePreserved) { // Dormant - objects in memory intact } else { // Tombstoned - need to reload } }
7
Windows Phone
Deactivation Resource Management
MediaPlayer.Pause
MediaElement.Pause
SoundEffectInstance.Pause
VibrateController.Stop
PhotoCamera.Dispose
Save page/global state
XNA Audio Paused
Sensors Notifications suppressed
Networking Cancelled
Sockets Disconnected
MediaElement Disconnected
Camera Disposed
8
Windows Phone
Activation Resource Management
MediaElement.Source/Position/
Play
Socket.ConnectAsync
new PhotoCamera/VideoCamera
Restore app state if tombstoned
XNA Audio Resumed
Sensors Notifications resumed
Networking Completed with Cancellation
Sockets -
MediaElement -
Camera -
9
Windows Phone
Isolated Storage vs State Storage
Isolated storage is so called because the data for an application is isolated from all other applications
It can be used as filestore where an application can store folders and files
It is slow to access, since it is based on NVRAM technology
It can also be used to store name/value pairs, e.g. program settings
State storage is so called because it is used to hold the state of an application
It can be used to store name/value pairs which are held in memory for dormant or tombstoned applications
It provides very quick access to data
10 1
0
Demo
• With No Storage • With Storage • Fully Working
Captain’s Log
Windows Phone
Fast App Switching and
Tombstoning Review
Only one Windows Phone application is Active at any time
The Start and Back buttons on the phone are used to start new applications and return to previously used ones
If an application is replaced by another it is either made Dormant (still in memory but not running) or Tombstoned (removed from memory)
Applications must use populate methods provided in the App.xaml.cs class to save and retrieve state information when appropriate
State can be stored in memory for quick reload and in isolated storage which serve as a permanent store
12 1
2
Background Tasks
Windows Phone
Multitasking Capabilities
Background Agents
Periodic
Resource Intensive
Background Transfer Service
Alarms and Reminders
Background Audio
14 1
4
Windows Phone
Background Agents
Agents
Periodic
Resource Intensive
An app may have up to one of each
Initialized in foreground, run in background
Persisted across reboots
User control through CPL
System maximum of 18 periodic agent
Agent runs for up to 14 days (can be renewed)
1
5
Generic Agent Types
Periodic Agents
Occurrence
Every 30 min
Duration
~15 seconds
Constraints
<= 6 MB Memory
<=10% CPU
Resource Intensive Agents
Occurrence
External power
Non-cell network
Duration
10 minutes
Constraints
<= 6 MB Memory
1
6 Windows Phone
Windows Phone
Background Agent Functionality
Allowed
Tiles Toast Location Network R/W ISO store Sockets Most framework APIs
Restricted
Display UI XNA libraries Microphone and Camera Sensors Play audio
(may only use background audio APIs)
1
7
Demo
Demo1: Captain’s Location Log
Windows Phone
Debugging a Background Task
It would be annoying if we had to wait 30 minutes to get code in the agent running so we could debug it
When we are debugging we can force service to launch itself
Such code can be conditionally compiled and removed before the production version is built
19
#if DEBUG_AGENT ScheduledActionService.LaunchForTest(taskName, TimeSpan.FromSeconds(60)); #endif
1
9
Windows Phone
Debugging the Agent Code
When you use the Back button or Start on the phone to interrupt an application with an active Background Task ,Visual Studio does not stop running
It remains attached to the application
You can then put breakpoints into the background task application and debug them as you would any other program
You can single step, view the contents of variables and even change them using the Immediate Window
This is also true if you are working on a device rather than the emulator
The same techniques work on ResourceIntensiveAgents
20 2
0
Demo
Demo2: Debugging Tasks
Windows Phone
File Transfer Tasks
It is also possible to create a background task to transfer files to and from your application’s isolated storage
The transfers will continue to work even when the application is not running
An application can monitor the state of the downloads and display their status
Files can be fetched from HTTP or HTTPs hosts
At the moment FTP is not supported
The system maintains a queue of active transfers and services each one in turn
Applications can query the state of active transfers
22 2
2
Windows Phone
Background Transfer Policies
There are a set of policies that control transfer behaviour
Maximum Upload file size: 5Mb
Maximum Download file size over cellular (mobile phone) data: 20Mb
Maximum Download file size over WiFi: 100Mb
These can be modified by setting the value of TransferPreferences on a particular transfer
23 2
3
Windows Phone
Transfer Management
An application can find out how many file transfers it has active
It will have to do this when it is restarted, as file transfers will continue even when the application is not running
It can then perform transfer management as required
There is a good example of transfer list management on MSDN:
http://msdn.microsoft.com/en-us/library/hh202953.aspx
2
4
Demo
Demo3: Picture Fetch
Windows Phone
Scheduled Notifications
Time-based, on-phone notifications
Supports Alerts & Reminders
Persist across reboots
Adheres to user settings
Consistent with phone UX
26 2
6
Alarms vs Reminders?
Alarms
27
Reminders
• Modal
• Snooze and Dismiss
• Sound customization
• No app invocation
• No stacking
• Rich information
• Integrates with other
reminders
• Snooze and Dismiss
• Launch app
• Follows the phones global
settings
Windows Phone
Creating a Reminder
This code creates a reminder and adds it as a scheduled service
The value eggTime holds the length of the delay
This code also sets the url of the page in the application
28
using Microsoft.Phone.Scheduler; ... eggReminder = new Reminder("Egg Timer"); eggReminder.BeginTime = DateTime.Now + new TimeSpan(0, eggTime, 0); eggReminder.Content = "Egg Ready"; eggReminder.RecurrenceType = RecurrenceInterval.None; eggReminder.NavigationUri = new Uri("/EggReadyPage.xaml", UriKind.Relative); ScheduledActionService.Add(eggReminder);
2
8
Windows Phone
Reminder Housekeeping
Reminders are identified by name
This code finds the “Egg Timer” reminder and then removes it from the scheduler
29
Reminder eggReminder = ScheduledActionService.Find("Egg Timer") as Reminder; if ( eggReminder != null ) { ScheduledActionService.Remove("Egg Timer"); }
2
9
Demo
Demo4: Egg Timer
Windows Phone
Audio Playback Agents
It is also possible to create an Audio Playback Agent that will manage an application controlled playlist
The mechanism is the same as for other background tasks
The audio can be streamed or held in the application isolated storage
31 3
1
Windows Phone
Background Audio
Playback
App provides URL or stream to Zune
Audio continues to play even if app is closed
App is notified of file or buffer near completion
Phone Integration
Music & Video Hub
Universal Volume Control (UVC), lauch app, controls, contextual info
Contextual launch – Start menu, UVC, Music & Video Hub
App Integration
App can retrieve playback status, progress, & metadata
Playback notification registration
32 3
2
Windows Phone
Review
An application can create background processes
Periodic Task and ResourceIntensive task run when the application is stopped
Scheduled notifications will fire whether the application is running or not
Audio Playback run alongside the application
Applications and their background processes can communicate via isolated storage
Visual Studio can be used to debug background tasks in the same way as foreground applications
33 3
3