follow-app BOOTCAMP 2: Windows phone fast application switching


Published on

Published in: Education, Technology
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

follow-app BOOTCAMP 2: Windows phone fast application switching

  1. 1. Fast ApplicationSwitching &Tombstoning
  2. 2. Topics The Windows Phone execution model Application State Management Fast Application Switching Dormant programs and Tombstoning Application Navigation and Application Switching2 Windows Phone
  3. 3. DemoDemo 1:Fast ApplicationSwitching
  4. 4. Application Lifecycle - Dormant Fast App Resume runningState preserved!e.IsApplicationInstancePreserved Save State!== true activated deactivated dormant Phone resources detached Threads & timers suspended 4 Windows Phone
  5. 5. Application Lifecycle - Tombstoned Resuming .. .Restore state!e.IsApplicationInstancePreserved== false running activated deactivated Tombstone the oldest app Tombstoned dormant Phone resources detached Threads & timers suspended 5 Windows Phone
  6. 6. Methods & Events6 Windows Phone 6
  7. 7. Finding the Resume type private void Application_Activated(object sender, ActivatedEventArgs e) { if (e.IsApplicationInstancePreserved) { // Dormant - objects in memory intact } else { // Tombstoned - need to reload } } The Activation handler can test a flag to determine the type of resume taking place7 Windows Phone
  8. 8. 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 Disposed8 Windows Phone
  9. 9. 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
  10. 10. 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 data1 Windows Phone 100
  11. 11. Captain’s LogDemo• With No Storage• With Storage• Fully Working
  12. 12. Fast App Switching andTombstoning 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 store1 Windows Phone 122
  13. 13. Background Tasks
  14. 14. Multitasking Capabilities Background Agents  Periodic  Resource Intensive Background Transfer Service Alarms and Reminders Background Audio1 Windows Phone 144
  15. 15. 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 Windows Phone5
  16. 16. Generic Agent TypesPeriodic Agents Resource Intensive Occurrence Agents  Every 30 min  Occurrence Duration  External power  ~15 seconds  Non-cell network Constraints  Duration  <= 6 MB Memory  10 minutes  <=10% CPU  Constraints  <= 6 MB Memory1 Windows Phone6
  17. 17. Background Agent Functionality Allowed Restricted Tiles Toast  Display UI Location  XNA libraries Network  Microphone and Camera R/W ISO store  Sensors Sockets  Play audio (may only use background audio APIs) Most framework APIs1 Windows Phone7
  18. 18. DemoDemo1: Captain’s Location Log
  19. 19. Debugging a Background Task #if DEBUG_AGENT ScheduledActionService.LaunchForTest(taskName, TimeSpan.FromSeconds(60)); #endif 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 built1 Windows Phone 199
  20. 20. 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 ResourceIntensiveAgents2 Windows Phone 200
  21. 21. DemoDemo2: Debugging Tasks
  22. 22. 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 transfers2 Windows Phone 222
  23. 23. 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 transfer2 Windows Phone 233
  24. 24. 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: Windows Phone4
  25. 25. DemoDemo3: Picture Fetch
  26. 26. Scheduled Notifications Time-based, on-phone notifications Supports Alerts & Reminders Persist across reboots Adheres to user settings Consistent with phone UX2 Windows Phone 266
  27. 27. Alarms vs Reminders? Alarms Remind ers• Modal • Rich information• Snooze and Dismiss • Integrates with other• Sound customization reminders• No app invocation • Snooze and Dismiss• No stacking • Launch app • Follows the phones global 27 settings
  28. 28. Creating a Reminder 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); 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 application2 Windows Phone 288
  29. 29. Reminder Housekeeping Reminder eggReminder = ScheduledActionService.Find("Egg Timer") as Reminder; if ( eggReminder != null ) { ScheduledActionService.Remove("Egg Timer"); } Reminders are identified by name This code finds the “Egg Timer” reminder and then removes it from the scheduler2 Windows Phone 299
  30. 30. DemoDemo4: Egg Timer
  31. 31. 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 storage3 Windows Phone 311
  32. 32. 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 registration3 Windows Phone 322
  33. 33. 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 applications3 Windows Phone 333