我正在尝试设置一个闹钟,使其从编辑文本时间选择器中重复,但是该闹钟不起作用。 我没有得到警报管理器和待定意图如何在这里工作。 预先感谢。
alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
chooseTime = findViewById(R.id.etChooseTime);
chooseTime.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
calendar = Calendar.getInstance();
currentHour = calendar.get(Calendar.HOUR_OF_DAY);
currentMinute = calendar.get(Calendar.MINUTE);
timePickerDialog = new TimePickerDialog(MainActivity.this, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker timePicker, int hourOfDay, int minutes) {hours=currentHour;mins=currentMinute;
if (hourOfDay > 12) {
hourOfDay -= 12;
amPm = "PM";
} else if (hourOfDay == 0) {
hourOfDay += 12;
amPm = "AM";
} else if (hourOfDay == 12) {
amPm = "PM";
} else {
amPm = "AM";
}
chooseTime.setText(String.format("%02d:%02d", hourOfDay, minutes) + amPm);
}
}, currentHour, currentMinute, false);
timePickerDialog.show();
}
});
}
这是警报管理器代码。
public void OnToggleClicked(View view)
{
if (((ToggleButton) view).isChecked()) {
int p = hours * 60;
int q = p + mins;
Intent intent = new Intent(this, AlarmReceiver.class);
pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime(),
q * 60 * 1000, pendingIntent);
}
else
{
alarmManager.cancel(pendingIntent);
}
}