-2

I am using the following code to create an instance of AudioManager :

AudioManager mgr = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
mgr.setStreamMute(AudioManager.STREAM_SYSTEM, true);

while compiling i get an error saying:

Cannot find symbol  : method getSystemService(java.lang.String)
AudioManager mgr = (AudioManager)getSystemService(Context.AUDIO_SERVICE);

I also tried using :

AudioManager mgr = (AudioManager)Context.getSystemService(Context.AUDIO_SERVICE);

and I got the error:

non-static method getSystemService(java.lang.String) cannot be referenced from a static context

Which is the correct way of using the class and how can I avoid the above errors!

Milad Faridnia
  • 9,113
  • 13
  • 65
  • 78
Sid
  • 21
  • 1
  • 8

1 Answers1

3

use following in your main activity class

Context context=getApplicationContext();

then pass context into your constructor of class in which you are creating AudiManager's object. Then use

AudioManager mgr = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
mgr.setStreamMute(AudioManager.STREAM_SYSTEM, true);
kalimba
  • 408
  • 4
  • 14