我正在尝试将触发器设置为每天早上5点,以准备好进行USER_PRESENT广播的电话,但是一旦服务启动,该触发器就会关闭。服务(PAService)通过主电源上的开关关闭和打开 使用 stopService 和 startService 进行活动。此代码在 模拟器,而不是实际的Android手机。
public partial class Job {
[JsonProperty("object")]
public string Object { get; set; }
[JsonProperty("entry")]
public Entry[] Entry { get; set; }
[JsonProperty("resource_url")]
public Uri ResourceUrl { get; set; }
}
public partial class Entry {
[JsonProperty("uuid")]
public string Uuid { get; set; }
[JsonProperty("changed_fields")]
public string[] ChangedFields { get; set; }
[JsonProperty("time")]
public DateTimeOffset Time { get; set; }
}
}
答案 0 :(得分:0)
manager.setRepeating(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, // for repeating
// in every 24
// hours
pendingIntent);
如果2nd parameter
过去,则calendar.getTimeInMillis()
的触发时间将立即触发警报。所以发生的事情是您可能在晚上7点打开应用程序。您希望闹钟第二天早上5点响起,但是您的
calendar.getTimeInMillis()
将在当天早上5点。因此,您需要为此添加检查:
Calendar calendar = Calendar.getInstance();
// calendar.setTimeInMillis(System.currentTimeMillis()); // YOU DON'T NEED THIS LINE
calendar.set(Calendar.HOUR_OF_DAY, 5);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
Calendar current = Calendar.getInstance();
int curTime = current.getTimeInMillis();
int alarmTime = calendar.getTimeInMillis();
if (alarmTime >= curTime){
manager.setRepeating(AlarmManager.RTC_WAKEUP,
alarmTime, AlarmManager.INTERVAL_DAY, // for repeating
// in every 24
// hours
pendingIntent);
}else{
calendar.add(Calendar.DAY_OF_MONTH, 1);
int alarmTime = calendar.getTimeInMillis();
manager.setRepeating(AlarmManager.RTC_WAKEUP,
alarmTime, AlarmManager.INTERVAL_DAY, // for repeating
// in every 24
// hours
pendingIntent);
}