2011-11-15 251 views
0

有了這個代碼,我能夠採取一些字符串,如「週三10:10」下一個報警,時間以秒或毫秒

String nextAlarm = Settings.System.getString(getContentResolver(), 
    Settings.System.NEXT_ALARM_FORMATTED); 

,但我想有幾秒鐘到下一個報警值。那可能嗎 ? 如果我能夠讀取說明字段的值以及啓用此警報的日期,那麼對我而言,這將會很好。

回答

1

您需要通過以下方式

private void updateAlarmDate(String nextAlarm) { 
    if (nextAlarm.length()==0 || nextAlarm == "") { 
     return; 
    } 
    else 
    { 
     DateFormat sdf = new SimpleDateFormat("EEE hh:mm aa"); 
     Date alarmDate = null; 
     try { 
      alarmDate = sdf.parse(nextAlarm); 
     } catch (ParseException e) { 
      e.printStackTrace(); 
     } 
     int alarmDay = alarmDate.getDay(); 
     Calendar now = Calendar.getInstance(); 
     Date currentDate = now.getTime(); 
     int todayDay = currentDate.getDay(); 
     int daysDiff = 0; 
     if (todayDay > alarmDay) { 
      daysDiff = ((7 + (alarmDay - todayDay)) % 7) + 1; 
     } else { 
      daysDiff = ((7 + (alarmDay - todayDay)) % 7); 
     } 
     now.add(Calendar.DATE, daysDiff); 
     AlarmFulldate = now.getTime(); 
     AlarmFulldate.setHours(alarmDate.getHours()); 
     AlarmFulldate.setMinutes(alarmDate.getMinutes()); 
     AlarmFulldate.setSeconds(0); 
    } 
}  

長米利斯= AlarmFulldate.getTime()來做到這一點 - 新的Date()的getTime()。 變毫米將有毫秒。

使用下面的代碼獲取天,米利斯小時和分鐘

days = (int) (millis/(1000 * 60 * 60 * 24)); 
hours = (int) ((millis - (1000 * 60 * 60 * 24 * days))/(1000 * 60 * 60)); 
min = (int) (millis - (1000 * 60 * 60 * 24 * days) - (1000 * 60 * 60 * hours))/(1000 * 60); 

希望這將幫助你

+0

AlarmFulldate只需用「日期」被替換 – MobileMon