C# 如何在xamarin android中使用DateTime对象为特定日期和时间设置报警?
我正在android xamarin中使用alarm manager,我有一个日期时间对象,我想为该特定日期时间设置一个警报。目前,我正在使用以下代码设置相同天分钟的警报C# 如何在xamarin android中使用DateTime对象为特定日期和时间设置报警?,c#,android,xamarin,alarmmanager,C#,Android,Xamarin,Alarmmanager,我正在android xamarin中使用alarm manager,我有一个日期时间对象,我想为该特定日期时间设置一个警报。目前,我正在使用以下代码设置相同天分钟的警报 alarmIntent.PutExtra("title", reminder.Title); alarmIntent.PutExtra("description", reminder.Description); int _id = reminder.ID;
alarmIntent.PutExtra("title", reminder.Title);
alarmIntent.PutExtra("description", reminder.Description);
int _id = reminder.ID;
PendingIntent pendingIntent = PendingIntent.GetBroadcast(Activity, _id, alarmIntent, PendingIntentFlags.UpdateCurrent);
AlarmManager alarmManager = (AlarmManager)Activity.GetSystemService(Context.AlarmService);
DateTime dt = DateTime.ParseExact(reminder.ReminderTime, "yyyy.MM.dd HH:mm", CultureInfo.InvariantCulture);
var totalMinutes = dt.Minute - DateTime.Now.Minute;
var time = (totalMinutes * 60) * 1000;
alarmManager.Set(AlarmType.ElapsedRealtimeWakeup, SystemClock.ElapsedRealtime() + time, pendingIntent);```
above code is working fine i just want to set alarm on a specific time.
上面的代码解决了我的问题
上面的代码解决了我的问题你所说的特定时间是什么意思?如问题中所述,我有一个datetime类型的对象,那就是特定datetime你所说的特定时间是什么意思?如问题中所述,我有一个datetime类型的对象,这是特定的日期时间Thankyu@Jason你能解释为什么是1970年吗?Unix时间戳使用1/1/1970作为起始点Thankyu@Jason你能解释为什么是1970年吗?Unix时间戳使用1/1/1970作为起始点
DateTime date = // set some date;
var ms = (long)(date - new DateTime(1970, 1, 1)).TotalMilliseconds
alarmManager.Set(AlarmType.RtcWakeup, ms, pendingIntent);
DateTime dt= //some date
DateTimeOffset dateOffsetValue = DateTimeOffset.Parse(dt.ToString());
var millisec = dateOffsetValue.ToUnixTimeMilliseconds();
alarmManager.Set(AlarmType.RtcWakeup, millisec, pendingIntent);