Advertisement
Guest User

Untitled

a guest
Apr 11th, 2025
9
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.72 KB | None | 0 0
  1. class TXApp extends Application.AppBase {
  2. var lastIsNetworkAvailable as Boolean = false;
  3.  
  4. function isNetworkAvailable() as Boolean {
  5. var settings = System.getDeviceSettings();
  6. if (settings has :connectionAvailable) {
  7. return settings.connectionAvailable;
  8. }
  9. return settings.phoneConnected;
  10. }
  11.  
  12. function checkNetworkAvailable() {
  13. var available = isNetworkAvailable();
  14. // network state changed
  15. if (lastIsNetworkAvailable != available) {
  16. setBackgroundEvent();
  17. }
  18. lastIsNetworkAvailable = available;
  19. }
  20.  
  21. // called in both FG and BG
  22. function initialize() {
  23. AppBase.initialize();
  24. // ...
  25. }
  26.  
  27. // only called in FG
  28. function getInitialView() {
  29. checkNetworkAvailable();
  30. setBackgroundEvent();
  31. // ...
  32. }
  33.  
  34. function setBackgroundEvent() {
  35. if (Toybox.System has :ServiceDelegate) {
  36. var updatePeriod = Application.Properties.getValue("WU") as Number?;
  37. if (updatePeriod == null) {
  38. updatePeriod = 0; // or whatever the default setting value is
  39. }
  40. if (updatePeriod < 5) {
  41. // weather updates are disabled
  42. Background.deleteTemporalEvent();
  43. return;
  44. }
  45. // if network is not connected, use default of 5 minutes
  46. if (!lastIsNetworkAvailable) {
  47. updatePeriod = 5;
  48. }
  49.  
  50. Background.registerForTemporalEvent(new Time.Duration(updatePeriod * 60));
  51. }
  52. }
  53.  
  54. function onUpdate(dc as Dc) {
  55. checkNetworkAvailable();
  56. // ...
  57. }
  58. // ...
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement