繁体   English   中英

Android:通知声音持续时间

[英]Android: Notification sound duration

尝试设置通知警报声音的持续时间时遇到问题。 现在是这样的:

NotificationManager mNotificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);
    // mId allows you to update the notification later on.
    mBuilder.setAutoCancel(true);

    Uri alarmSound = Uri.parse(sharedPref.getString(
                "notifications_new_message_ringtone", ""));

    if (alarmSound != null) {
        mediaPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);
        try {
            mediaPlayer.setDataSource(context, alarmSound);
            mediaPlayer.prepare();
        } catch (IllegalArgumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SecurityException e) {

        }

         mBuilder.setSound(alarmSound);



        mNotificationManager.notify(notificationId, mBuilder.build());

        new Thread() {
            public void run() {

                try {
                    sleep(sharedPref.getInt("alarm_duration",2000) * DateUtils.SECOND_IN_MILLIS);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                mNotificationManager.cancel(notificationId);
            }
        }.start();
    }

如您所见,我在sharedPref变量给定的一段时间后停止了通知(用户可以进行设置)。 关键是这一行:“ mNotificationManager.cancel(notificationId)”不仅会停止声音,还会停止整个通知(灯光,振动,甚至是通知抽屉上的图标)。

有没有办法只关闭通知而不完全取消通知?

提前致谢!。

没有办法只能取消声音。 您可以取消整个Notification ,然后重新发布而没有声音。 或者,您可以将AudioManager.STREAM_NOTIFICATION流类型静音x倍的时间,然后在Notification声音结束后将其取消静音。

要控制音量:

    setVolumeControlStream(AudioManager.STREAM_NOTIFICATION);

要使Notification声音静音:

    final AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
    am.setStreamMute(AudioManager.STREAM_NOTIFICATION, true or false);

您知道收到通知并触摸通知栏时会发生什么吗? 它不会取消通知,只是停止其声音。 似乎应该有一种从代码中执行相同操作的方法。

暂无
暂无

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

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