我在这个alarmClock方法中做错了什么?

时间:2013-08-07 17:17:41

标签: android alarm android-alarms

第一次调用此方法时,它会在正确的时间(当前时间+2分钟)设置警报。第二次调用此方法时,它不会在当前时间+2分钟设置警报。它在与第一次完全相同的时间设置警报。我究竟做错了什么? 这是我的代码:

private void setAlarmNow() {
    Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
    // Vibrate for 500 milliseconds
    v.vibrate(1000);
    GregorianCalendar cal = new GregorianCalendar();

    int minute = cal.get(Calendar.MINUTE)+2;
    int hour = cal.get(Calendar.HOUR_OF_DAY);
    if(minute>59){minute-=59; hour++;}  
    if(hour>23){hour-=23;}

    Intent intent = new Intent(AlarmClock.ACTION_SET_ALARM);
    intent.putExtra(AlarmClock.EXTRA_HOUR, hour);
    intent.putExtra(AlarmClock.EXTRA_MINUTES, minute);
    intent.putExtra(AlarmClock.EXTRA_SKIP_UI, true);            

    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
    AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);

    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(System.currentTimeMillis());
    calendar.add(Calendar.SECOND, 10);
    alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);   
    Log.d(TAG, "Backup alarm geset.");
}

0 个答案:

没有答案