Windows Phone 8.1 e i background tasks - Andrea Boschin - Codemotion Milan 2014

21
MILAN november 28 th /29 th , 2014 Windows Phone 8.1 e i background tasks [email protected] – Elite Agency Andrea Boschin

description

Una delle richieste ricorrenti dalle applicazioni mobile moderne è quella di gestire gli eventi che arrivano al dispositivo per trattare queste informazioni in termini di tracking, e di notifiche. Windows Phone 8.1 mette a disposizione dei background task che consentono di gestire questo tipo di scenari con un consumo minimo delle risorse del device. In questa sessione vedremo come funzionano i background task e questo tipo in particolare.

Transcript of Windows Phone 8.1 e i background tasks - Andrea Boschin - Codemotion Milan 2014

Page 1: Windows Phone 8.1 e i background tasks - Andrea Boschin - Codemotion Milan 2014

MILAN november 28th/29th, 2014

Windows Phone 8.1 e i background tasks

[email protected] – Elite Agency

Andrea Boschin

Page 2: Windows Phone 8.1 e i background tasks - Andrea Boschin - Codemotion Milan 2014

MILAN november 28th/29th, 2014 – Andrea Boschin

{ Background Tasks?cosa si intende?

Page 3: Windows Phone 8.1 e i background tasks - Andrea Boschin - Codemotion Milan 2014

MILAN november 28th/29th, 2014 – Andrea Boschin

{ Background Tasks?cosa si intende?

esecuzione di codice in background, anche quando l’applicazione non è in esecuzione allo scopo di effettuare aggiornamenti, notifiche e altre operazioni similari.

Page 4: Windows Phone 8.1 e i background tasks - Andrea Boschin - Codemotion Milan 2014

MILAN november 28th/29th, 2014 – Andrea Boschin

Multitasking in Windows Phone

– Live Tiles/Toasts • Aggiornamento via Push Notifications

– Scheduled Toasts and Notifications• Aggiornamenti e notifiche periodiche

– OS brokered tasks• Attività svolte in background dal sistema operativo

– Background Tasks and agents• Veri e propri task eseguiti in parallelo

Page 5: Windows Phone 8.1 e i background tasks - Andrea Boschin - Codemotion Milan 2014

MILAN november 28th/29th, 2014 – Andrea Boschin

• Evoluzione

– L’introduzione di WinRT ha cambiato le cose

• Silverlight: Background Agents– Limitati a pochi scenari– Hanno una scadenza (max due settimane)– Limitati esclusivamente a Windows Phone– Tutt’ora presenti per compatibilità

• WinRT: Background Tasks– Danno molte maggiori possibilità (es: trigger e condition)– Non scadono– Disponibili ugualmente anche per Windows Store Apps (Windows

8.1)– Universal Application model

Page 6: Windows Phone 8.1 e i background tasks - Andrea Boschin - Codemotion Milan 2014

MILAN november 28th/29th, 2014 – Andrea Boschin

Cosa è un background task?

– Attività in condizioni controllate• Tempo di esecuzione limitato

– Ospitata in un processo separato• Può essere eseguita anche se l’applicazione non è in

esecuzione– Attivata in risposta a specifici eventi e condizioni

• L’attività è legata a Trigger e Conditions

•Perchè?– Il sistema operativo tutela l’utilizzo delle risorse– Prima di tutto il consumo della batteria

Page 7: Windows Phone 8.1 e i background tasks - Andrea Boschin - Codemotion Milan 2014

MILAN november 28th/29th, 2014 – Andrea Boschin

Implementare un Background task

– E’ necessario che sia ospitato in un Windows Runtime Component• Attenzione: Ci sono limitazioni per il fatto che i

componenti devono poter essere utilizzabili da Javascript

– Implementare una interfaccia IBackgroundTask• Dispone di un unico metodo Run() eseguito quando

il task deve svolgere il proprio compito

Page 8: Windows Phone 8.1 e i background tasks - Andrea Boschin - Codemotion Milan 2014

MILAN november 28th/29th, 2014 – Andrea Boschin

Registrazione

– Il task deve essere dichiarato nel manifest

– Nella dichiarazione va specificato il tipo di task e l’entry point• Attenzione alla specifica corretta!

– Non è sufficiente dichiararlo nel manifest. Occorre registrarlo a runtime con il BackgroundTaskBuilder• Importante nome e Entry Point siano corretti e corrispondano a

quelli del manifest

– Il task può essere dichiarato ovunque nel codice. • All’avvio dell’applicazione• In risposta a una azione dell’utente

Page 9: Windows Phone 8.1 e i background tasks - Andrea Boschin - Codemotion Milan 2014

MILAN november 28th/29th, 2014 – Andrea Boschin

Deregistrare?

– Usare BackgroundTaskRegistration.AllTasks per trovare i task registrati

– Ogni istanza ha un metodo Unregister

– Importante: Prima di registrare un task verificare che esso non sia già registrato e rimuovere la precedente istanza

Page 10: Windows Phone 8.1 e i background tasks - Andrea Boschin - Codemotion Milan 2014

MILAN november 28th/29th, 2014 – Andrea Boschin

Background task Opt-in

– E’ sempre necessario verificare che l’utente acconsenta al task di essere eseguito

– L’utente può cambiare il tipo di accesso in ogni momento dai settings

– Usare BackgroundExecutionManager.RequestAccessAsync()

– La verifica è obbligatoria per prevenire eccezioni quando l’utente abbia cambiato la modalità di esecuzione in background

Page 11: Windows Phone 8.1 e i background tasks - Andrea Boschin - Codemotion Milan 2014

MILAN november 28th/29th, 2014 – Andrea Boschin

Triggers

– Definiscono l’evento che darà l’avvio al processo in background

– Solo un trigger per ogni task

– Tipi di base:• SystemTrigger

– Esecuzione in risposta ad eventi di sistema

• TimeTrigger– Esecuzione ad intervalli di tempo prestabiliti

• MaintenanceTrigger– Esegue attività quando il device è connesso alla rete elettrica (sotto carica)

Page 12: Windows Phone 8.1 e i background tasks - Andrea Boschin - Codemotion Milan 2014

MILAN november 28th/29th, 2014 – Andrea Boschin

• Conditions

– Definiscono le condizioni che devono essere presenti perchè il task possa essere eseguito

– Sono indipendenti dal Trigger usato

– Ogni task può avere più condizioni

– I seguenti tipi:• BackgroundWorkCostNotHigh, FreeNetworkAvailable,

InternetAvailable, InternetNotAvailable, Invalid, SessionConnected, SessionDisconnected, UserNotPresent, UserPresent

Page 13: Windows Phone 8.1 e i background tasks - Andrea Boschin - Codemotion Milan 2014

MILAN november 28th/29th, 2014 – Andrea Boschin

{ DemoSimple Background Task

Page 14: Windows Phone 8.1 e i background tasks - Andrea Boschin - Codemotion Milan 2014

MILAN november 28th/29th, 2014 – Andrea Boschin

Cosa posso fare in un BackgroundTask

– Prima di tutto rispettare i seguenti limiti

– Meglio usare un «deferral» per comunicare al O.S. Quando abbiamo completato il lavoro• Ma non ci esime dai limiti!

CPU Refresh

Windows App non in Lock Screen 1 secondo 2 ore

Windows App in lock screen 2 secondi 15 minuti

Windows Phone App 2 secondi 15 minuti

Page 15: Windows Phone 8.1 e i background tasks - Andrea Boschin - Codemotion Milan 2014

MILAN november 28th/29th, 2014 – Andrea Boschin

• E quindi cosa posso fare?

– Aggiornare le Tile– Inviare Toast notification– Richiedere la posizione– Salvare/aggiornare file– Chiamare servizi remoti– … altro

– NON posso mai interagire con l’utente mediante elementi di interfaccia!

Page 16: Windows Phone 8.1 e i background tasks - Andrea Boschin - Codemotion Milan 2014

MILAN november 28th/29th, 2014 – Andrea Boschin

Altri tipi di Trigger

– PushNotification• Attivazione del task in risposta a notifiche dal WNS

(Windows Notification Service)

– BlueTooth, RF Comm e Sensori• GattCharacteristicNotificationTrigger• DeviceChangeTrigger• DeviceUpdateTrigger• RfcommConnectionTrigger

Page 17: Windows Phone 8.1 e i background tasks - Andrea Boschin - Codemotion Milan 2014

MILAN november 28th/29th, 2014 – Andrea Boschin

Altri tipi di Trigger

– LocationTrigger• Associato al GeoFencing

• Consente di ricevere notifiche in risposta ad eventi relativi ai GeoFence definiti

• Eventi: Ingresso, Uscita, Rimozione

Page 18: Windows Phone 8.1 e i background tasks - Andrea Boschin - Codemotion Milan 2014

MILAN november 28th/29th, 2014 – Andrea Boschin

XamlRenderingBackgroundTask

– Si tratta di un particolare tipo di background task

– Consente di creare un VisualTree a partire dallo XAML

– Con RenderTargetBitmap si può convertire il VisualTree in immagine

– Il contenuto può essere usato ad esempio per aggiornare una tile

Page 19: Windows Phone 8.1 e i background tasks - Andrea Boschin - Codemotion Milan 2014

MILAN november 28th/29th, 2014 – Andrea Boschin

{ DemoXamlRenderingBackgroundTask

Page 20: Windows Phone 8.1 e i background tasks - Andrea Boschin - Codemotion Milan 2014

MILAN november 28th/29th, 2014 – Andrea Boschin

• Debugging di un Background Task

– Visual Studio offre uno strumento ottimo per il debug

– Consente di attivarli in ogni momento senza attendere che l’evento si verifichi

– Non si applica ai tutti i tipi di SystemTrigger

Page 21: Windows Phone 8.1 e i background tasks - Andrea Boschin - Codemotion Milan 2014

MILAN november 28th/29th, 2014 – Andrea Boschin

Andrea Boschin

http://blog.boschin.it

https://www.facebook.com/thelittlegrove

@aboschin

thesmallgrove

[email protected]