[英]Past notifications showing with current notifications
我正在使用AlarmManager
重复警报。 我已经设置了星期一,星期四和星期六的重复警报。 该警报在星期一正确显示,但是在星期四到来时,它将同时显示星期一和星期四的两个通知,并在星期六显示所有三个通知。 我如何编码,这样当第一个警报完成时,它不会在同一周的后续警报中显示,而是转到接下来的几周?
EDIT1-代码
主要活动
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case R.id.action_settings:
EnableNoti();
return true;
}
return super.onOptionsItemSelected(item);
}
private void EnableNoti() {
Calendar calendar = Calendar.getInstance();
Calendar calendar1 = Calendar.getInstance();
Calendar calendar2 = Calendar.getInstance();
Calendar calendar3 = Calendar.getInstance();
calendar.set(Calendar.DAY_OF_WEEK, 1); // Sunday
calendar1.set(Calendar.DAY_OF_WEEK, 4); // Wednesday
calendar2.set(Calendar.DAY_OF_WEEK, 6); // Friday
calendar3.set(Calendar.DAY_OF_WEEK, 5); // Thursday
calendar3.set(Calendar.DAY_OF_WEEK_IN_MONTH, 1); // First Thursday of
// Each Month
Date now = new Date(System.currentTimeMillis());
if (calendar3.getTime().before(now)) {
calendar3.add(Calendar.MONTH, 1);
} else if (calendar.getTime().before(now)) {
calendar.add(Calendar.HOUR, 168);
} else if (calendar1.getTime().before(now)) {
calendar1.add(Calendar.HOUR, 168);
} else if (calendar2.getTime().before(now)) {
calendar2.add(Calendar.HOUR, 168);
}
// Sunday
calendar.set(Calendar.HOUR_OF_DAY, 9);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.AM_PM, Calendar.AM);
// Wednesday
calendar1.set(Calendar.HOUR_OF_DAY, 17);
calendar1.set(Calendar.MINUTE, 30);
calendar1.set(Calendar.SECOND, 0);
calendar1.set(Calendar.AM_PM, Calendar.PM);
// Friday
calendar2.set(Calendar.HOUR_OF_DAY, 17);
calendar2.set(Calendar.MINUTE, 30);
calendar2.set(Calendar.SECOND, 0);
calendar2.set(Calendar.AM_PM, Calendar.PM);
// Thursday
calendar3.set(Calendar.HOUR_OF_DAY, 22);
calendar3.set(Calendar.MINUTE, 0);
calendar3.set(Calendar.SECOND, 0);
calendar3.set(Calendar.AM_PM, Calendar.PM);
Intent myIntent = new Intent(TheBeginningEnglish.this,
MyReceiverEnglish.class);
pendingIntent = PendingIntent.getBroadcast(TheBeginningEnglish.this, 0,
myIntent, 0);
Intent myIntent1 = new Intent(TheBeginningEnglish.this,
MyReceiver1English.class);
pendingIntent1 = PendingIntent.getBroadcast(TheBeginningEnglish.this,
0, myIntent1, 0);
Intent myIntent2 = new Intent(TheBeginningEnglish.this,
MyReceiver2English.class);
pendingIntent2 = PendingIntent.getBroadcast(TheBeginningEnglish.this,
0, myIntent2, 0);
Intent myIntent3 = new Intent(TheBeginningEnglish.this,
MyReceiver3English.class);
pendingIntent3 = PendingIntent.getBroadcast(TheBeginningEnglish.this,
0, myIntent3, 0);
AlarmManager alarmMgr = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 7,
pendingIntent); // every Sunday
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP,
calendar1.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 7,
pendingIntent1); // every Wednesday
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP,
calendar2.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 7,
pendingIntent2); // every Friday
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP,
calendar3.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 31,
pendingIntent3); // every first Thursday
}
接收器
public class MyReceiverEnglish extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent)
{
Intent service = new Intent(context, MyAlarmServiceEnglish.class);
context.startService(service);
}
}
AlarmService.java
public class MyAlarmServiceEnglish extends Service {
int currentapiVersion = android.os.Build.VERSION.SDK_INT;
private NotificationManager mManager;
Notification notification;
// Notification notification;
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
}
@Override
public void onStart(Intent intent, int startId) {
onStartCommand(intent, 0, startId);
}
@SuppressWarnings({ "static-access", "deprecation" })
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
mManager = (NotificationManager) this.getApplicationContext()
.getSystemService(
this.getApplicationContext().NOTIFICATION_SERVICE);
Intent intent1 = new Intent(this.getApplicationContext(),
TheBeginning1English.class);
if (currentapiVersion < android.os.Build.VERSION_CODES.HONEYCOMB) {
Notification notification = new Notification(
R.drawable.ic_launcher,
"Its Sunday!.",
System.currentTimeMillis());
intent1.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP
| Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingNotificationIntent1 = PendingIntent
.getActivity(this.getApplicationContext(), 0, intent1,
PendingIntent.FLAG_UPDATE_CURRENT);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.setLatestEventInfo(this.getApplicationContext(),
"Testing", "Its Sunday.",
pendingNotificationIntent1);
mManager.notify(0, notification);
} else {
PendingIntent pendingNotificationIntent1 = PendingIntent
.getActivity(this.getApplicationContext(), 0, intent1,
PendingIntent.FLAG_UPDATE_CURRENT);
Notification notification = new NotificationCompat.Builder(this)
.setContentTitle("Test Test App")
.setContentText("Its Sunday!.")
.setWhen(System.currentTimeMillis())
.setContentIntent(pendingNotificationIntent1)
.setSmallIcon(R.drawable.ic_launcher).build();
notification.flags |= Notification.FLAG_AUTO_CANCEL;
mManager.notify(0, notification);
}
return START_NOT_STICKY;
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
}
}
就是这样,在主要活动中,您将看到四个单独的日历变量,我必须创建一个Receiver,Receiver1,Receiver2,Receiver3和AlarmService1,2,3。 对于每个日历,不是最好的方法,而是我能想到的唯一方法。 我添加了:
Date now = new Date(System.currentTimeMillis());
if (calendar3.getTime().before(now)) {
calendar3.add(Calendar.MONTH, 1);
} else if (calendar.getTime().before(now)) {
calendar.add(Calendar.HOUR, 168);
} else if (calendar1.getTime().before(now)) {
calendar1.add(Calendar.HOUR, 168);
} else if (calendar2.getTime().before(now)) {
calendar2.add(Calendar.HOUR, 168);
}
我今天添加了日期部分,还没有检查出来是否知道它是否可以工作。 感谢您为我提供帮助!
Alaram服务如何工作?
它根据您的条件开始,就像您为一天,一周或任何一周的一天设置的一样。 因此在每个指定的时间设置它接收命令。 像通知,音。
因此在onStartServiceCommand()方法中。 您必须为此应用逻辑。
因此通知仅在该特定日期发送一次。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.