Advertisement
Guest User

Untitled

a guest
Jul 28th, 2014
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.54 KB | None | 0 0
  1. class Service_Android : Java.Lang.Object, IService
  2. {
  3. Intent SyncIntent;
  4. User anUser;
  5. public void StartService()
  6. {
  7. ScheduleSyncService();
  8. //MainActivity test = new MainActivity();
  9. //test.ScheduleSyncService();
  10. }
  11.  
  12. public void ScheduleSyncService()
  13. {
  14.  
  15. if (!IsAlarmSet())
  16. {
  17. Calendar calendar = Calendar.GetInstance(Java.Util.TimeZone.Default);
  18. DateTime today = DateTime.Today;
  19. DateTime tomorrow = DateTime.Today.AddDays(1);
  20. TimeSpan ts = new TimeSpan(anUser.horaNotification, anUser.minutosNotification, 00);
  21. today = today.Date + ts;
  22.  
  23. calendar.Set(today.Year, today.Month, today.Day, anUser.horaNotification, anUser.minutosNotification, 00);
  24.  
  25. Calendar rightNow = Calendar.GetInstance(Java.Util.TimeZone.Default);
  26.  
  27. var alarm = (AlarmManager)Forms.Context.GetSystemService(Context.AlarmService);
  28. var pendingServiceIntent = PendingIntent.GetService(Forms.Context, 0, SyncIntent, 0);
  29. //var test = SystemClock.ElapsedRealtime() + calendar.TimeInMillis;
  30. var trigeredTime = (long)today.ToUniversalTime().Subtract(
  31. new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)
  32. ).TotalMilliseconds;
  33.  
  34. if (rightNow.TimeInMillis < trigeredTime)
  35. {
  36. alarm.SetRepeating(AlarmType.RtcWakeup, trigeredTime, AlarmManager.IntervalDay, pendingServiceIntent);
  37. }
  38. else
  39. {
  40. TimeSpan tsTmorrow = new TimeSpan(anUser.horaNotification, anUser.minutosNotification, 00);
  41. tomorrow = tomorrow.Date + tsTmorrow;
  42. var tomorrowMilis = (long)tomorrow.ToUniversalTime().Subtract(
  43. new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)
  44. ).TotalMilliseconds;
  45. alarm.SetRepeating(AlarmType.RtcWakeup, tomorrowMilis, AlarmManager.IntervalDay, pendingServiceIntent);
  46. }
  47.  
  48. }
  49. else
  50. {
  51. System.Console.WriteLine("La alarma ya esta configurada");
  52. }
  53. }
  54.  
  55. public bool IsAlarmSet()
  56. {
  57. //Exception thrown here!
  58. return PendingIntent.GetBroadcast(Forms.Context.ApplicationContext, 0, SyncIntent, PendingIntentFlags.NoCreate) != null;
  59. }
  60.  
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement