我正在编写一个从TimePicker获取小时/分钟值的应用程序,在屏幕上显示它们并在AlarmManager中设置这些值以响铃。
我可以正确显示屏幕上的小时和分钟,但我无法存储它们以使用alarmManager。我怎么能这样做?
public class TimePickerFragment extends DialogFragment implements TimePickerDialog.OnTimeSetListener {
Calendar calendar = Calendar.getInstance();
int hour = calendar.get(Calendar.HOUR_OF_DAY);
int minute = calendar.get(Calendar.MINUTE);
static final int timePicker1 = 1;
//need to store values in here
static int aHour1, aMin1;
private int mChosenTime = 1;
int cur = 0;
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Bundle bundle = this.getArguments();
if (bundle != null) {
mChosenTime = bundle.getInt("TIME", 1);
}
switch (mChosenTime) {
case timePicker1:
cur = timePicker1;
return new TimePickerDialog(getActivity(),AlertDialog.THEME_HOLO_DARK, this, hour, minute, DateFormat.is24HourFormat(getActivity()));
}
return null;
}
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
if (cur == timePicker1) {
TextView alarmTimePicker01 = (TextView) getActivity().findViewById(R.id.alarmTimePicker01);
alarmTimePicker01.setText(String.valueOf(hourOfDay) + ":" + String.valueOf(minute));
aHour1 = valueOf(hourOfDay);
aMin1 = valueOf(minute);
}
(*我认为我的代码是某种......'奇怪'。抱歉:()
答案 0 :(得分:1)
你可以使用这样的东西
Calendar cal= Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, 9);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
PendingIntent pi = PendingIntent.getService(context, 0,
new Intent(context, MyClass.class),PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, pi);
希望这适合你。
答案 1 :(得分:0)
请检查代码。我通过以下代码在我的应用程序中实现...
final Calendar c = Calendar.getInstance();
mHour = c.get(Calendar.HOUR_OF_DAY);
mMinute = c.get(Calendar.MINUTE);
TimePickerDialog timePickerDialog = new TimePickerDialog(this,
new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay,
int minute) {
String AM_PM = " AM";
String mm_precede = "";
if (hourOfDay >= 12) {
AM_PM = " PM";
if (hourOfDay >= 13 && hourOfDay < 24) {
hourOfDay -= 12;
} else {
hourOfDay = 12;
}
} else if (hourOfDay == 0) {
hourOfDay = 12;
}
if (minute < 10) {
mm_precede = "0";
}
editTeimet.setText(hourOfDay + ":" + mm_precede + minute + AM_PM);
}
}, mHour, mMinute, false);
timePickerDialog.show();