gpt4 book ai didi

android - 检测现有警报

转载 作者:行者123 更新时间:2023-11-30 01:51:22 25 4
gpt4 key购买 nike

我用这个函数来创建闹钟,如果已经创建了闹钟,则取消闹钟,如果没有,则创建闹钟

但它不起作用,alarmUp 永远不会为 false

如何判断告警是否存在?

public void alarm(int hour, int minutes)
{
Intent intent = new Intent(getApplicationContext(),AlarmActivity.class);
PendingIntent sender = PendingIntent.getActivity(getApplicationContext(), 0, intent, 0);

Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR, 24);
calendar.set(Calendar.HOUR_OF_DAY, hour);
calendar.set(Calendar.MINUTE, minutes);
calendar.set(Calendar.SECOND, 0);
boolean alarmUp = (PendingIntent.getActivity(getApplicationContext(), 0, intent, PendingIntent.FLAG_NO_CREATE) != null);

AlarmManager am = (AlarmManager) getApplicationContext().getSystemService(getApplicationContext().ALARM_SERVICE);
if(!alarmUp)
{
am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), sender);
}
else
{
am.cancel(sender);
}
}

最佳答案

你的想法是对的,但是顺序错了。你需要这样做:

boolean alarmUp = (PendingIntent.getActivity(getApplicationContext(), 0, intent, PendingIntent.FLAG_NO_CREATE) != null);

在你这样做之前:

PendingIntent sender = PendingIntent.getActivity(getApplicationContext(), 0, intent, 0);

alarmUp 始终为真的原因是因为您之前刚刚自己创建了 PendingIntent!

关于android - 检测现有警报,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32994252/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com