我知道很多关于这个话题的问题,但他们没有一个能帮助我做我想做的事情,所以我希望你能帮助我。 我想设置在一天中的不同时间触发的3个闹钟,让我们去上午9点,下午1点,下午6点。关于小时的信息保存在sqlite数据库中。设置3个不同的闹钟android
我逐行读取数据库并设置每小时报警。对于我只使用一个数字的ID。
这是我正在使用的代码。它只在设置一个闹钟时有效,但如果我尝试设置更多闹钟,则不会设置它们。
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;
}
你能开发更多的答案吗?我不完全明白你的意思。 – cotita