public static void ProgramNextAlarm(Context context) { ISharedPreferences prefs = PreferenceManager.GetDefaultSharedPreferences(context); int hour = prefs.GetInt(General.ALARM_HOUR, DateTime.Now.Hour); int minute = prefs.GetInt(General.ALARM_MINUTE, DateTime.Now.Minute); int seconds = TimeCalculator.GetSecondsUntilNextCheck(new DateTime(1984, 10, 12, hour, minute, 00)); //GET TIME IN SECONDS AND INITIALIZE INTENT Intent i = new Intent(context, typeof(AlarmBroadcastReceiver)); //PASS CONTEXT,YOUR PRIVATE REQUEST CODE,INTENT OBJECT AND FLAG Android.App.PendingIntent pi = Android.App.PendingIntent.GetBroadcast(context, 1, i, 0); //INITIALIZE ALARM MANAGER Android.App.AlarmManager alarmManager = (Android.App.AlarmManager)context.GetSystemService(Context.AlarmService); //SET THE ALARM long triggermillis = Java.Lang.JavaSystem.CurrentTimeMillis() + (seconds * 1000); if (Build.VERSION.SdkInt >= BuildVersionCodes.M) { alarmManager.SetAndAllowWhileIdle(Android.App.AlarmType.RtcWakeup, triggermillis, pi); } else //if (Build.VERSION.SdkInt >= BuildVersionCodes.Kitkat) { alarmManager.SetExact(Android.App.AlarmType.RtcWakeup, triggermillis, pi); } //else // alarmManager.Set(Android.App.AlarmType.RtcWakeup, triggermillis, pi); }
private void HowLongToNext() { bool alarmActive = prefs.GetBoolean(General.ALARM_ACTIVE, false); int hour = prefs.GetInt(General.ALARM_HOUR, DateTime.Now.Hour); int minute = prefs.GetInt(General.ALARM_MINUTE, DateTime.Now.Minute); if (alarmActive) { int seconds = TimeCalculator.GetSecondsUntilNextCheck(new DateTime(1984, 10, 12, hour, minute, 00)); TimeSpan timeSpan = new TimeSpan(0, 0, seconds); tvHowLong.Text = ((timeSpan.Days > 0 ? (timeSpan.Days.ToString() + " " + Resources.GetString(Resource.String.auto_days)) : string.Empty) + " ").TrimStart() + ((timeSpan.Hours > 0 ? (timeSpan.Hours + " " + Resources.GetString(Resource.String.auto_hours)) : string.Empty) + " ").TrimStart() + ((timeSpan.Minutes > 0 ? (timeSpan.Minutes + " " + Resources.GetString(Resource.String.auto_minutes)) : string.Empty) + " ").TrimStart() + ((timeSpan.Seconds > 0 ? (timeSpan.Seconds + " " + Resources.GetString(Resource.String.auto_seconds)) : string.Empty) + " ").TrimStart() + Resources.GetString(Resource.String.auto_until_next); } else { tvHowLong.Text = string.Empty; } }