特定工作日的Android通知会直接发出
我试图通过使用以下代码发出通知,在周四和周五离开: 此代码适用于星期四特定工作日的Android通知会直接发出,android,eclipse,Android,Eclipse,我试图通过使用以下代码发出通知,在周四和周五离开: 此代码适用于星期四 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(updateTim
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);
但是,此代码不起作用。如果时间已超过通知时间,则直接弹出通知
我遗漏了什么?显然,您的代码还可以,但完成了一半。现在,您必须创建一个BroadcastReceiver,从中设置通知。你可以浏览一下,这篇文章将对你有所帮助。我想在这里提到,它将为用户设置单一警报,而不是重复。对于重复,您必须使用
public void set repeating(int-type,long-triggerAtMillis,long-intervalMillis,pendingent操作)
而不是public void set(int-type,long-triggerAtMillis,pendingent操作)
。但它仍将定期重复
在我看来,对于你特定类型的重复,你可以用不同的方式安排。最初使用公共无效设置(int类型、长触发器、挂起操作)
仅为第二天设置警报(在检查第二天警报后)。这将非常简单。当警报将由系统广播并且将调用您的广播接收器时。在onreceive()
您的BroadcastReceiver
中,设置通知并设置第二天的警报(在检查第二天的警报后)。这样,整个循环过程将由您控制,您可以轻松地为其设置任何逻辑。这样做
if (updateTime.getTimeInMillis() < System.currentTimeMillis()
|| updateTime.before(new GregorianCalendar())) {
updateTime.set(Calendar.HOUR_OF_DAY, hourOfDay + 24);
updateTime.add(GregorianCalendar.DAY_OF_MONTH, 7);
}
}
报警广播接收机是
公共类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);
}
}你有没有尝试过Android内置的报警调度功能?没有,我正在为我所有的报警使用此功能。我将编辑第一篇文章,让它更具体一点。是否有一种方法可以像我上面所做的那样,我是安卓开发的初学者,所以我不能100%确定你的意思。
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);
}