title: How to setStreamVolume on STREAM_NOTIFICATION to value, gives java.lang.SecurityException
body: <p>I am not able to solve this Security Exception, please help:</p>
  <hr />
  <pre><code>java.lang.RuntimeException: 
    at android.app.ActivityThread.performDestroyActivity (ActivityThread.java:5567)
    at android.app.ActivityThread.handleDestroyActivity (ActivityThread.java:5597)
    at android.app.servertransaction.DestroyActivityItem.execute (DestroyActivityItem.java:44)
    at android.app.servertransaction.TransactionExecutor.executeLifecycleState (TransactionExecutor.java:190)
    at android.app.servertransaction.TransactionExecutor.execute (TransactionExecutor.java:105)
    at android.app.ActivityThread$H.handleMessage (ActivityThread.java:2386)
    at android.os.Handler.dispatchMessage (Handler.java:107)
    at android.os.Looper.loop (Looper.java:213)
    at android.app.ActivityThread.main (ActivityThread.java:8178)
    at java.lang.reflect.Method.invoke (Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:513)
    at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1101)
  Caused by: java.lang.SecurityException: 
    at android.os.Parcel.createException (Parcel.java:2071)
    at android.os.Parcel.readException (Parcel.java:2039)
    at android.os.Parcel.readException (Parcel.java:1987)
    at android.media.IAudioService$Stub$Proxy.setStreamVolume (IAudioService.java:2180)
    at android.media.AudioManager.setStreamVolume (AudioManager.java:1193)
    **at sk.alfa.alias.VoiceControl.muteAudio (VoiceControl.java:253)**
    at sk.alfa.alias.MainActivity.onDestroy (MainActivity.java:289)
    at android.app.Activity.performDestroy (Activity.java:8350)
    at android.app.Instrumentation.callActivityOnDestroy (Instrumentation.java:1348)
    at android.app.ActivityThread.performDestroyActivity (ActivityThread.java:5552)
  Caused by: android.os.RemoteException: 
    at com.android.server.audio.AudioService.setStreamVolume (AudioService.java:2530)
    at com.android.server.audio.AudioService.setStreamVolume (AudioService.java:2359)
    at android.media.IAudioService$Stub.onTransact (IAudioService.java:947)
    at com.android.server.audio.HwAudioService.onTransact (HwAudioService.java:1057)
    at android.os.Binder.execTransactInternal (Binder.java:1028)
  </code></pre>
  <hr />
  <p>Problem is in row VoiceControl.java:253 - audio.setStreamVolume(AudioManager.STREAM_NOTIFICATION, actVolume,  0); :</p>
  <hr />
  <pre><code>public class MainActivity extends AppCompatActivity{
  
  static int REQ_CODE_NO_BEEPS = 1;
  
  requestDoNotDisturbPermission();
  
  
  private void requestDoNotDisturbPermission() {
      if(audio.getVolumeFixedPolicy()) {
          NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
          assert notificationManager != null;
          if (notificationManager.isNotificationPolicyAccessGranted()) {
              StaticUtils.save(context,&quot;volume_policy&quot;,true);
          } else {
              StaticUtils.save(context,&quot;volume_policy&quot;,false);
          }
      }else{
          StaticUtils.save(context,&quot;volume_policy&quot;,true);
      }
  }
  
  if(!StaticUtils.getBool(context,&quot;volume_policy&quot;)){
                      volumePolicyText.setVisibility(View.VISIBLE);
                      volumePolicyText.setOnClickListener(new View.OnClickListener() {
                          @Override
                          public void onClick(View view) {
                              // Ask the user to grant access
                              Intent intent = new Intent(android.provider.Settings.ACTION_NOTIFICATION_POLICY_ACCESS_SETTINGS);
                              startActivityForResult(intent, MainActivity.REQ_CODE_NO_BEEPS);
                          }
                      });
                  }
  
  
  
  
  
  @Override
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
      super.onActivityResult(requestCode, resultCode, data);
      if (requestCode == MainActivity.REQ_CODE_NO_BEEPS) {
          if(audio.getVolumeFixedPolicy()){
              NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
              assert notificationManager != null;
              if (notificationManager.isNotificationPolicyAccessGranted()) {
                  StaticUtils.save(context,&quot;volume_policy&quot;,true);
              }
          }
          volumePolicyText.setVisibility(View.GONE);
          Intent i = new Intent(getApplicationContext(),MainActivity.class);
          startActivity(i);
      }
  }
  
  }
  
  public VoiceControl(Context context) {
      this.context = context;
      audio = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
      assert audio != null;
      actVolume = audio.getStreamVolume(AudioManager.STREAM_MUSIC);
  }
  
  public boolean getVolumeFixedPolicy(){
      volumeFixedPolicy = audio.isVolumeFixed();
      return volumeFixedPolicy;
  }
  
  public void muteAudio(Boolean muted){
      boolean notificationValid = true;
      if(getVolumeFixedPolicy()){
          if(!StaticUtils.getBool(context,&quot;volume_policy&quot;)){
              notificationValid = false;
          }
      }
      if(notificationValid){
          if(muted){
                  audio.adjustStreamVolume(AudioManager.STREAM_NOTIFICATION, AudioManager.ADJUST_MUTE, AudioManager.FLAG_VIBRATE);
                  audio.adjustStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.ADJUST_MUTE, AudioManager.FLAG_VIBRATE);
          }else{
                  audio.setStreamVolume(AudioManager.STREAM_NOTIFICATION, actVolume,  0);
                  audio.adjustStreamVolume(AudioManager.STREAM_NOTIFICATION, AudioManager.ADJUST_UNMUTE, 0);
                  audio.setStreamVolume(AudioManager.STREAM_MUSIC, actVolume, 0);
                  audio.adjustStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.ADJUST_UNMUTE,0);
          }
      }
  }
  </code></pre>
  <hr />
  <p>Here in function muteAudio I am using also solution to fixedvolume policy, where I added to manifest
  <code>&lt;uses-permission android:name=&quot;android.permission.ACCESS_NOTIFICATION_POLICY&quot;/&gt;</code>.
  When it is needed programatically unmute I need to set specific volume for NOTIFICATION.
  Some devices throw these RuntimeExceptions and SecurityException, what is wrong ?</p>