mydexterid

BackgroundService

Nov 15th, 2017
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.86 KB | None | 0 0
  1. using Toybox.Background;
  2. using Toybox.System;
  3. using Toybox.Communications as Comm;
  4.  
  5. (:background)
  6. class GetDataListener extends Comm.ConnectionListener
  7. {
  8. function initialize()
  9. {
  10. Comm.ConnectionListener.initialize();
  11. }
  12.  
  13. public function onComplete()
  14. {
  15. System.println("oncomplete");
  16. }
  17.  
  18. public function onError()
  19. {
  20. System.println("onerror");
  21. Background.exit("error");
  22. }
  23. }
  24.  
  25. (:background)
  26. class BackgroundServiceDelegate extends System.ServiceDelegate {
  27. var communications;
  28. function initialize()
  29. {
  30. $.is_background = true;
  31. System.println("BackgroundServiceDelegate::nitialize");
  32. //var communications = new MyComms();
  33. ServiceDelegate.initialize();
  34. //Comm.registerForPhoneAppMessages(method(:on_received));
  35. }
  36.  
  37. function onTemporalEvent()
  38. {
  39. var time = System.getClockTime();
  40. System.println(Lang.format("BackgroundServiceDelegate::onTemporalEvent: $1$:$2$:$3$", [time.hour, time.min, time.sec]));
  41.  
  42. //Background.requestApplicationWake("Your timer has expired!");
  43. if (System.getDeviceSettings().phoneConnected)
  44. {
  45. Comm.registerForPhoneAppMessages(method(:on_received));
  46. //communications.enqueue(Communications.MESSAGE_TYPE_GET_DATA);
  47. System.println("BackgroundServiceDelegate::onTemporalEvent - before transmit");
  48. Comm.transmit(MyComms.MESSAGE_TYPE_GET_DATA, null, new GetDataListener());
  49. System.println("BackgroundServiceDelegate::onTemporalEvent - after transmit");
  50. }
  51. else
  52. {
  53. Background.exit(0);
  54. }
  55.  
  56. }
  57.  
  58. function on_received(msg)
  59. {
  60. var time = System.getClockTime();
  61. System.println(Lang.format("BackgroundServiceDelegate::on_received: $1$:$2$:$3$", [time.hour, time.min, time.sec]));
  62.  
  63. Background.exit(msg.data);
  64. }
  65.  
  66. }
Advertisement
Add Comment
Please, Sign In to add comment