我正在尝试使用以下代码在星期四和星期五发出通知: 这段代码是星期四
Calendar updateTime = Calendar.getInstance();
updateTime.set(Calendar.HOUR_OF_DAY, 14);
updateTime.set(Calendar.MINUTE, 44);
updateTime.set(Calendar.SECOND, 0);
updateTime.set(Calendar.DAY_OF_WEEK, 5);
if(updateTime.before(new GregorianCalendar())){
updateTime.add(GregorianCalendar.DAY_OF_MONTH, 7);
}
Intent intent = new Intent(context, Alarm4.class);
intent.putExtra(ONE_TIME, Boolean.TRUE);
PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, updateTime.getTimeInMillis(), pi);
但是,如果时间已超过通知时间,则此代码无法直接弹出通知。
我错过了什么?
答案 0 :(得分:1)
这样做。
if (updateTime.getTimeInMillis() < System.currentTimeMillis()
|| updateTime.before(new GregorianCalendar())) {
updateTime.set(Calendar.HOUR_OF_DAY, hourOfDay + 24);
updateTime.add(GregorianCalendar.DAY_OF_MONTH, 7);
}
并创建一个类来设置警报。
public class setAlarm {
Context context;
public setAlarm(Context c) {
// TODO Auto-generated constructor stub
this.context = c;
}
public void settingAlarm(Calendar calendar, int counter) {
Intent intent = new Intent(context, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context,
counter, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) context
.getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY,
pendingIntent);
}
}
和Alarm BroadCastReceiver是 公共类AlarmReceiver扩展了BroadcastReceiver {
private NotificationManager manager;
private int NOTIFICATION_ID;
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Toast.makeText(context, "Alarm Received", Toast.LENGTH_LONG).show();
manager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.ic_launcher,
"Food Time", System.currentTimeMillis());
Intent intent2 = new Intent(context, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
intent2, PendingIntent.FLAG_UPDATE_CURRENT);
notification.setLatestEventInfo(context, "FoodTime",
"Click to View your food", pendingIntent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.sound = Uri
.parse("android.resource://"
+ "com.technogalax.sixsmallmealaday.alarm" + "/"
+ R.raw.nsound);
manager.notify(NOTIFICATION_ID, notification);
}
}
答案 1 :(得分:0)
显然,你的代码还可以,但已经完成了一半。现在,您必须创建一个BroadcastReceiver,您必须从中设置通知。您可以浏览此article,这篇文章对您有所帮助。我想在这里提一下,它会为用户设置单一警报,而不是重复。要重复,您必须使用public void setRepeating (int type, long triggerAtMillis, long intervalMillis, PendingIntent operation)
而不是public void set (int type, long triggerAtMillis, PendingIntent operation)
。它仍然会定期重复。</ p>
在我对您的特定重复类型的想法中,您可以以不同的方式进行排列。最初使用public void set (int type, long triggerAtMillis, PendingIntent operation)
将警报设置为第二天(在第二天检查警报后)。这将非常简单。当系统广播报警时,您的BroadcastReceiver
将被呼叫。在onRecieve()
您的BroadcastReceiver
中,设置通知并设置第二天的闹钟(在第二天检查闹钟后)。通过这种方式,总循环过程将在您的控制之下,您可以轻松地为其设置任何逻辑。