Android 中如何处理闹钟通知?
我正在为 Android 开发一个媒体播放器应用程序,为此我需要处理任何警报通知,并基于此我将暂停播放。当闹钟暂停或解除时,我将恢复播放。
我在谷歌上搜索了很多有关警报处理的信息,但我发现的是通过代码启用警报通知、设置意图然后处理它的方法。但是,我无法找到仅处理警报通知部分的位置。我不需要打开警报,它可以由用户设置,而且我不需要以编程方式设置。我所需要的只是处理该通知。
关于这方面的任何想法都会非常有用吗?
谢谢, 阿希什
I'm developing an media player application for Android, for which I need to handle any Alarm notification, and based on that I'll pause my playback. When the Alarm in snoozed or dismissed, I'll then resume the playback.
I googled a lot for the Alarm handling, but what I found was the way to enable Alarm notifications through code, set the intent and then handle it. However, no where could I locate just handling the Alarm notification part. I don't need to set the Alarm on, it could've been set by the user, and I don't need to programmatically. All I need is just handle that notification.
Any ideas on this would be extremely useful?
Thanks,
Asheesh
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您好,Asheesh Vashishtha,
纠正我这一点,但是据我所知,每当任何其他应用程序(即使是闹钟)被激活时,您的活动肯定会进入后台。所以我想你可以重写 OnPause 和 OnResume 函数来放置你的代码。就贪睡或其他事情而言,它们都会导致警报活动被破坏(或暂停,对此不太了解),并且您的活动将恢复。所以这不会是你担心的问题!
希望这有帮助...
HI Asheesh Vashishtha,
Correct me on this, but AFAIK whenever any other application even if it is the alarm clock, is activated, your activity will surely go in background. So i guess u can override the
OnPause
andOnResume
functions to put your bit of code. As far as snooze or other things are concerned, they all will result in the Alarm Activity getting destroyed(or paused, don know much about it) and your activity will get resumed. So that wont be a matter of concern for u!Hope this helps...
AFAIK,您无法收到有关闹钟应用程序功能的通知,就像您无法收到有关任何其他第三方闹钟的通知一样。
请注意,
AlarmManager
(您可能正在阅读的内容)与 Alarm Clock 应用程序不同。对不起!
AFAIK, there is no way for you to be notified of what the Alarm Clock application does, any more than you get notified about any other third-party alarm clock.
Note that
AlarmManager
-- what you were probably reading about -- is not the same as the Alarm Clock application.Sorry!
我在开发媒体播放器时遇到了类似的情况。我的解决方案是使用 AudioManager 的 OnAudioFocusChangeListener。
您可以像这样在类中实现侦听器
然后覆盖
onAudioFocusChange
这里的关键是
AudioManager.AUDIOFOCUS_LOSS_TRANSIENT
。这是当闹钟响起时听众不断收到的代码(在 Note 5 上)。因此,我只是通过暂停媒体播放器并释放音频焦点来处理与AudioManager.AUDIOFOCUS_LOSS
相同的AudioManager.AUDIOFOCUS_LOSS_TRANSIENT
。当我们设置媒体播放器时,我在添加数据源之前添加了这一行
确保启动媒体播放器的代码中也包含这一行(我将其放在启动代码和
onResume
代码中如果应用程序在后台时闹钟响起)。当您关闭闹钟后按下播放按钮时,该行可以帮助您重新获得音频焦点。
当您使用完媒体播放器后,您还应该释放音频焦点。我将这行代码放在
onStop
和onDetach
方法中。它的设置并不像您想象的那么多,它允许您在引入意外音频(例如闹钟或计时器响起)时调整媒体播放器。
I ran into a similar situation while developing a media player. My solution was to use the AudioManager's
OnAudioFocusChangeListener
.You implement the listener in the class like so
Then you override
onAudioFocusChange
The key here is
AudioManager.AUDIOFOCUS_LOSS_TRANSIENT
. This was the code the listener kept receiving when the alarm clock would go off (on The Note 5). So I simply handledAudioManager.AUDIOFOCUS_LOSS_TRANSIENT
the same asAudioManager.AUDIOFOCUS_LOSS
by pausing the media player and letting go of the audio focus.When we setup the media player, I added this line before adding the data source
Make sure your code for starting the media player also has this line in it (I have it in the start code and
onResume
code in case the alarm went off while the app was in the background).That line helps you get back the audio focus when you hit the play button after dismissing the alarm clock.
You should also let go off audio focus when you're finished with the media player. I put this line of code in the
onStop
andonDetach
methods.It's not as much setup as you may think and it allows you to adjust your media player whenever unexpected audio is introduced (such as an alarm clock or timer goes off).