calender.add在android中无法正常运行

问题描述 投票:0回答:1

我编写此代码的目的是确保我的应用程序将在特定时间通知用户,并且应用程序将每天从早上7点开始重复相同的通知。

但是,我尝试使用此功能(如下所示)。但似乎无法正常工作。

calender.add(DATE,1)

实际上,我想生成一个自动通知。药物信息和每天重复的次数是用户输入。

所以我想做的事情是if语句,比较当前时间是否在7 AM-8 PM之间。

否则,语句将比较当前时间是否在晚上8点之后,如果是,则日历将再增加1天。这样,这些应用第二天就会在7点弹出相同的通知上午。

另外,语句将检查当前时间是否在上午7点之前或不。如果是,则应用程序将在当前时间等于上午7点

但是else if语句不符合我的预期。我的意思是,如果我现在设置通知是在晚上10点,那么这些应用将不会在第二天(早上7点)通知我。

因此,我想问问你们中的任何人是否可以帮助我修复我的代码,如下所示。 if and else段运行良好。但是else if语句似乎不起作用。

 AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

Calendar calendar = Calendar.getInstance();
Calendar now = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());

//repeat on 7am
calendar.set(Calendar.HOUR_OF_DAY, 7);
calendar.set(Calendar.MINUTE, 00);
calendar.set(Calendar.SECOND, 0);

//check current hour
int timeOfDay = now.get(Calendar.HOUR_OF_DAY);

//to check if current hour is after 7pm
Calendar later = Calendar.getInstance();
later.set(Calendar.HOUR_OF_DAY, 20);
later.set(Calendar.MINUTE, 00);
later.set(Calendar.SECOND, 0);

if (timeOfDay >= 7 && timeOfDay <= 20) {
    //notification's process
}

else if (now.after(later)) {
        Log.d("Hey", "Added a day");
        calendar.add(Calendar.DATE, 1);
        calendar.set(Calendar.HOUR_OF_DAY, 7);
        calendar.set(Calendar.MINUTE, 00);
        calendar.set(Calendar.SECOND, 0);

        this.context = context;
        prefs = context.getSharedPreferences("prefs", Context.MODE_PRIVATE);
        nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        notification = new NotificationCompat.Builder(context);
        name = prefs.getString("name" + count, "");
        hours = prefs.getInt("hora" + count, 8);
        minutes = prefs.getInt("minuto" + count, 0);


        Intent newIntentKill = new Intent(context, Broadcast.class);
        Bundle saco = new Bundle();
        saco.putInt("count", count);
        saco.putBoolean("shownBefore", true);
        newIntentKill.putExtras(saco);

        PendingIntent pendingIntentKill = PendingIntent.getBroadcast(context, count, newIntentKill, PendingIntent.FLAG_UPDATE_CURRENT);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
               alarmManager.setExact(AlarmManager.RTC, calendar.getTimeInMillis(), pendingIntentKill);

         } else {
               alarmManager.set(AlarmManager.RTC, calendar.getTimeInMillis(), pendingIntentKill);
         }

            Log.d("Alarm", "Alarms set for everyday 7 am.");
    }

else {

    this.context = context;
    prefs = context.getSharedPreferences("prefs", Context.MODE_PRIVATE);
    nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    notification = new NotificationCompat.Builder(context);
    name = prefs.getString("name" + count, "");
    hours = prefs.getInt("hora" + count, 8);
    minutes = prefs.getInt("minuto" + count, 0);


    Intent newIntentKill = new Intent(context, Broadcast.class);
    Bundle saco = new Bundle();
    saco.putInt("count", count);
    saco.putBoolean("shownBefore", true);
    newIntentKill.putExtras(saco);

    PendingIntent pendingIntentKill = PendingIntent.getBroadcast(context, count, newIntentKill, PendingIntent.FLAG_UPDATE_CURRENT);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
           alarmManager.setExact(AlarmManager.RTC, calendar.getTimeInMillis(), pendingIntentKill);

     } 
     else {
           alarmManager.set(AlarmManager.RTC, calendar.getTimeInMillis(), pendingIntentKill);
         }

     Log.d("Alarm", "Alarms set for everyday 7 am.");
}

所以,在此先感谢您的帮助。我真的很感激。

java android notifications alarmmanager
1个回答
0
投票

java.time和ThreeTenABP

我建议您使用现代Java日期和时间API java.time进行日期和时间工作。与设计较差的老式Calendar类相比,这种方法要好得多,而且通常不会给人带来很多不愉快的惊喜。例如,您的if / else方案可能像这样:

    ZoneId zone = ZoneId.systemDefault();
    ZonedDateTime nowZdt = ZonedDateTime.now(zone);
    LocalTime now = nowZdt.toLocalTime();
    if (now.isBefore(LocalTime.of(20, 0))) {
        if (now.isBefore(LocalTime.of(7, 0))) {
            // Set alarm for 7 today
            long alarmTimeMillis = nowZdt.with(LocalTime.of(7, 0))
                    .toInstant()
                    .toEpochMilli();
            // Set alarm
        } else { // between 7 AM and 8 PM
            // notification's process
        }
    } else { // after 8 PM
        // Set alarm for 7 AM tomorrow
        long alarmTimeMillis = nowZdt.plusDays(1)
                .with(LocalTime.of(7, 0))
                .toInstant()
                .toEpochMilli();
        // Set alarm
    }           

您的代码出了什么问题?

我不确定发生了什么。我已经发现的可能问题在此行:

if (timeOfDay >= 7 && timeOfDay <= 20) {

您仅比较一天中的小时,而您正在使用<=。这意味着,当时间为20:something(最高20:59且包括20:59)时,条件为true。因此,在这种情况下,您不会进入要添加1天的分支。我认为这不是您想要的。

问题:java.time是否不需要Android API级别26?

java.time在较新和较旧的Android设备上均可正常运行。它只需要至少Java 6

  • 在Java 8和更高版本以及更新的Android设备(来自API级别26)中,内置了现代API。
  • 在非Android Java 6和7中,获得了ThreeTen Backport,这是现代类的backport(JSR 310的ThreeTen;请参见底部的链接。
  • 在(较旧的)Android上,使用ThreeTen Backport的Android版本。称为ThreeTenABP。并确保使用子包从org.threeten.bp导入日期和时间类。

链接

© www.soinside.com 2019 - 2024. All rights reserved.