Android 重复警报,每次打开时覆盖自我
Android repeating alarm, override self on each open
我有以下警报,在应用程序启动时从 MainActivity 获取 运行。我想我已将其编程为在每次启动新应用程序时覆盖自身。但我发现很难测试。这是否确实会覆盖自身,或者如果我多次关闭和打开应用程序,我会收到很多警报吗?
如何测试我创建了多少个警报?
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = new Intent(this, AlarmBroadcastReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
manager.setRepeating(AlarmManager.RTC_WAKEUP, 5000, 60000, pendingIntent);
}
因此 PendingIntent.getBroadcast(Context context, int requestCode, Intent intent, int flags)
requestCode
是? (我假设为警报设置一个 ID,您可以用新警报覆盖它)
flags
干什么? (告诉闹钟如果新的闹钟ID相同怎么办?)
是的,您覆盖了它,因为每次创建 activity 时您都使用相同的请求代码。 requestcode 是 PendingIntent.getBrodacast() 方法的第二个参数,其操作类似于您的 id 警报以稍后更新它(例如取消它)。您可以使用此代码检查您的闹钟是否已设置:
boolean alarmUp = (PendingIntent.getBroadcast(context, 0,
new Intent("com.my.package.MY_UNIQUE_ACTION"),
PendingIntent.FLAG_NO_CREATE) != null);
但是您没有使用代码设置闹钟,您需要调用:
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, 0 ,pendingIntent);
实际设置闹钟。
关于您的最后一个问题,您可以通过多种方式使用标志:
int FLAG_CANCEL_CURRENT Flag indicating that if the described
PendingIntent already exists, the current one should be canceled
before generating a new one.
int FLAG_IMMUTABLE Flag indicating that the created PendingIntent
should be immutable.
int FLAG_NO_CREATE Flag> indicating that if the described
PendingIntent does not already exist,
then simply return null instead of creating it.
int FLAG_ONE_SHOT Flag indicating that this PendingIntent can be used
only once.
int FLAG_UPDATE_CURRENT Flag indicating that if the described
PendingIntent already exists, then keep it but replace its extra data
with what is in this new Intent.
您可以通过以下方式在 Android 中设置特定时间的闹钟:
Intent intent = new Intent(context, AlarmService.class);
final int _id = Integer.parseInt(""+MCZManagers.timeToMillies(alarmTime));
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, _id, intent, 0);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, MCZManagers.dateTimeToMillies(alarmDateTime), pendingIntent);
一旦你销毁应用程序,闹钟也会被销毁,不再播放。
我有以下警报,在应用程序启动时从 MainActivity 获取 运行。我想我已将其编程为在每次启动新应用程序时覆盖自身。但我发现很难测试。这是否确实会覆盖自身,或者如果我多次关闭和打开应用程序,我会收到很多警报吗?
如何测试我创建了多少个警报?
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = new Intent(this, AlarmBroadcastReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
manager.setRepeating(AlarmManager.RTC_WAKEUP, 5000, 60000, pendingIntent);
}
因此 PendingIntent.getBroadcast(Context context, int requestCode, Intent intent, int flags)
requestCode
是? (我假设为警报设置一个 ID,您可以用新警报覆盖它)flags
干什么? (告诉闹钟如果新的闹钟ID相同怎么办?)
是的,您覆盖了它,因为每次创建 activity 时您都使用相同的请求代码。 requestcode 是 PendingIntent.getBrodacast() 方法的第二个参数,其操作类似于您的 id 警报以稍后更新它(例如取消它)。您可以使用此代码检查您的闹钟是否已设置:
boolean alarmUp = (PendingIntent.getBroadcast(context, 0,
new Intent("com.my.package.MY_UNIQUE_ACTION"),
PendingIntent.FLAG_NO_CREATE) != null);
但是您没有使用代码设置闹钟,您需要调用:
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, 0 ,pendingIntent);
实际设置闹钟。
关于您的最后一个问题,您可以通过多种方式使用标志:
int FLAG_CANCEL_CURRENT Flag indicating that if the described PendingIntent already exists, the current one should be canceled before generating a new one.
int FLAG_IMMUTABLE Flag indicating that the created PendingIntent should be immutable.
int FLAG_NO_CREATE Flag> indicating that if the described PendingIntent does not already exist, then simply return null instead of creating it.
int FLAG_ONE_SHOT Flag indicating that this PendingIntent can be used only once.
int FLAG_UPDATE_CURRENT Flag indicating that if the described PendingIntent already exists, then keep it but replace its extra data with what is in this new Intent.
您可以通过以下方式在 Android 中设置特定时间的闹钟:
Intent intent = new Intent(context, AlarmService.class);
final int _id = Integer.parseInt(""+MCZManagers.timeToMillies(alarmTime));
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, _id, intent, 0);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, MCZManagers.dateTimeToMillies(alarmDateTime), pendingIntent);
一旦你销毁应用程序,闹钟也会被销毁,不再播放。