使用TimePickerDialog
從用戶獲得runDelay
這樣做
類變量:
private TimePickerDialog time_picker;
private Calendar targetCal;
類構造函數
public YOurClassName() {
final Calendar c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
mHour = c.get(Calendar.HOUR_OF_DAY);
mMinute = c.get(Calendar.MINUTE);
}
裏面的onCreate()或onActivityCreated( ):
time_picker = new TimePickerDialog(getActivity(), mTimeSetListener,
mHour, mMinute, false);
裏面的類:
private TimePickerDialog.OnTimeSetListener mTimeSetListener = new TimePickerDialog.OnTimeSetListener() {
// the callback received when the user "sets" the TimePickerDialog in
// the dialog
public void onTimeSet(TimePicker view, int hourOfDay, int min) {
hour = hourOfDay;
minute = min;
Calendar calNow = Calendar.getInstance();
Calendar calSet = (Calendar) calNow.clone();
calSet.set(Calendar.HOUR_OF_DAY, hour);
calSet.set(Calendar.MINUTE, minute);
calSet.set(Calendar.SECOND, 0);
calSet.set(Calendar.MILLISECOND, 0);
if (calSet.compareTo(calNow) <= 0) {
// Today Set time passed, count to tomorrow
calSet.add(Calendar.DATE, 1);
}
targetCal = calSet;
}
};
在代碼中使用的targetCal
這樣
alarm.set(AlarmManager.RTC, targetCal.getTimeInMillis(), pending_intent);
我希望這會幫助你
爲什麼?如果這樣做,爲什麼要花費時間和精力?如果你真的很討厭它,寫一個函數來封裝那些邏輯。 –
我想從當前時間獨立。 – theroom101
「獨立於當前時間」是什麼意思?這並沒有太多意義。由Independing提供的 –