如何正确取消ClockApp中由AlarmManager设置的AlarmClock警报?

编程入门 行业动态 更新时间:2024-10-23 21:27:21
本文介绍了如何正确取消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

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

推荐答案

因此,我发现我需要使用 ACTION_DISMISS_ALARM 而不是ACTION_SET_ALARM .它可以让您取消闹钟中已设置的闹钟.

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警报?

本文发布于:2023-11-27 10:23:12,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1637685.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:警报   如何正确   AlarmManager   ClockApp   AlarmClock

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!