AM和PM时间都会发出警报

时间:2016-03-08 04:47:33

标签: android alarmmanager

启动我的第一个真正的Android应用程序,我有麻烦设置警报正确的时间。当使用时间选择器(并使用AM / PM按钮)时,无论你选择什么都没关系,它会在两者之间发生。我能够提取用户使用哪一个作为字符串,但在尝试使警报音消失时无法区分。

以下是我编写的一些代码:

//setting calendar instance with the hour/minute that we picked
calendar.set(Calendar.HOUR_OF_DAY, alarm_timepicker.getCurrentHour());
calendar.set(Calendar.MINUTE, alarm_timepicker.getCurrentMinute());

//get the string value of the hour and minute
int hour = alarm_timepicker.getCurrentHour();
int minute = alarm_timepicker.getCurrentMinute();

//convert the int values to strings
String hour_string = String.valueOf(hour);
String minute_string = String.valueOf(minute);
String am_pm = "";
if (calendar.get(calendar.AM_PM) == Calendar.AM)
    am_pm = "AM";
else if (calendar.get(calendar.AM_PM) == Calendar.PM)
    am_pm = "PM";

if (hour > 12) {
    hour_string = String.valueOf(hour - 12);
}

if (hour == 0) {
    hour_string = String.valueOf(12);

}

if (minute < 10) {
    minute_string = "0" + String.valueOf(minute);
}

//method that changes hlp? status
set_Alarm_Text("hlp is on! " + hour_string + ":" + minute_string + " " + am_pm);

//put in extra sting into my_intent tells the clock, you pressed on
my_intent.putExtra("extra", "alarm on");

1 个答案:

答案 0 :(得分:-1)

尝试使用:

 if(calendar.get(Calendar.AM_PM) == 0){
    am_pm = "AM";
 }else{
    am_pm = "PM";
 }

AM 返回0值,而 PM 返回值1