Android重复闹钟在第二天无效

时间:2018-01-04 02:02:54

标签: android notifications alarmmanager

我需要创建警报,每天触发本地推送通知,并且仅在特定时间内有效。

例如。 第1节:上午7点至上午8点, 第2节:晚上7点 - 晚上8点

从上面开始,我需要创建一个警报,每天早上7点发送本地推送通知,并且通知应该在上午8点自动消失。同样,另一个警报将在晚上7点触发本地推送通知,并在晚上8点删除通知。

以下是用于创建警报并在结束时间到达时解除通知的代码。我循环使用以下方法2次,以便在一天内创建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警报,但是第二天,它不起作用。

1 个答案:

答案 0 :(得分:1)

首先,我不仅会将小时,分钟和秒存储到日历中,还会存储当天的日期(也可能是月份),以防您每天都想这样做。

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将在第二天和之后的每一天触发。

希望它能满足您的需求并为您提供帮助。