简体   繁体   English

虽然设置了手机静音模式,但在Android通知中播放声音

[英]Play sound in Android notifications although phone silent mode is set

My app is showing notifications, and when a notification is shown, a sound is played. 我的应用程序正在显示通知,当显示通知时,会播放声音。 But when my phone is on "silent mode" the notification is not played. 但是当我的手机处于“静音模式”时,不会播放通知。 I want to "override" volume settings, and play a sound although silent mode is set. 我想“覆盖”音量设置,并播放声音虽然设置了静音模式。 There is a way to do it? 有办法吗?

AudioManager am;
am= (AudioManager) getBaseContext().getSystemService(Context.AUDIO_SERVICE);
am.setRingerMode(AudioManager.RINGER_MODE_NORMAL);

Hi you can use MediaPlayer as the notification sound, by starting a Service that play's the MediaPlayer when the notification is shown. 嗨,您可以使用MediaPlayer作为通知声音,方法是在显示通知时启动播放MediaPlayer的服务。

To make the sound stop, stop the service in the BroadCastReceiver of the notification's PendingIntent. 要使声音停止,请在通知的PendingIntent的BroadCastReceiver中停止该服务。

Also, don't forget to stop the sound when the notification cancels. 此外,不要忘记在通知取消时停止声音。

use this to enable notification sound - 用这个来启用通知声音 -

Settings.System.putInt(getContentResolver(),android.provider.Settings.System.NOTIFICATION_SOUND, 1);

Dont forget give permission in manifest - 不要忘记在清单中给予许可 -

<uses-permission android:name="android.permission.WRITE_SETTINGS"/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM