mohamed_abdullah
mohamed_abdullah

Reputation: 475

Why can't instantiate object from AudioManager with new operator?

I am novice with the android. I am learning how to make audio and control the voice of this audio with AudioManager class. But I have a simple question, according to the Documentation of the android developers it says that

AudoiManager is a public class AudioManager extends Object and to instantiate an object from this class Use Context.getSystemService(Context.AUDIO_SERVICE)

I got confused here Why we can't instantiate this class with the new operator and what does Context have to do with this?

Upvotes: 2

Views: 1228

Answers (1)

Ally
Ally

Reputation: 843

Since this question concerns AudioManager object in Android, I would also like to add how you can achieve this using Kotlin as well since I had same challenge and I managed to sort using the below approach.

private lateinit var audioManager: AudioManager

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity)

    audioManager = this.getSystemService(Context.AUDIO_SERVICE) as AudioManager
//...your other codes
}

Upvotes: 2

Related Questions