title: How to play Notification sound even in silent mode and return back to its stat
body: <p>I'm trying to play notification sound even if silent mode is on</p>
  
  <pre><code>Uri uri = Uri.parse(alarmSound);
  notification.setSound(uri);
  AudioManager mobileMode = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
          int previousNotificationVolume = mobileMode.getStreamVolume(AudioManager.STREAM_NOTIFICATION);
          if (ignoreSilent) {
              mobileMode.setStreamVolume(AudioManager.STREAM_NOTIFICATION, mobileMode.getStreamMaxVolume(AudioManager.STREAM_NOTIFICATION), 0);
          }
          NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
          manager.notify(UNIQUE_ID, n);
          try {
              // to delay make a space to finish play sound before return back to original stat.
              Thread.sleep(2000);
          } catch(InterruptedException ex) {
              Thread.currentThread().interrupt();
          }
          mobileMode.setStreamVolume(AudioManager.STREAM_NOTIFICATION, previousNotificationVolume, 0);
  </code></pre>
  
  <p>This should be option that user enable or disable, I'm trying the code above but the things not going well, the sound heard "sometimes" and the phone return to the <strong>Vibration</strong> mode instead of silent, I want to handle lollipop case:</p>
  
  <ul>
  <li>In Silent mode , timed or indefinitely.</li>
  <li>In priority mode, timed or indefinitely.</li>
  </ul>
  
  <p>In another world, I want something to save full stat and return it as it was.</p>
  
  <p>Or, since I know how to play sound with media player, way to get the stat of phone, and if it is silent, make sound playing as a media, with max sound of media, the following how I can play sound with MediaPlayer : </p>
  
  <pre><code>public static void playSound(Context context, Uri alert) {
          MediaPlayer mMediaPlayer = new MediaPlayer();
          try {
              mMediaPlayer.setDataSource(context, alert);
              final AudioManager audioManager = (AudioManager) context
                      .getSystemService(Context.AUDIO_SERVICE);
              if (audioManager.getStreamVolume(AudioManager.STREAM_ALARM) != 0) {
                  mMediaPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);
                  mMediaPlayer.prepare();
                  mMediaPlayer.start();
              }
          } catch (IOException e) {
              Log.e(TAG, e.getMessage());
          }
      }
  </code></pre>
  
  <p>And then like this (but I want to know if phone in silent or vibration or priority mode) : </p>
  
  <pre><code>if (ignoreSilent) {
                  CoreServices.playSound(context, uri);
              } else {
                  // else just follow normal behavior
                  notification.setSound(uri);
              }
  </code></pre>
  
  <p>Finally, I prefer to solve by switching status, at least I will know how android deal things in this part, and working with API 14+.</p>