android - 特定日期和月份的Android xamarin本地通知
问题描述
一段时间以来,他一直试图在应用程序中为给定的日期和月份设置通知。不幸的是,通知会在每天调用它几秒钟后出现。
这是我的代码:
private void Remind_Click(object sender, System.EventArgs e)
{
string title = "If you see this";
string message = "it means it works";
Intent alarmIntent = new Intent(Application.Context, typeof(AlarmReceiver));
alarmIntent.PutExtra("message", message);
alarmIntent.PutExtra("title", title);
var pendingIntent = PendingIntent.GetBroadcast(this, 0, alarmIntent, PendingIntentFlags.UpdateCurrent);
var alarmManager = GetSystemService(AlarmService).JavaCast<AlarmManager>();
DateTime nowDate = DateTime.Now;
int month = 09;
int day = 24;
DateTime newDate = new DateTime(nowDate.Year, month, day);
DateTime date = newDate;
var ms = (long)(date - new DateTime(1970, 1, 1)).TotalMilliseconds;
alarmManager.SetInexactRepeating(AlarmType.RtcWakeup, 3600000, ms, pendingIntent);
ShowSnack(main, $"Set date: {date} ");
}
请帮我解决这个问题。
解决方案
- 对于
setInexactRepeating (int type, long startTime, long intervalTime, PendingIntent pi);
第二个参数是开始执行时间,第三个参数是两次执行之间的间隔时间。所以你在输入参数时有错误。 - 如果你只触发一次事件,你可以使用
set(int type, long startTime, PendingIntent pi);
使用这种方法只会触发一次事件。
注意:因为您使用的是 AlarmType.RtcWakeup。DateTime.Now 的时间可能与 Utc 时间不同(可以使用 DateTime.Now 和 DateTime.UtcNow 来查看两者之间是否存在时间差)。
推荐阅读
- vba - 创建文件夹并移动新创建的文件夹中的所有 xlsx 文件
- python - 在嵌套字典中切换两个字典
- javascript - 在 React Redux 中向 jsonplaceholder 发送 post 请求失败
- sql - 尝试从 ODBC 数据库中获取数据时在 vba 上出现类型不匹配错误
- javascript - 与 && 或 || JavaScript 中的组合
- php - 如何使用数据库会话驱动程序在 laravel 多重身份验证中获取登录的客户信息?
- azure - 如何从 Azure.Storage.Blobs 模拟 GetBlobsByHierarchy()?
- php - PHP:添加星期天以外的日子
- r - 是否有 R 函数可以找到硬币模拟给定间隔内的最大化运行?
- python - 获取 DateTimeSeries 的整数位置索引而不是 DateTime 索引