0

我要实现的是通知,即使用通知管理器的警报。我要求用户选择日期和时间并设置警报。设置警报有一些值,如无、准时、5 分钟前、15 分钟前、30 分钟前等等。它适用于除 NONE 之外的每个人。 .如果用户选择 NONE 则 NOTIFICATION 不应该去。而是它会马上去如果用户选择无。所以我没有为它设置任何东西。

if(alert.equalsIgnoreCase("On Time"))
{
    Calendar cal=Calendar.getInstance();
    cal.set(Calendar.DAY_OF_MONTH,reminderday);
    cal.set(Calendar.MONTH,remindermonth);
    cal.set(Calendar.YEAR,reminderyear);
    cal.set(Calendar.HOUR_OF_DAY,hour);
    cal.set(Calendar.MINUTE,minute);
    starttime=cal.getTimeInMillis();
}
else if(alert.equalsIgnoreCase("5 Minutes Before"))
{
    Log.i("inside","5 mins before");
    Calendar cal=Calendar.getInstance();
    cal.set(Calendar.DAY_OF_MONTH,reminderday);
    cal.set(Calendar.MONTH,remindermonth);
    cal.set(Calendar.YEAR,reminderyear);
    cal.set(Calendar.HOUR_OF_DAY,hour);
    cal.set(Calendar.MINUTE,minute-5);
    starttime=cal.getTimeInMillis();
}

else if(alert.equalsIgnoreCase("15 Minutes Before"))
{
    Log.i("inside","15 mins before");
    Calendar cal=Calendar.getInstance();
    cal.set(Calendar.DAY_OF_MONTH,reminderday);
    cal.set(Calendar.MONTH,remindermonth);
    cal.set(Calendar.YEAR,reminderyear);
    cal.set(Calendar.HOUR_OF_DAY,hour);
    cal.set(Calendar.MINUTE,minute-15);
    starttime=cal.getTimeInMillis();
}
else if(alert.equalsIgnoreCase("30 Minutes Before"))
{
    Log.i("inside","30 mins before");
    Calendar cal=Calendar.getInstance();
    cal.set(Calendar.DAY_OF_MONTH,reminderday);
    cal.set(Calendar.MONTH,remindermonth);
    cal.set(Calendar.YEAR,reminderyear);
    cal.set(Calendar.HOUR_OF_DAY,hour);
    cal.set(Calendar.MINUTE,minute-30);
    starttime=cal.getTimeInMillis();
}
else if(alert.equalsIgnoreCase("1 Hour Before"))
{
    Log.i("inside","1 hr before");
    Calendar cal=Calendar.getInstance();
    cal.set(Calendar.DAY_OF_MONTH,reminderday);
    cal.set(Calendar.MONTH,remindermonth);
    cal.set(Calendar.YEAR,reminderyear);
    cal.set(Calendar.HOUR_OF_DAY,hour-1);
    cal.set(Calendar.MINUTE,minute);
    starttime=cal.getTimeInMillis();
}
else if(alert.equalsIgnoreCase("2 Hour Before"))
{
    Log.i("inside","2 hr before");
    Calendar cal=Calendar.getInstance();
    cal.set(Calendar.DAY_OF_MONTH,reminderday);
    cal.set(Calendar.MONTH,remindermonth);
    cal.set(Calendar.YEAR,reminderyear);
    cal.set(Calendar.HOUR_OF_DAY,hour-2);
    cal.set(Calendar.MINUTE,minute);
    starttime=cal.getTimeInMillis();
}
else if(alert.equalsIgnoreCase("1 Day Before"))
{
    Log.i("inside","1 day before");
    Calendar cal=Calendar.getInstance();
    cal.set(Calendar.DAY_OF_MONTH,reminderday-1);
    cal.set(Calendar.MONTH,remindermonth);
    cal.set(Calendar.YEAR,reminderyear);
    cal.set(Calendar.HOUR_OF_DAY,hour);
    cal.set(Calendar.MINUTE,minute);
    starttime=cal.getTimeInMillis();
}
else if(alert.equalsIgnoreCase("2 Day Before"))
{
    Log.i("inside","2 day before");
    Calendar cal=Calendar.getInstance();
    cal.set(Calendar.DAY_OF_MONTH,reminderday-2);
    cal.set(Calendar.MONTH,remindermonth);
    cal.set(Calendar.YEAR,reminderyear);
    cal.set(Calendar.HOUR_OF_DAY,hour);
    cal.set(Calendar.MINUTE,minute);
    starttime=cal.getTimeInMillis();
}
else if(alert.equalsIgnoreCase("1 Week Before"))
{
    Log.i("inside","1 week before");
    Calendar cal=Calendar.getInstance();
    cal.set(Calendar.DAY_OF_MONTH,reminderday-7);
    cal.set(Calendar.MONTH,remindermonth);
    cal.set(Calendar.YEAR,reminderyear);
    cal.set(Calendar.HOUR_OF_DAY,hour);
    cal.set(Calendar.MINUTE,minute);
    starttime=cal.getTimeInMillis();
}
else
        {
            starttime=-1;   
        }
PendingIntent pendingIntent = PendingIntent.getBroadcast(this,0,intent, PendingIntent.FLAG_UPDATE_CURRENT);
            am.set(AlarmManager.RTC_WAKEUP,starttime, pendingIntent);

我遇到的问题是,如果没有,则 starttime 值为 0。那么我应该为 starttime 设置什么值,这样它就不会消失。

4

1 回答 1

3
if (starttime > 0){    
    PendingIntent pendingIntent = PendingIntent.getBroadcast(this,0,intent, PendingIntent.FLAG_UPDATE_CURRENT);
                am.set(AlarmManager.RTC_WAKEUP,starttime, pendingIntent);    
}

请改用上面的代码。

于 2012-12-29T07:01:45.620 回答