[英]Android Repeating Alarm not working for the next day
我需要创建警报,该警报每天都会触发本地推送通知,并且仅在特定时间有效。
例如。 时段1:上午7点至8点,时段2:下午7点至8点
从上面,我需要创建一个警报,该警报将每天早上7点发送本地推送通知,并且通知应该在早上8点自动消失。 同样,另一个警报将在晚上7点触发本地推送通知并在晚上8点删除通知。
这是我用来创建警报并在结束时间到达时取消通知的代码。 我将以下方法循环2次,以在一天中创建两次重复警报。 如果当前时间超过结束时间,那么我会将通知推迟到第二天。
private void setCheckInAlarm(JSONObject checkInDetails) {
try {
Calendar checkInTimeCalendar = Calendar.getInstance();
String checkInTimeUnparsed = (String) checkInDetails.get("checkInTimeIN");
Integer checkInHour = Integer.parseInt(checkInTimeUnparsed.split(":")[0]);
Integer checkInMinutes = Integer.parseInt(checkInTimeUnparsed.split(":")[1]);
checkInTimeCalendar.set(Calendar.HOUR_OF_DAY, checkInHour);
checkInTimeCalendar.set(Calendar.MINUTE, checkInMinutes);
checkInTimeCalendar.set(Calendar.SECOND, 00);
Calendar checkOutTimeCalendar=Calendar.getInstance();
String checkOutTimeUnparsed=(String)checkInDetails.get("checkInTimeOUT");
Integer checkOutHour=Integer.parseInt(checkOutTimeUnparsed.split(":")[0]);
Integer checkOutMinutes=Integer.parseInt(checkOutTimeUnparsed.split(":")[1]);
checkOutTimeCalendar.set(Calendar.HOUR_OF_DAY, checkOutHour);
checkOutTimeCalendar.set(Calendar.MINUTE, checkOutMinutes);
checkOutTimeCalendar.set(Calendar.SECOND, 0);
Date currentTime=Calendar.getInstance().getTime();
Date checkOutTime=checkOutTimeCalendar.getTime();
if ( currentTime.after(checkOutTime) ) {
checkInTimeCalendar.add(Calendar.DATE,1);
}
Intent intent1 = new Intent(HomeScreen.this, AlarmReceiver.class);
intent1.putExtra("checkInId", (Integer) checkInDetails.get("checkInID"));
intent1.putExtra("notificationTitle", (String) checkInDetails.get("checkInNotificationMessage"));
intent1.putExtra("notificationContent", (String) checkInDetails.get("message"));
intent1.putExtra("checkInTime", (String) checkInDetails.get("checkInTimeIN"));
intent1.putExtra("checkOutTime", (String) checkInDetails.get("checkInTimeOUT"));
intent1.putExtra("checkInNotify", true);
PendingIntent pendingIntent = PendingIntent.getBroadcast(HomeScreen.this, (Integer) checkInDetails.get("checkInID"), intent1, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager) HomeScreen.this.getSystemService(HomeScreen.this.ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC_WAKEUP, checkInTimeCalendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
// setCheckInAlarmDismiss(checkInDetails);
}
catch (Exception e){
e.printStackTrace();
Log.d(activityName, "setCheckInAlarm: "+e);
}
}
触发警报时,我正在创建另一个警报,以在时间结束时自动关闭通知。
推送通知。
public class AlarmReceiver extends BroadcastReceiver
{
public static final String activityName="AlarmReceiver";
@Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
boolean checkInNotificationDismiss=bundle.getBoolean("checkInNotify");
if(checkInNotificationDismiss) {
NotificationParameters notificationParameters = new NotificationParameters();
notificationParameters.setCheckInId(bundle.getInt("checkInId"));
notificationParameters.setMessageTitle(bundle.getString("notificationTitle"));
notificationParameters.setMessageContent(bundle.getString("notificationContent"));
notificationParameters.setCheckInTime(bundle.getString("checkInTime"));
notificationParameters.setCheckOutTime(bundle.getString("checkOutTime"));
new CheckInNotification().createNotification(notificationParameters,context,HomeScreen.class);
}else {
new CheckInNotification().clearAllNotications(bundle.getInt("checkInId"),context,HomeScreen.class);
}
}
}
下面是处理通知和警报创建以消除通知的类。
public class CheckInNotification extends Activity {
public static final String activityName="checkNotification";
public void createNotification(NotificationParameters notificationParameters,Context context,Class<?> cls){
try {
HomeScreenActions homeScreenActions = new HomeScreenActions();
homeScreenActions.setCheckin(false);
homeScreenActions.setRenderCheckin(true);
homeScreenActions.setMenu(true);
homeScreenActions.setRenderMenu(true);
homeScreenActions.setWeather(true);
homeScreenActions.setRenderWeather(true);
homeScreenActions.setCheckInId(notificationParameters.getCheckInId());
homeScreenActions.setMessageTitle(notificationParameters.getMessageTitle());
homeScreenActions.setMessageContent(notificationParameters.getMessageContent());
/* try {
ConstraintLayout isHomeScreenVisible = findViewById(R.id.homeScreenLayout);
}
catch (NullPointerException nullPointer){
Intent i = new Intent(CheckInNotification.this, HomeScreen.class);
i.putExtra("actions", homeScreenActions);
startActivity(i);
}*/
pushNotification(notificationParameters, homeScreenActions, context, cls);
}
catch (Exception e){
e.printStackTrace();
}
}
public void clearAllNotications(Integer notificationId,Context context,Class<?> cls){
NotificationManager notificationManager = (NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancel(notificationId);
}
private void pushNotification(NotificationParameters notificationParameters,HomeScreenActions homeScreenActions,Context context,Class<?> cls){
try{
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Intent notificationIntent = new Intent(context, cls);
Log.d(activityName, "pushNotification main: "+homeScreenActions.getCheckInId());
Log.d(activityName, "pushNotification: "+homeScreenActions.getMessageTitle());
Log.d(activityName, "pushNotification: "+homeScreenActions.getMessageContent());
notificationIntent.putExtra("actions",homeScreenActions);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addParentStack(cls);
stackBuilder.addNextIntent(notificationIntent);
PendingIntent pendingIntent = stackBuilder.getPendingIntent(
homeScreenActions.getCheckInId(),PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
Notification notification = builder.setContentTitle(notificationParameters.getMessageTitle())
.setContentText(notificationParameters.getMessageContent())
.setSound(alarmSound).setSmallIcon(R.mipmap.ic_launcher)
.setOngoing(true)
.setContentIntent(pendingIntent).build();
NotificationManager notificationManager = (NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(homeScreenActions.getCheckInId(), notification);
setCheckInAlarmDismiss(notificationParameters,context,cls);
}
catch (Exception e){
e.printStackTrace();
}
}
private void setCheckInAlarmDismiss(NotificationParameters notificationParameters,Context context,Class<?> cls) {
try {
Calendar calendar = Calendar.getInstance();
String checkOut = notificationParameters.getCheckOutTime();
Integer checkOutHour = Integer.parseInt(checkOut.split(":")[0]);
Integer checkOutMinutes = Integer.parseInt(checkOut.split(":")[1]);
calendar.set(Calendar.HOUR_OF_DAY, checkOutHour);
calendar.set(Calendar.MINUTE, checkOutMinutes);
calendar.set(Calendar.SECOND, 0);
Intent intent1 = new Intent(context, AlarmReceiver.class);
intent1.putExtra("checkInId", notificationParameters.getCheckInId());
intent1.putExtra("notificationMessage", notificationParameters.getMessageTitle());
intent1.putExtra("message", (String) notificationParameters.getMessageContent());
intent1.putExtra("checkInNotify", false);
intent1.putExtra("checkInTime", notificationParameters.getCheckInTime());
intent1.putExtra("checkOutTime", notificationParameters.getCheckOutTime());
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, notificationParameters.getCheckInId(), intent1, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager) context.getSystemService(context.ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
}
catch (Exception e){
e.printStackTrace();
Log.d(activityName, "setCheckInAlarmDismiss: "+e);
}
}
}
因此,问题在于首次初始化应用程序警报并首次创建通知时,会同时发生session1和session2警报,但第二天却无法正常工作。
首先,我不仅要把小时,分钟和秒存储到日历中,还要存储当天的日期(也许是月份),以防您每天都要这样做。
calendar.set(Calendar.DAY_OF_MONTH, insert_current_day);
calendar.set(Calendar.MONTH, insert_current_month);
...
会话1和会话2的警报均被触发(并且通知被撤消)后,您需要在日历中添加一天。
calendar.add(Calendar.DATE, 1);
在设置AlarmService之前。 它将startTime设置为第二天,AlarmService将在第二天及之后的每一天触发。
希望这就是您想要的,将对您有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.