大家。
我有这段代码。
pending_intent = PendingIntent.getActivity(context, 0, intent, 0);
alarm = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
alarm.set(AlarmManager.RTC, System.currentTimeMillis() + runDelay, pending_intent);
我想摆脱System.currentTimeMillis()+ runDelay,并只传递runDelay变量intofunction,有人知道我应该怎么做吗?
P.S。我需要代码为API8
非常感谢。
答案 0 :(得分:1)
使用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);
我希望这会对你有所帮助