这是如何重置警报?
How is this resetting the alarm?
我一直在阅读本教程以创建重复应用程序:
http://rdyonline.net/android-bytes-alarms/
我已按照说明进行操作,效果非常好。但是,如果我不明白它是如何工作的,我不喜欢使用它。
警报管理器使用的版本 >= 19,因此不需要重复(准确)警报,它需要一个一次性警报,该警报在退出意图时重置。
现在正如我所说的那样,它每 15 分钟就会关闭一次(在我的版本中)。我可以看到他们将数据与意图捆绑在一起,但我真的不明白是什么重新触发了单次警报。
这是他们的代码:
Repeating alarms
If you’re targeting any Android version before API 19 (KitKat), or,
you don’t need them to be exact then repeating alarms are nice and
easy. All you need in this case is to use the setRepeating call.
In some cases, it will be important that you set a repeating alarm
that is accurate, I’ll go in to a little more detail on how to handle
this.
The trick here is to make sure you schedule the next alarm once the
previous alarm goes off. You’ll have to check whether the alarm you
have set is intended to be repeated and also make sure the platform
you’re running on is above API 19
@Override
public void onReceive(android.content.Context context,
android.content.Intent intent) {
WrappedAlarmManager am = new WrappedAlarmManager(context);
Bundle extras = intent.getExtras();
if (am.isSingleAlarm(extras)) {
Toast.makeText(context, "Single alarm", Toast.LENGTH_SHORT).show();
} else if (am.isRepeatAlarm(extras)) {
Toast.makeText(context, "Repeat alarm", Toast.LENGTH_SHORT).show();
if (android.os.Build.VERSION.SDK_INT >= 19) {
am.scheduleRepeatingAlarm(context);
}
}
}
A quick check to see if it’s a repeating alarm and then the repeating
alarm is scheduled again. Below are the two pertinent methods to deal
with this logic:
public boolean isRepeatAlarm(Bundle extras) {
return extras.containsKey(KEY_REPEAT) && extras.getBoolean(KEY_REPEAT);
}
public void scheduleRepeatingAlarm(Context context) {
Intent intent = new Intent(context, NotificationReceiver.class);
Bundle extras = new Bundle();
extras.putBoolean(KEY_REPEAT, true);
intent.putExtras(extras);
PendingIntent pIntent = PendingIntent.getBroadcast(context,
REPEAT_ALARM_ID, intent, PendingIntent.FLAG_CANCEL_CURRENT);
Calendar futureDate = Calendar.getInstance();
futureDate.add(Calendar.SECOND, (int) (INTERVAL_SEVEN_SECONDS / 1000));
if (android.os.Build.VERSION.SDK_INT >= 19) {
setSingleExactAlarm(futureDate.getTime().getTime(), pIntent);
} else {
mAlarmManager.setRepeating(AlarmManager.RTC_WAKEUP, futureDate
.getTime().getTime(), INTERVAL_SEVEN_SECONDS, pIntent);
}
}
感谢您的帮助
要在 SDK versions >= 19
中安排重复警报,当收到警报广播时,将检查布尔值 KEY_REPEAT
,如果为真,则为 [=12= 安排一个确切的警报】 秒后。
INTERVAL_SEVEN_SECONDS / 1000
秒后再次收到广播并设置下一个警报。
这种接收广播和调度下一个报警周期不断地重复。
我一直在阅读本教程以创建重复应用程序:
http://rdyonline.net/android-bytes-alarms/
我已按照说明进行操作,效果非常好。但是,如果我不明白它是如何工作的,我不喜欢使用它。
警报管理器使用的版本 >= 19,因此不需要重复(准确)警报,它需要一个一次性警报,该警报在退出意图时重置。
现在正如我所说的那样,它每 15 分钟就会关闭一次(在我的版本中)。我可以看到他们将数据与意图捆绑在一起,但我真的不明白是什么重新触发了单次警报。
这是他们的代码:
Repeating alarms
If you’re targeting any Android version before API 19 (KitKat), or, you don’t need them to be exact then repeating alarms are nice and easy. All you need in this case is to use the setRepeating call.
In some cases, it will be important that you set a repeating alarm that is accurate, I’ll go in to a little more detail on how to handle this.
The trick here is to make sure you schedule the next alarm once the previous alarm goes off. You’ll have to check whether the alarm you have set is intended to be repeated and also make sure the platform you’re running on is above API 19
@Override
public void onReceive(android.content.Context context,
android.content.Intent intent) {
WrappedAlarmManager am = new WrappedAlarmManager(context);
Bundle extras = intent.getExtras();
if (am.isSingleAlarm(extras)) {
Toast.makeText(context, "Single alarm", Toast.LENGTH_SHORT).show();
} else if (am.isRepeatAlarm(extras)) {
Toast.makeText(context, "Repeat alarm", Toast.LENGTH_SHORT).show();
if (android.os.Build.VERSION.SDK_INT >= 19) {
am.scheduleRepeatingAlarm(context);
}
}
}
A quick check to see if it’s a repeating alarm and then the repeating alarm is scheduled again. Below are the two pertinent methods to deal with this logic:
public boolean isRepeatAlarm(Bundle extras) {
return extras.containsKey(KEY_REPEAT) && extras.getBoolean(KEY_REPEAT);
}
public void scheduleRepeatingAlarm(Context context) {
Intent intent = new Intent(context, NotificationReceiver.class);
Bundle extras = new Bundle();
extras.putBoolean(KEY_REPEAT, true);
intent.putExtras(extras);
PendingIntent pIntent = PendingIntent.getBroadcast(context,
REPEAT_ALARM_ID, intent, PendingIntent.FLAG_CANCEL_CURRENT);
Calendar futureDate = Calendar.getInstance();
futureDate.add(Calendar.SECOND, (int) (INTERVAL_SEVEN_SECONDS / 1000));
if (android.os.Build.VERSION.SDK_INT >= 19) {
setSingleExactAlarm(futureDate.getTime().getTime(), pIntent);
} else {
mAlarmManager.setRepeating(AlarmManager.RTC_WAKEUP, futureDate
.getTime().getTime(), INTERVAL_SEVEN_SECONDS, pIntent);
}
}
感谢您的帮助
要在 SDK versions >= 19
中安排重复警报,当收到警报广播时,将检查布尔值 KEY_REPEAT
,如果为真,则为 [=12= 安排一个确切的警报】 秒后。
INTERVAL_SEVEN_SECONDS / 1000
秒后再次收到广播并设置下一个警报。
这种接收广播和调度下一个报警周期不断地重复。