Создаю действие для настроек и в нем создаю переключатель для отправки уведомлений в определенное время в 7 утра и 5 вечера. Когда я проверил переключатель, появились два уведомления.ящик тревоги не работает в указанное время
Код переключателя
public void OnRepeationNotification(int id,int hours,int minutes,int seconds) {
Calendar calendar= Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY,hours);
calendar.set(Calendar.MINUTE,minutes);
calendar.set(Calendar.SECOND,seconds);
Intent intent=new Intent(getApplicationContext(),NotificationReciever.class);
PendingIntent pendingIntent=PendingIntent.getBroadcast(getApplicationContext(),id,intent,PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager=(AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),AlarmManager.INTERVAL_DAY,pendingIntent);
}
public void onClickNotify(View view) {
Switch v=(Switch) view;
if(v.isChecked()){
putSett(true,"sett",this,0);
OnRepeationNotification(1,7,0,0);
OnRepeationNotification(2,17,0,0);
}else {
alert_sound.setChecked(false);
alert_vibrate.setChecked(false);
putSett(false,"sett",this,1);
putSett(false,"sett",this,2);
putSett(false,"sett",this,0);
}
}
код широковещательного приемника
public class NotificationReciever extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
sendNotification(context,1);
sendNotification(context,2);
}
public void sendNotification(Context context,int id){
NotificationManager notificationManager=(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Intent repeating_intent=new Intent(context,RepeatingActivity.class);
repeating_intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent=PendingIntent.getActivity(context,id,repeating_intent,PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder=new NotificationCompat.Builder(context)
.setContentIntent(pendingIntent)
.setSmallIcon(R.drawable.fav_black)
.setContentTitle("Notifiction Title")
.setContentText("Notification Text")
.setAutoCancel(true);
notificationManager.notify(id,builder.build());
}
}
Как отправлять уведомления, даже если приложение закрыто или устройство перезагружается?
два уведомления все еще появляются вместе, независимо от того, установлен ли я в другое время –
Они оба появляются правильно, когда вы переворачиваете переключатель? Вы добавили 'calendar.set (Calendar.DATE, 1);' part? –
Я установил этот код Calendar rightNow = Calendar.getInstance(); if (calendar.before (rightNow)); calendar.set (Calendar.DATE, 1); это правильно –