Android 中如何处理闹钟通知?

发布于 2024-08-30 10:33:18 字数 244 浏览 7 评论 0原文

我正在为 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 技术交流群。

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

发布评论

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

评论(3

仅此而已 2024-09-06 10:33:18

您好,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 and OnResume 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...

森林散布 2024-09-06 10:33:18

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!

心清如水 2024-09-06 10:33:18

我在开发媒体播放器时遇到了类似的情况。我的解决方案是使用 AudioManager 的 OnAudioFocusChangeListener。

您可以像这样在类中实现侦听器

public class VideoPlayerHelper implements AudioManager.OnAudioFocusChangeListener {

然后覆盖 onAudioFocusChange

@Override
public void onAudioFocusChange(int focusChange) {
    switch (focusChange) {

        //Just fall through by omitting break
        case AudioManager.AUDIOFOCUS_LOSS:
        case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
            LogUtil.log(LogUtil.DEBUG, TAG, "AUDIOFOCUS_LOSS or AUDIOFOCUS_LOSS_TRANSIENT"); //Custom logging class
            if (isPlaying()) {
                pause();
                mAudioManager.abandonAudioFocus(VideoPlayerHelper.this);
            }
            break;
        case AudioManager.AUDIOFOCUS_GAIN:
            LogUtil.log(LogUtil.DEBUG, TAG, "AUDIOFOCUS_GAIN"); //Custom logging class
            break;
        default:
            break;
    }
}

这里的关键是 AudioManager.AUDIOFOCUS_LOSS_TRANSIENT。这是当闹钟响起时听众不断收到的代码(在 Note 5 上)。因此,我只是通过暂停媒体播放器并释放音频焦点来处理与 AudioManager.AUDIOFOCUS_LOSS 相同的 AudioManager.AUDIOFOCUS_LOSS_TRANSIENT

当我们设置媒体播放器时,我在添加数据源之前添加了这一行

player.setAudioStreamType(AudioManager.STREAM_MUSIC);

确保启动媒体播放器的代码中也包含这一行(我将其放在启动代码和 onResume 代码中如果应用程序在后台时闹钟响起)。

mAudioManager.requestAudioFocus(VideoPlayerHelper.this, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);

当您关闭闹钟后按下播放按钮时,该行可以帮助您重新获得音频焦点。

当您使用完媒体播放器后,您还应该释放音频焦点。我将这行代码放在 onStoponDetach 方法中。

mAudioManager.abandonAudioFocus(VideoPlayerHelper.this);

它的设置并不像您想象的那么多,它允许您在引入意外音频(例如闹钟或计时器响起)时调整媒体播放器。

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

public class VideoPlayerHelper implements AudioManager.OnAudioFocusChangeListener {

Then you override onAudioFocusChange

@Override
public void onAudioFocusChange(int focusChange) {
    switch (focusChange) {

        //Just fall through by omitting break
        case AudioManager.AUDIOFOCUS_LOSS:
        case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
            LogUtil.log(LogUtil.DEBUG, TAG, "AUDIOFOCUS_LOSS or AUDIOFOCUS_LOSS_TRANSIENT"); //Custom logging class
            if (isPlaying()) {
                pause();
                mAudioManager.abandonAudioFocus(VideoPlayerHelper.this);
            }
            break;
        case AudioManager.AUDIOFOCUS_GAIN:
            LogUtil.log(LogUtil.DEBUG, TAG, "AUDIOFOCUS_GAIN"); //Custom logging class
            break;
        default:
            break;
    }
}

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 handled AudioManager.AUDIOFOCUS_LOSS_TRANSIENT the same as AudioManager.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

player.setAudioStreamType(AudioManager.STREAM_MUSIC);

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).

mAudioManager.requestAudioFocus(VideoPlayerHelper.this, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);

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 and onDetach methods.

mAudioManager.abandonAudioFocus(VideoPlayerHelper.this);

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).

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