【问题标题】:Android - Notification alarm soundAndroid - 通知警报声
【发布时间】:2012-05-16 14:38:55
【问题描述】:

我已经实现了一个带有警报声的通知,如下所示:

    PendingIntent pi = PendingIntent.getActivity(context, id, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    Notification note = new Notification(R.drawable.icon, "Alarm", System.currentTimeMillis());
    note.setLatestEventInfo(context, "Alarm", title + " (alarm)", pi);
    Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
    if(alarmSound == null){
        alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
        if(alarmSound == null){
            alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        }
    }
    note.sound = alarmSound;
    note.defaults |= Notification.DEFAULT_VIBRATE;
    note.flags |= Notification.FLAG_AUTO_CANCEL;
    manager.notify(id, note);

闹钟的声音响起,但我面临的问题是它一直在播放,直到我向下拖动通知栏。播放时,我将活动显示为对话框(弹出窗口),其中包含有关当前警报播放的一些信息。 我希望能够在对话框(弹出活动)中的按钮中取消此警报声。

我怎样才能做到这一点? 谢谢。

【问题讨论】:

  • 能不能把完整的代码放上来

标签: android notifications alarmmanager alarm


【解决方案1】:

取消声音只是我们manager.cancel(id) 在您的对话框按钮中单击 id 是您在使用manager.notify(id,note) 创建通知时设置的 id

【讨论】:

  • 但它也不会删除我的通知吗?
  • 我想保留通知,但删除声音。如果我使用正常的通知声音,它会响一次,但使用警报声,它会重复自己
  • 如果您不想删除通知,则必须使用MediaPlayer 播放声音而不是通知。当您的待处理意图触发时,您可以播放声音
  • 好的,我会调查的!那我怎样才能让媒体播放器停止另一个活动呢?
  • 只是想一想,在您深入 MediaPlayer 之前,只需将 Notification.DEFAULT_SOUND 添加到 note.defaults 即可获得良好的结果。我已经这样做了(在 Galaxy S3、Android OS 4.0.4 上进行测试),但它并没有像你的情况那样一直播放。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多