虽然手机静音,但声音通知

时间:2014-10-14 05:40:19

标签: android notifications android-notifications

我正在使用Android的NotificationManager创建通知。

是否可以以这样的方式“覆盖”手机的音量(静音)设置,通知的声音始终播放?

我需要这个的原因如下: 通知非常重要,单靠振动可能还不够。必须提醒用户。因此即使手机静音或音量很低,也应播放声音。

2 个答案:

答案 0 :(得分:7)

是的,有可能,

MediaPlayer mMediaPlayer;
Uri notification = null;
notification = RingtoneManager
                .getDefaultUri(RingtoneManager.TYPE_ALARM);

mMediaPlayer = new MediaPlayer();
        mMediaPlayer.setDataSource(ctx, notification);
        // mMediaPlayer = MediaPlayer.create(ctx, notification);
        final AudioManager audioManager = (AudioManager) ctx
                .getSystemService(Context.AUDIO_SERVICE);

        mMediaPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);

        mMediaPlayer.prepare();
        // mMediaPlayer.start();
        mMediaPlayer.setLooping(true);
        mMediaPlayer.setOnPreparedListener(new OnPreparedListener() {
            public void onPrepared(MediaPlayer arg0) {
                mMediaPlayer.seekTo(0);
                mMediaPlayer.start();

            }

        });

答案 1 :(得分:3)

您可以将这样的RINGING模式从静音更改为普通

AudioManager mobilemode = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
        mobilemode.setRingerMode(AudioManager.RINGER_MODE_NORMAL);


        // Turn on all sound

        // turn on sound, enable notifications
        mobilemode.setStreamMute(AudioManager.STREAM_SYSTEM, false);
        //notifications
        mobilemode.setStreamMute(AudioManager.STREAM_NOTIFICATION, false);
        //alarm
        mobilemode.setStreamMute(AudioManager.STREAM_ALARM, false);
        //ringer
        mobilemode.setStreamMute(AudioManager.STREAM_RING, false);
        //media
        mobilemode.setStreamMute(AudioManager.STREAM_MUSIC, false);


        // Turn off all sound

        // turn off sound, disable notifications
        mobilemode.setStreamMute(AudioManager.STREAM_SYSTEM, true);
        //notifications
        mobilemode.setStreamMute(AudioManager.STREAM_NOTIFICATION, true);
        //alarm
        mobilemode.setStreamMute(AudioManager.STREAM_ALARM, true);
        //ringer
        mobilemode.setStreamMute(AudioManager.STREAM_RING, true);
        //media
        mobilemode.setStreamMute(AudioManager.STREAM_MUSIC, true);

对于您的情况,您可以尝试这样的事情

int previousNotificationVolume =mobilemode.getStreamVolume(AudioManager.STREAM_NOTIFICATION);

        mobilemode.setStreamVolume(AudioManager.STREAM_NOTIFICATION,mobilemode.getStreamMaxVolume(AudioManager.STREAM_NOTIFICATION), 0);

        // Play notification sound



        // Set notification sound to its previous 

        mobilemode.setStreamVolume(AudioManager.STREAM_NOTIFICATION,previousNotificationVolume, 0);