设置 3 个不同的闹钟 android
Set 3 different alarms android
我知道有很多关于这个主题的问题,但其中 none 对我正在尝试做的事情有帮助,所以我希望你能帮助我。
我想设置 3 个在一天中的不同时间触发的警报,假设上午 9 点、下午 1 点,6PM.The 有关时间的信息保存在 sqlite 数据库中。
我逐行读取我的数据库并设置每小时闹钟。对于 id,我只使用一个数字。
这是我正在使用的代码。它仅在我设置一个闹钟时有效,但如果我尝试设置更多闹钟,则会设置 none 个。
private void setAlarm() throws ParseException {
DBHelper helper = new DBHelper(Main3Activity.this);
Cursor cursor = helper.getQueryDB();
cursor.moveToPosition(-1);
int i = 1;
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(getBaseContext(), AlarmReceiver.class);
while (cursor.moveToNext()) {
final String name = cursor.getString(1);
final String time = cursor.getString(2);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), i, intent, 0);
Calendar t = parseTime(time);
alarmManager.set(AlarmManager.RTC_WAKEUP, t.getTimeInMillis(), pendingIntent);
i++;
}
}
public Calendar parseTime (String t) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("EE MMM dd HH:mm:ss z yyyy");
Calendar cal = Calendar.getInstance();
cal.setTime(sdf.parse(t));
return cal;
}
您只需要设置一个闹钟,当它触发时设置下一个。
代码很好,我忘了把接收器放在 android 清单上。
我的错!
我知道有很多关于这个主题的问题,但其中 none 对我正在尝试做的事情有帮助,所以我希望你能帮助我。 我想设置 3 个在一天中的不同时间触发的警报,假设上午 9 点、下午 1 点,6PM.The 有关时间的信息保存在 sqlite 数据库中。
我逐行读取我的数据库并设置每小时闹钟。对于 id,我只使用一个数字。
这是我正在使用的代码。它仅在我设置一个闹钟时有效,但如果我尝试设置更多闹钟,则会设置 none 个。
private void setAlarm() throws ParseException {
DBHelper helper = new DBHelper(Main3Activity.this);
Cursor cursor = helper.getQueryDB();
cursor.moveToPosition(-1);
int i = 1;
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(getBaseContext(), AlarmReceiver.class);
while (cursor.moveToNext()) {
final String name = cursor.getString(1);
final String time = cursor.getString(2);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), i, intent, 0);
Calendar t = parseTime(time);
alarmManager.set(AlarmManager.RTC_WAKEUP, t.getTimeInMillis(), pendingIntent);
i++;
}
}
public Calendar parseTime (String t) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("EE MMM dd HH:mm:ss z yyyy");
Calendar cal = Calendar.getInstance();
cal.setTime(sdf.parse(t));
return cal;
}
您只需要设置一个闹钟,当它触发时设置下一个。
代码很好,我忘了把接收器放在 android 清单上。 我的错!