【问题标题】:How to Silence the Notification when pressing volume button?按下音量按钮时如何使通知静音?
【发布时间】:2015-07-28 10:06:25
【问题描述】:

我正在使用警报管理器创建一个在特定时间触发的通知,它可以工作,但在按下音量按钮时它不会停止播放,例如来电时

这是我创建通知的方式

        NotificationCompat.Builder notification = new NotificationCompat.Builder(this);
        notification.setAutoCancel(true);

        notification.setSmallIcon(R.drawable.notification_icon);
        notification.setTicker("This's the ticker");//status bar text
        notification.setContentTitle("title");
        notification.setContentText("text");

        notification.setPriority(1);

        Uri sound = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.notifysound);
        notification.setSound(sound);

        String ns = Context.NOTIFICATION_SERVICE;
        NotificationManager manager = (NotificationManager) this.getSystemService(ns);

        manager.notify(0,notification.build());

希望有人能帮助我弄清楚如何实现这一目标。在此先感谢

【问题讨论】:

  • 我不是安卓开发者,所以我不能保证这是最好的解决方案,但是你不能创建一个在按下音量键时触发的事件监听器,调整音量听众?
  • 我试过使用监听器,但没有用。感谢您的帮助

标签: java android notifications alarmmanager


【解决方案1】:

试试这个。首先,构建通知以使用系统 alarm 音频流而不是默认(环形)音频流。

--- 你的通知代码

Uri sound = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.notifysound);
NotificationCompat builder = NotificationCompat.builder(this)
        .setPriority(NotificationCompat.PRIORITY_HIGH)
        .setCategory(NotificationCompat.CATEGORY_ALARM)
        .setSound(sound, AudioManager.STREAM_ALARM)
        .setAutoCancel(true)
        .setSmallIcon(R.drawable.notification_icon)
        .setTicker("This's the ticker")
        .setContentTitle("title")
        .setContentText("text");
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
notificationManager.notify(NOTIFICATION_ID, builder.build());

[也许像这样设置通知优先级和类别也会使您的应用程序的警报在通话期间静音。查看AudioManager。]

然后配置音量控件以在您的活动中调整警报音量。

--- MainActivity.java

setVolumeControlStream(AudioManager.STREAM_ALARM);

在此活动中,音量控件现在将调整警报音量,这将控制您的通知警报。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-12
    相关资源
    最近更新 更多