Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class TXApp extends Application.AppBase {
- var lastIsNetworkAvailable as Boolean = false;
- function isNetworkAvailable() as Boolean {
- var settings = System.getDeviceSettings();
- if (settings has :connectionAvailable) {
- return settings.connectionAvailable;
- }
- return settings.phoneConnected;
- }
- function checkNetworkAvailable() {
- var available = isNetworkAvailable();
- // network state changed
- if (lastIsNetworkAvailable != available) {
- setBackgroundEvent();
- }
- lastIsNetworkAvailable = available;
- }
- // called in both FG and BG
- function initialize() {
- AppBase.initialize();
- // ...
- }
- // only called in FG
- function getInitialView() {
- checkNetworkAvailable();
- setBackgroundEvent();
- // ...
- }
- function setBackgroundEvent() {
- if (Toybox.System has :ServiceDelegate) {
- var updatePeriod = Application.Properties.getValue("WU") as Number?;
- if (updatePeriod == null) {
- updatePeriod = 0; // or whatever the default setting value is
- }
- if (updatePeriod < 5) {
- // weather updates are disabled
- Background.deleteTemporalEvent();
- return;
- }
- // if network is not connected, use default of 5 minutes
- if (!lastIsNetworkAvailable) {
- updatePeriod = 5;
- }
- Background.registerForTemporalEvent(new Time.Duration(updatePeriod * 60));
- }
- }
- function onUpdate(dc as Dc) {
- checkNetworkAvailable();
- // ...
- }
- // ...
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement