如何取消这个重复的闹钟呢?

发布于 2024-09-11 20:24:27 字数 1091 浏览 10 评论 0原文

我正在写一些类似于用户提醒的东西。用户将为他们的事件设置提醒,当时间到来时,将设置重复闹钟以触发状态栏通知。但在我选择通知或清除通知后,闹钟似乎不停。我不知道在哪里可以取消这个重复的警报。下面是一些代码: 在我的主要活动中设置重复闹钟

alarmTime = Calendar.getInstance();
Intent intent = new Intent(this, AlarmReceive.class);
PendingIntent sender = PendingIntent.getBroadcast(this, 0, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);

alarmTime.add(Calendar.MINUTE,offset_time);

//Schedule the alarm
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, alarmTime.getTimeInMillis(), 30 * 1000, sender);

在我的 OnReceive 方法中,我只是在状态栏中显示通知并将标志设置为 FLAG_AUTO_CANCEL

manager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);

// Set the icon, scrolling text and timestamp
Notification notification = new Notification(R.drawable.medical, text, System.currentTimeMillis());

PendingIntent contentIntent = PendingIntent.getActivity(context, 0, i, 0);

notification.flags = Notification.FLAG_AUTO_CANCEL;

manager.notify(R.string.service_text, notification);

当用户选择通知或清除通知时如何停止闹钟?

I'm writing something like a reminder for users. Users will set reminders for their events, when the time comes, a repeating alarm will be set to trigger a status bar notification. But the alarm seems non-stop after I selected the notification or cleared the notification. I am not sure where to cancel this repeating alarm. Below are some of the codes:
Set up the repeating alarm in my main activity

alarmTime = Calendar.getInstance();
Intent intent = new Intent(this, AlarmReceive.class);
PendingIntent sender = PendingIntent.getBroadcast(this, 0, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);

alarmTime.add(Calendar.MINUTE,offset_time);

//Schedule the alarm
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, alarmTime.getTimeInMillis(), 30 * 1000, sender);

In my OnReceive method, I just display the notification in status bar and set the flag as FLAG_AUTO_CANCEL

manager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);

// Set the icon, scrolling text and timestamp
Notification notification = new Notification(R.drawable.medical, text, System.currentTimeMillis());

PendingIntent contentIntent = PendingIntent.getActivity(context, 0, i, 0);

notification.flags = Notification.FLAG_AUTO_CANCEL;

manager.notify(R.string.service_text, notification);

How can I stop the alarm when the user selects the notification or clears it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

可爱咩 2024-09-18 20:24:27

使用与 setRepeating() 中使用的等效的 PendingIntentAlarmManager 调用 cancel()

Intent intent = new Intent(this, AlarmReceive.class);
PendingIntent sender = PendingIntent.getBroadcast(this, 0, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);

alarmManager.cancel(sender);

Call cancel() on AlarmManager with an equivalent PendingIntent to the one you used with setRepeating():

Intent intent = new Intent(this, AlarmReceive.class);
PendingIntent sender = PendingIntent.getBroadcast(this, 0, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);

alarmManager.cancel(sender);
所谓喜欢 2024-09-18 20:24:27

我尝试了各种方法都无法奏效,所以我决定搞一个肮脏的把戏。当我想取消重复闹钟时,我使用创建闹钟的相同方法(因此替换旧闹钟),然后立即取消它。使用此方法,如果布尔变量设置为 true,它将创建一个警报流并替换,然后取消具有相同 id 的替换:

static void CancelRepeatingAlarm(Context context, boolean creating){
    //if it already exists, then replace it with this one
    Intent alertIntent = new Intent(context, AlertReceiver.class);
    PendingIntent timerAlarmIntent = PendingIntent
            .getBroadcast(context, 100, alertIntent,PendingIntent.FLAG_CANCEL_CURRENT); 
    AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    if (creating){
        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), INTERVAL, timerAlarmIntent);
    }else{
        alarmManager.cancel(timerAlarmIntent);
    }

I tried various methods and couldnt get it to work, so I decided to do a dirty trick. When I want to cancel my repeating alarm, I use the same method that created my alarm (and hence replacing the old one) and then cancel it right away. With this method if the boolean variable is set to true it creates an alarm ow it replaces and then cancels the replacement which has the same id:

static void CancelRepeatingAlarm(Context context, boolean creating){
    //if it already exists, then replace it with this one
    Intent alertIntent = new Intent(context, AlertReceiver.class);
    PendingIntent timerAlarmIntent = PendingIntent
            .getBroadcast(context, 100, alertIntent,PendingIntent.FLAG_CANCEL_CURRENT); 
    AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    if (creating){
        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), INTERVAL, timerAlarmIntent);
    }else{
        alarmManager.cancel(timerAlarmIntent);
    }
只是一片海 2024-09-18 20:24:27

在您的 MainActivity 中,设置闹钟时间。如果您要使用多个闹钟,请使用 SharedPreferences 存储它们各自的 ID。代码如下:

PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, _id,intent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager = (AlarmManager) getSystemService(Activity.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(),
            pendingIntent);

public static Context getContext() {
    return mContext;
}
mContext=mainactivity.this;

在您的第二个 Activity 中,使用 SharedPreferences 中的相同 ID。在我的代码中,我从 ArrayList 中获取 ID,Alarm_id。最后,您可以通过 MainActivity.getContext() 使用 MainActivity 上下文。这是代码:

AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intentAlarm = new Intent(AlarmListviewActivity.this,
        MainActivity.class);
PendingIntent morningIntent = PendingIntent.getBroadcast(MainActivity.getContext(), Alarm_id.get(positon),
        intentAlarm, PendingIntent.FLAG_CANCEL_CURRENT);

alarmManager.cancel(morningIntent);
morningIntent.cancel();

In your MainActivity, set the alarm time. If you're going to use multiple alarms, use SharedPreferences to store their respective IDs. Here is the code:

PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, _id,intent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager = (AlarmManager) getSystemService(Activity.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(),
            pendingIntent);

public static Context getContext() {
    return mContext;
}
mContext=mainactivity.this;

In your second Activity use same ID from SharedPreferences. In my code, I get the ID from the ArrayList, Alarm_id. Last, you can use the MainActivity context here with MainActivity.getContext(). Here is the code:

AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intentAlarm = new Intent(AlarmListviewActivity.this,
        MainActivity.class);
PendingIntent morningIntent = PendingIntent.getBroadcast(MainActivity.getContext(), Alarm_id.get(positon),
        intentAlarm, PendingIntent.FLAG_CANCEL_CURRENT);

alarmManager.cancel(morningIntent);
morningIntent.cancel();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文