Android重复警报第二天不工作

Android重复警报第二天不工作,android,notifications,alarmmanager,Android,Notifications,Alarmmanager,我需要创建警报,每天触发本地推送通知,并且只在特定时间有效 比如说。 第一节:上午7时至8时, 第二节:晚上七时至八时 从上面开始,我需要创建一个警报,每天早上7点发送本地推送通知,该通知在早上8点自动消失。类似地,另一个警报将在晚上7点触发本地推送通知,并在晚上8点删除通知 下面是我用来创建警报并在结束时间到达时取消通知的代码。我将以下方法循环2次,以创建一天2次的重复警报。如果当前时间超过结束时间,则我将通知推到第二天 private void setCheckInAlarm(JSONObj

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

比如说。 第一节:上午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);
        }
    }



}
下面是处理通知和警报创建以取消通知的类

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);
        }
    }



}
公共类CheckInNotification扩展活动{
公共静态最终字符串activityName=“checkNotification”;
public void createNotification(NotificationParameters NotificationParameters、上下文上下文、类cls){
试一试{
宅基地宅基地=新宅基地();
HomeCreenactions.setCheckin(假);
HomeCreenactions.setRenderCheckin(真实);
HomeCreenactions.setMenu(真);
HomeCreenactions.setRenderMenu(真);
家庭信念。setWeather(真实);
HomeCreenactions.setRenderWeather(真);
HomeCreenactions.setCheckInId(notificationParameters.getCheckInId());
setMessageTitle(notificationParameters.getMessageTitle());
setMessageContent(notificationParameters.getMessageContent());
/*试一试{
ConstraintLayout isHomeScreenVisible=findViewById(R.id.homeScreenLayout);
}
捕获(NullPointerException nullPointer){
意图i=新意图(选中Innotification.this、HomeScreen.class);
i、 putExtra(“行动”,家庭信念);
星触觉(i);
}*/
pushNotification(通知参数、家庭信念、上下文、cls);
}
捕获(例外e){
e、 printStackTrace();
}
}
public void ClearAllNotification(整数notificationId、上下文上下文、类cls){
NotificationManager NotificationManager=(NotificationManager)
getSystemService(context.NOTIFICATION\u服务);
notificationManager.cancel(notificationId);
}
私有无效推送通知(NotificationParameters NotificationParameters、HomeCreenactions HomeCreenactions、上下文上下文、类cls){
试一试{
Uri alarmSound=RingtoneManager.getDefaultUri(RingtoneManager.TYPE_通知);
意向通知意向=新意向(上下文,cls);
Log.d(activityName,“pushNotification main:+homeScreenActions.getCheckInId());
Log.d(activityName,“pushNotification:+HomeCreenactions.getMessageTitle());
Log.d(activityName,“pushNotification:+HomeCreenactions.getMessageContent());
notificationIntent.putExtra(“行动”,家乡信条);
TaskStackBuilder stackBuilder=TaskStackBuilder.create(上下文);
stackBuilder.addParentStack(cls);
stackBuilder.addNextIntent(通知意图);
PendingEvent PendingEvent=stackBuilder.GetPendingEvent(
homeScreenActions.getCheckInId(),pendingent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder=新建NotificationCompat.Builder(上下文);
Notification Notification=builder.setContentTitle(notificationParameters.getMessageTitle())
.setContentText(notificationParameters.getMessageContent())
.setSound(alarmSound).setSmallIcon(R.mipmap.ic_启动器)
.正在进行(正确)
.setContentIntent(pendingIntent).build();
NotificationManager NotificationManager=(NotificationManager)
getSystemService(context.NOTIFICATION\u服务);
notificationManager.notify(homeScreenActions.getCheckInId(),notification);
SetCheckinalArmDisclose(通知参数、上下文、cls);
}
捕获(例外e){
e、 printStackTrace();
}
}
私有void setCheckInAlarmDismise(NotificationParameters NotificationParameters、上下文上下文、类cls){
试一试{
日历=Calendar.getInstance();
字符串签出=notificationParameters.getCheckOutTime();
整数checkOutHour=Integer.parseInt(checkOut.split(“:”[0]);
整数checkOutMinutes=Integer.parseInt(checkOut.split(“:”[1]);
calendar.set(calendar.HOUR,OF,OF,the,checkOutHour);
calendar.set(calendar.Minutes、checkOutMinutes);
calendar.set(calendar.SECOND,0);
意图intent1=新意图(上下文,AlarmReceiver.class);
intent1.putExtra(“checkInId”,notificationParameters.getCheckInId());
intent1.putExtra(“notificationMessage”,notificationParameters.getMessageTitle());
intent1.putExtra(“message”,(String)notificationParameters.getMessageContent();
意图1.putExtra(“checkInNotify”,false);
intent1.putExtra(“checkInTime”,notificationParameters.getCheckInTime());
intent1.putExtra(“checkOutTime”,notificationParameters.getCheckOutTime());
PendingEvent PendingEvent=PendingEvent.getBroadcast(上下文,notificationParameters.getCheckInId(),intent1,PendingEvent.FLAG_UPDATE_CURRENT);
AlarmManager am=(AlarmM