Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Service_Android : Java.Lang.Object, IService
- {
- Intent SyncIntent;
- User anUser;
- public void StartService()
- {
- ScheduleSyncService();
- //MainActivity test = new MainActivity();
- //test.ScheduleSyncService();
- }
- public void ScheduleSyncService()
- {
- if (!IsAlarmSet())
- {
- Calendar calendar = Calendar.GetInstance(Java.Util.TimeZone.Default);
- DateTime today = DateTime.Today;
- DateTime tomorrow = DateTime.Today.AddDays(1);
- TimeSpan ts = new TimeSpan(anUser.horaNotification, anUser.minutosNotification, 00);
- today = today.Date + ts;
- calendar.Set(today.Year, today.Month, today.Day, anUser.horaNotification, anUser.minutosNotification, 00);
- Calendar rightNow = Calendar.GetInstance(Java.Util.TimeZone.Default);
- var alarm = (AlarmManager)Forms.Context.GetSystemService(Context.AlarmService);
- var pendingServiceIntent = PendingIntent.GetService(Forms.Context, 0, SyncIntent, 0);
- //var test = SystemClock.ElapsedRealtime() + calendar.TimeInMillis;
- var trigeredTime = (long)today.ToUniversalTime().Subtract(
- new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)
- ).TotalMilliseconds;
- if (rightNow.TimeInMillis < trigeredTime)
- {
- alarm.SetRepeating(AlarmType.RtcWakeup, trigeredTime, AlarmManager.IntervalDay, pendingServiceIntent);
- }
- else
- {
- TimeSpan tsTmorrow = new TimeSpan(anUser.horaNotification, anUser.minutosNotification, 00);
- tomorrow = tomorrow.Date + tsTmorrow;
- var tomorrowMilis = (long)tomorrow.ToUniversalTime().Subtract(
- new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)
- ).TotalMilliseconds;
- alarm.SetRepeating(AlarmType.RtcWakeup, tomorrowMilis, AlarmManager.IntervalDay, pendingServiceIntent);
- }
- }
- else
- {
- System.Console.WriteLine("La alarma ya esta configurada");
- }
- }
- public bool IsAlarmSet()
- {
- //Exception thrown here!
- return PendingIntent.GetBroadcast(Forms.Context.ApplicationContext, 0, SyncIntent, PendingIntentFlags.NoCreate) != null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement