我想從時間選擇器中選擇一個時間并使用 AlarmManager 為那個時間設定一個鬧鐘。當我使用時when.getTimeInMillis 回傳的值與我設定的時間不同。
例如,如果我將時間設定為 20:40 并列印when.getTime我從第二天開始得到 17:12 的值。
為什么會這樣?
從時間選擇器獲取時間
MedicationReminder mr=new MedicationReminder(getApplicationContext());
int hour = tp.getCurrentHour();
int min = tp.getCurrentMinute();
Calendar c=Calendar.getInstance();
c.add(Calendar.HOUR, hour);
c.add(Calendar.MINUTE, min);
mr.setReminder(new Medication(name,quant_aux,time),c);
設定鬧鐘
Intent i = new Intent(mContext, MedicationReceiver.class);
i.putExtra("medName",medication.getName());
i.putExtra("medQuant",medication.getQuantity());
PendingIntent pi=PendingIntent.getBroadcast(mContext,0,i,0);
mAlarmManager.set(AlarmManager.RTC_WAKEUP,when.getTimeInMillis(),pi);
uj5u.com熱心網友回復:
代替
Calendar c=Calendar.getInstance();
c.add(Calendar.HOUR, hour);
c.add(Calendar.MINUTE, min);
嘗試做
Calendar c=Calendar.getInstance();
c.set(Calendar.HOUR, hour);
c.set(Calendar.MINUTE, min);
如果要使用 24 小時制,請使用
c.set(Calendar.HOUR_OF_DAY, hour);
我不是 100% 的問題,因為您沒有包含所有代碼,但我認為您正在將 20 小時添加到當前時間,而不是將日歷設定為 20 小時。
另一個注意事項:我相信 getCurrentHour() 和 getCurrentMinute() 自 API 23 以來已被棄用,因此我建議您改用 getHour() 和 getMinute()。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/416051.html
標籤:
上一篇:SyntaxError:使用Selenium和ChromeDriver在XPath中掃描三引號字串文字時出現EOF?
