按音量按钮时如何使通知静音?

时间:2015-05-16 15:00:06

标签: java android notifications alarmmanager

我创建了一个使用闹钟管理器在特定时间触发的通知,它可以工作,但是当按下音量按钮时它不会停止播放,就像电话打入时一样

这是我如何创建通知

        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());

希望有人能帮助我弄清楚如何实现这一目标。提前致谢

1 个答案:

答案 0 :(得分:0)

试试这个。首先,构建通知以使用系统警报音频流而不是默认(铃声)音频流。

---您的通知代码

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

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