我正在使用闹钟管理器创建一个在特定时间触发的通知,它可以正常工作,但在按下音量按钮时不会停止播放,例如有来电时
这是我创建通知的方式
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());
希望有人能帮助我弄清楚如何实现这一目标。提前致谢
最佳答案
试试这个。首先,构建通知以使用系统警报 音频流而不是默认(响铃)音频流。
---你的通知码
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
。]
然后在您的 Activity 中配置音量控件以调整闹钟音量。
--- MainActivity.java
setVolumeControlStream(AudioManager.STREAM_ALARM);
在此 Activity 中,音量控件现在将调整警报音量,这将控制您的通知警报。
关于java - 按下音量按钮时如何使通知静音?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30277172/