如何正确取消ClockApp中由AlarmManager设置的AlarmClock警报? [英] How to correctly cancel an AlarmClock alarm set by AlarmManager in the Clock app?

查看:470
本文介绍了如何正确取消ClockApp中由AlarmManager设置的AlarmClock警报?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题已经在此处此处.但是,OP并没有确认它们是否正常工作,而就我而言,由同一PendingIntent设置的警报不会被取消. 是否可以取消AlarmClock警报?

This question already has answers here, here and here. But they were not confirmed by OPs to be working, and in my case, the alarm set by the same PendingIntent doesn't get canceled. Is there a way to cancel an AlarmClock alarm?

@Override
protected void onHandleIntent(@Nullable Intent i) {
    Intent i = new Intent(AlarmClock.ACTION_SET_ALARM);
    i.putExtra(AlarmClock.EXTRA_SKIP_UI, true);
    i.putExtra(AlarmClock.EXTRA_HOUR, 6);
    i.putExtra(AlarmClock.EXTRA_MINUTES, 0);
    i.putExtra(AlarmClock.EXTRA_MESSAGE, "Good Morning");
    i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

    AlarmManager alarmMgr = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
    PendingIntent alarmIntent = PendingIntent.getActivity(
                    this, 0, i, PendingIntent.FLAG_UPDATE_CURRENT);
    alarmMgr.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,
                SystemClock.elapsedRealtime(),
                alarmIntent);
    Log.d(TAG, "Alarm set");

    try {
        Thread.sleep(15000);
        alarmMgr.cancel(alarmIntent);
        Log.i(TAG, "Alarm canceled");
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}

此代码按预期输出:

Alarm set
Alarm canceled

但是它不会取消设置的警报.我在做什么错了?

推荐答案

因此,我发现我需要使用

So, I found that I needed to use ACTION_DISMISS_ALARM instead of ACTION_SET_ALARM. It lets you cancel already set alarms in the alarm clock.

此处这里这里建议取消挂起"意图,该意图对我不起作用,并且未确认是还是为问题作者工作.

The answers here, here, and here suggested cancelling the PendingIntent which didn't work for me and was not confirmed to be working for the question authors either.

我认为为什么无法从AlarmManager取消PendingIntent的解释可能是因为那里有一个PendingIntent可以让您做某事或稍后发送该Intent.一旦完成(已经发送了意图),就无法撤消它.例如,您可以取消通知,但不能撤消已从通知中打开应用程序(执行的意图或操作).而且,就我而言,这是我需要为其解除警报的另一个应用程序,因此也许我不应该希望能够撤消该操作. 因此,要消除或取消警报,您需要使用操作ACTION_DISMISS_ALARM发送新的意图.

The explanation for why cancelling the PendingIntent from AlarmManager doesn't work, I think, could be that a PendingIntent is there to let you do something or send the intent later on. Once you've already done it (already sent the intent) you can't undo it. For example, you can cancel a notification, but can't undo opening the app from the notification (the intent or action performed) as it's already done. Moreover, in my case, it was another app that I needed to unset the alarm for so maybe I shouldn't have expected to be able to undo that action. So in order to dismiss or cancel the alarm, you need to send a new intent with the action ACTION_DISMISS_ALARM.

按如下所示替换try/catch块可以正确设置并取消警报:

Replacing the try/catch block as follows sets and cancels the alarm correctly for me:

try {
    if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.M) {
        return;
    }
    Thread.sleep(15000);
    Intent ci = new Intent(AlarmClock.ACTION_DISMISS_ALARM);
    ci.setData(i.getData());
    alarmIntent = PendingIntent.getActivity(this, 0, ci, 0);
    alarmMgr.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,
                SystemClock.elapsedRealtime(),
                alarmIntent);
    Log.i(TAG, "Alarm cancelled");
} catch (InterruptedException e) {
    e.printStackTrace();
}

但是,这仅适用于API级别23 + ,并且不会像ACTION_SET_ALARM中那样让您SKIP_UI.

However, this only works for API level 23+ and doesn't let you SKIP_UI like in ACTION_SET_ALARM.

这篇关于如何正确取消ClockApp中由AlarmManager设置的AlarmClock警报?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆