Cooper Scott
Cooper Scott

Reputation: 706

Getting compile time error in onClick-cannot resolve "context"

I am trying to make my phone mute when I press the button "Mute" but keep getting the same error. Here is the Code so far:

public void onClick(View v) {
    AudioManager audioManager = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
    audioManager.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
}

context in context.getSystem.<blah> is red and cannot resolve. Any help? Thanks!

Upvotes: 0

Views: 76

Answers (1)

tachyonflux
tachyonflux

Reputation: 20211

If your method is in an Activity, you can just get rid of context and call getSystemService() directly

public void onClick(View v){
    AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
    audioManager.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
}

Upvotes: 1

Related Questions