Android通知问题

时间:2012-12-29 06:56:55

标签: android android-notifications

我想要实现的是通知,即使用通知管理器的警报。 我要求用户选择日期和时间并设置alert.Set警报有一些值 无,准时,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);

我遇到的问题是,如果没有,则启动时间值为0。 那么我应该为starttime设置什么样的值,以便它不会发生。

1 个答案:

答案 0 :(得分:3)

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

请改用上述代码。