设置重复闹钟
Set a repeating alarm
9我在做一个定时提醒你吃药的app,让用户给我频率和时间段,例如:
频率 = 8 小时
经期 = 7 天
所以我会制作一个警报,在 7 天内每 8 小时触发一次。这是我现在的代码,但它没有按照我想要的方式工作......(它同时发送所有通知,所以在上面的例子中它一次发送 21 个通知,只需几秒钟在我 运行 代码之后,这意味着它不是发送第一个,而是发送所有代码)
private void makeAlarm(MedAlarm mAlarm) {
String medName = mAlarm.getMedName();
String medDescripcion = mAlarm.getMedDescription();
String frequency = mAlarm.getMedIntervalHours();
String duration = mAlarm.getMedIntervalDays();
int lastitem = ((24 / Integer.parseInt(frequency)) *
Integer.parseInt(duration)) - 1;
String alarmMsg = medName;
for (int k = 0; k < ((24 / Integer.parseInt(frequency)) *
Integer.parseInt(duration)); k++) {// hours
setAlarm(Integer.parseInt(frequency) * (k + 1), alarmMsg, k + "",
"0");
Log.i("setAlarm", k + " - ");
Log.i("duration", Integer.parseInt(frequency) * (k + 1) + "");
if (k == lastitem) {
setAlarm(Integer.parseInt(frequency) * (k + 1), alarmMsg, k + "",
"1");
}
}
}
/**
* @param duration
* @param name
* @param id
* @param lastitem 0 normal item, 1 last item
*/
private void setAlarm(int duration, String name, String id, String lastitem)
{
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
Intent alertIntent = new Intent(this, AlertReceiver.class);
alertIntent.putExtra("message", name);
alertIntent.putExtra("id", id);
alertIntent.putExtra("lastitem", lastitem);
PendingIntent pi = PendingIntent.getBroadcast(this, duration++,
alertIntent, PendingIntent.FLAG_UPDATE_CURRENT);
int alarmType = AlarmManager.ELAPSED_REALTIME;
final int FIFTEEN_SEC_MILLIS = 15000;
final int HOURS_MILLIS = 3600000 * duration;
AlarmManager alarmManager = (AlarmManager)
this.getSystemService(this.ALARM_SERVICE);
alarmManager.setRepeating(alarmType, SystemClock.elapsedRealtime() +
FIFTEEN_SEC_MILLIS,
HOURS_MILLIS, pi);
Log.i("intent sent", HOURS_MILLIS + " || ");
}
并且 class AlertReciver 是发送通知的那个。
如果你能帮我解决这个问题并告诉我我缺少什么,或者我是否应该以不同的方式解决这个问题。
谢谢。
使用 alarmManager.set()
而不是 alarmManager.setRepeating()
。
更新您的 setAlarm()
方法如下:
private void setAlarm(int duration, String name, String id, String lastitem)
{
Intent alertIntent = new Intent(this, AlertReceiver.class);
alertIntent.putExtra("message", name);
alertIntent.putExtra("id", id);
alertIntent.putExtra("lastitem", lastitem);
PendingIntent pi = PendingIntent.getBroadcast(this, duration++,
alertIntent, PendingIntent.FLAG_UPDATE_CURRENT);
int alarmType = AlarmManager.ELAPSED_REALTIME;
final int FIFTEEN_SEC_MILLIS = 15000;
final int HOURS_MILLIS = 3600000 * duration;
AlarmManager alarmManager = (AlarmManager) this.getSystemService(this.ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis() + HOURS_MILLIS);
alarmManager.set(alarmType, calendar.getTimeInMillis() + FIFTEEN_SEC_MILLIS, pi);
Log.i("intent sent", System.currentTimeMillis() + HOURS_MILLIS + " || ");
}
希望对你有所帮助~
9我在做一个定时提醒你吃药的app,让用户给我频率和时间段,例如:
频率 = 8 小时 经期 = 7 天
所以我会制作一个警报,在 7 天内每 8 小时触发一次。这是我现在的代码,但它没有按照我想要的方式工作......(它同时发送所有通知,所以在上面的例子中它一次发送 21 个通知,只需几秒钟在我 运行 代码之后,这意味着它不是发送第一个,而是发送所有代码)
private void makeAlarm(MedAlarm mAlarm) {
String medName = mAlarm.getMedName();
String medDescripcion = mAlarm.getMedDescription();
String frequency = mAlarm.getMedIntervalHours();
String duration = mAlarm.getMedIntervalDays();
int lastitem = ((24 / Integer.parseInt(frequency)) *
Integer.parseInt(duration)) - 1;
String alarmMsg = medName;
for (int k = 0; k < ((24 / Integer.parseInt(frequency)) *
Integer.parseInt(duration)); k++) {// hours
setAlarm(Integer.parseInt(frequency) * (k + 1), alarmMsg, k + "",
"0");
Log.i("setAlarm", k + " - ");
Log.i("duration", Integer.parseInt(frequency) * (k + 1) + "");
if (k == lastitem) {
setAlarm(Integer.parseInt(frequency) * (k + 1), alarmMsg, k + "",
"1");
}
}
}
/**
* @param duration
* @param name
* @param id
* @param lastitem 0 normal item, 1 last item
*/
private void setAlarm(int duration, String name, String id, String lastitem)
{
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
Intent alertIntent = new Intent(this, AlertReceiver.class);
alertIntent.putExtra("message", name);
alertIntent.putExtra("id", id);
alertIntent.putExtra("lastitem", lastitem);
PendingIntent pi = PendingIntent.getBroadcast(this, duration++,
alertIntent, PendingIntent.FLAG_UPDATE_CURRENT);
int alarmType = AlarmManager.ELAPSED_REALTIME;
final int FIFTEEN_SEC_MILLIS = 15000;
final int HOURS_MILLIS = 3600000 * duration;
AlarmManager alarmManager = (AlarmManager)
this.getSystemService(this.ALARM_SERVICE);
alarmManager.setRepeating(alarmType, SystemClock.elapsedRealtime() +
FIFTEEN_SEC_MILLIS,
HOURS_MILLIS, pi);
Log.i("intent sent", HOURS_MILLIS + " || ");
}
并且 class AlertReciver 是发送通知的那个。
如果你能帮我解决这个问题并告诉我我缺少什么,或者我是否应该以不同的方式解决这个问题。
谢谢。
使用 alarmManager.set()
而不是 alarmManager.setRepeating()
。
更新您的 setAlarm()
方法如下:
private void setAlarm(int duration, String name, String id, String lastitem)
{
Intent alertIntent = new Intent(this, AlertReceiver.class);
alertIntent.putExtra("message", name);
alertIntent.putExtra("id", id);
alertIntent.putExtra("lastitem", lastitem);
PendingIntent pi = PendingIntent.getBroadcast(this, duration++,
alertIntent, PendingIntent.FLAG_UPDATE_CURRENT);
int alarmType = AlarmManager.ELAPSED_REALTIME;
final int FIFTEEN_SEC_MILLIS = 15000;
final int HOURS_MILLIS = 3600000 * duration;
AlarmManager alarmManager = (AlarmManager) this.getSystemService(this.ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis() + HOURS_MILLIS);
alarmManager.set(alarmType, calendar.getTimeInMillis() + FIFTEEN_SEC_MILLIS, pi);
Log.i("intent sent", System.currentTimeMillis() + HOURS_MILLIS + " || ");
}
希望对你有所帮助~