HelloCW
HelloCW

Reputation: 2295

Why did the system display an warning information when I use AudioManager?

The following code display an warning information "The static field Context.AUDIO_SERVICE should be accessed in a static way", why? how to fix it? thanks!

public class CallerMain extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.caller_main);

        findViewById(R.id.btnEnableCall).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                AudioManager mAudioManager = (AudioManager) getSystemService(getApplicationContext().AUDIO_SERVICE);
                int max = mAudioManager.getStreamMaxVolume( AudioManager.STREAM_RING );
                mAudioManager.setStreamVolume(AudioManager.STREAM_RING, max, 0);

            }
        });     
    }

}

Upvotes: 0

Views: 65

Answers (2)

NoXSaeeD
NoXSaeeD

Reputation: 924

this is java object oriented thing :

you geting static variable name called AUDIO_SERVICE non static way so instead of use object to call static variable use class name to do that.

in your case :

1- object name you calling from it static variable : appContext 2- static variable name you getting useing object : AUDIO_SERVICE

how to fix :

1- simple call static variable static way without useing object, use Class name like so:

AudioManager mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE)   

Upvotes: 1

Pragnani
Pragnani

Reputation: 20155

AUDIO_SERVICE is the static variable in Context class

you should call like this

Context.AUDIO_SERVICE

i.e

AudioManager mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE)

Upvotes: 2

Related Questions