Notification with sound although phone is muted

前端 未结 2 874
轻奢々
轻奢々 2021-01-12 19:03

I am creating a notification with Android\'s NotificationManager.

Is it possible to \'override\' the phone\'s volume (mute) settings in such a way, that the notifica

2条回答
  •  囚心锁ツ
    2021-01-12 19:40

    You can change RINGING mode like this from silent to normal

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

    FOR YOUR CASE YOU CAN TRY SOMETHING LIKE THIS

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

提交回复
热议问题